已知 UCI 数据集 breast-cancer-wisconsin,breast-cancer-wisconsin 是肿瘤学家研究切片组织,描述组织各种特征决定肿瘤是良性还是恶性的数据集,数据集共有699个样本个数,有11个特征,第一个为id number,最后一个为class(有无癌症的分类),该数据集包含若干个缺失数据。要求:
(1)首先对缺失数据进行处理,并说明处理的方法。
(2)随机选取数据集的 70%的数据构成训练集,剩余30%数据构成测试集,并应用逻辑回归算法对测试集进行分类,采用Accuracy作为评估算法的标准。
(3)在(1)(2)的基础上采用5-折交叉验证方式进行试验,得到实验的Accuracy值。
# -*- coding: utf-8 -*-
#####一:作业提交#####
import pandas as pd
# from scipy.interpolate import lagrange
inputfile='C:/Users/PEXYGGJF/Desktop/ly/text/breast-cancer-wisconsin.csv'
outputfile='C:/Users/PEXYGGJF/Desktop/ly/work/ly.csv'
data=pd.read_excel(inputfile)
data['Bare Nuclei'][(data['Bare Nuclei']<0) | (data['Bare Nuclei']>11)]=None
import pandas as pd
import numpy as np
column_names = ['Sample code number','Clump Thickness','Uniformity of Cell Size','Uniformity of Cell Shape','Marginal Adhesion','Single Epithelial Cell Size','Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class']
data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin//breast-cancer-wisconsin.data',names=column_names)
data = data.replace(to_replace='?',value=np.nan) #非法字符的替代,缺失值处理
data = data.dropna(how='any') #去掉空值,any:出现空值行则删除
print(data.shape)
print(data.head())
##################################################################################
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report
# from sklearn.externals
import joblib
import pandas as pd
import numpy as np
def logistic():
"""
逻辑回归做二分类进行癌症预测(根据细胞的属性特征)
"""
# 构造列标签名字,一共11个
column = ['Sample code number','Clump Thickness', 'Uniformity of Cell Size',
'Uniformity of Cell Shape','Marginal Adhesion', 'Single Epithelial Cell Size',
'Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class']
# 读取数据
data = pd.read_csv("E:/文档资料/python数据分析和数据挖掘/work/python report/breast-cancer-wisconsin.csv",
engine='python',names=column)
print(data)
# 缺失值进行处理
data = data.replace(to_replace='?', value=np.nan)
data = data.dropna()
# 进行数据的分割,1到10列为特征值,11列为目标值
x_train, x_test, y_train, y_test = train_test_split(data[column[1:10]], data[column[10]], test_size=0.25)
# 进行标准化处理
std = StandardScaler()
x_train = std.fit_transform(x_train)
x_test = std.transform(x_test)
# 逻辑回归预测
lg = LogisticRegression(C=1.0)
lg.fit(x_train, y_train)
print(lg.coef_)
y_predict = lg.predict(x_test)
print("准确率:", lg.score(x_test, y_test))
print("召回率:", classification_report(y_test, y_predict, labels=[2, 4], target_names=["良性", "恶性"]))
#保存训练好的模型
joblib.dump(lg, "./lg.pkl")
#加载模型,预测自己的数据
model = joblib.load("./lg.pkl")
# 读取数据,数据为相同格式下需要预测的数据。和前文七、(1)(2)的操作一样
data = pd.read_csv("E:/文档资料/python数据分析和数据挖掘/work/python report/new-breast-cancer-wisconsin.csv",
engine='python',names=column)
xx_test= data[column[1:10]] #获取特征值1 到 10 列,
xx_test = std.transform(xx_test)
yy_predict = model.predict(xx_test)
print("保存的模型预测的结果:", yy_predict)
if __name__ == "__main__":
logistic()
# -*- coding:utf-8 -*-
#####二:较标准呈现#####
import numpy as np #导入numpy库
import random #导入random库,产生随机数
import csv #导入csv格式文件
def loadDataSet(): #加载数据,标签,特征值
trainMat = [];data0=[] #训练集数组,测试集数组
data = csv.reader(open('E:/pywork/test/sy-3/breast-cancer-wisconsin.csv'))
for line in data: #读入每一行数据,判断数据集中是否有“?”,对缺失值进行处理
if "?" in line:
continue
lineArr = []
if line[0] != '':
for i in range(2, 12): #读取第二列到第十一列的标签的每一行数据
lineArr.append(float(line[i]))
data0.append(lineArr) #添加数据
m ,n = np.shape(data0)
times = int(m*0.7) #70%分割线
for i in range(times):
randIndex = int(random.uniform(0,len(data0))) #产生随机数
trainMat.append(data0[randIndex])
del(data0[randIndex])
testMat = data0[:]
return trainMat, testMat #返回测试集,训练集
def depart(dataset):
dataMat =[] ; labelMat = []
for line in dataset:
lineArr = []
for i in range(9):
lineArr.append(line[i])
dataMat.append(lineArr)
labelMat.append(line[9])
return dataMat,labelMat
def sigmoid(inX): #S函数
return 1.0/(1+np.exp(-inX))
def stocGradAscent1(dataMatrix,LabelMat,numIter=500): #随机梯度上升算法
m,n = np.shape(dataMatrix)
weights = np.ones(n)
for j in range(numIter):
dataIndex =list( range(m))
for i in range(m):
alpha = 4/(1.0+j+i)+0.01 #每次迭代时更新alpha值
randIndex = int(random.uniform(0,len(dataIndex))) #随机选取更新
h = sigmoid(sum(dataMatrix[randIndex]*weights))
error = LabelMat[randIndex] - h
weights = weights + alpha * error * dataMatrix[randIndex]
del(dataIndex[randIndex])
return weights
def classifyVector(inX,weights): #逻辑回归分类函数,inX特征向量,weights回归系数
prob = sigmoid(sum(inX*weights))
if prob > 0.5:return 2.0
else:return 4.0
def colicTest(): #打开测试集和训练集,并对数据进行格式化处理
trainMat, testMat = loadDataSet()
trainSet, trainLabel = depart(trainMat) #导入类别标签为最后一项
trainWeights = stocGradAscent1(np.array(trainSet),trainLabel,500) #使用随机梯度上升算法计算回归系数向量
rightCount = 0; numTestVec = 0.0
for line in testMat: #格式化测试集
numTestVec += 1.0
lineArr = []
for i in range(9): #导入特征值,有9个特征
lineArr.append(float(line[i]))
if int(classifyVector(np.array(lineArr),trainWeights)) == int(line[9]): #使用训练集计算出回归系数对测试集进行分类,并对比测试集的类别标签,计算错误数量
rightCount += 1
rightCount = (float(rightCount)/numTestVec) #正确率
print ("The right rate of this test is: %f" % rightCount)
return rightCount
def multiTest(): #colicTest函数10次,取正确率平均值
numTests = 10; rightSum = 0.0
for k in range(numTests):
rightSum += colicTest()
print ("after %d iterations the average error rate is: %f" % (numTests, rightSum/float(numTests)))
multiTest()