Advertisement

对良/恶性肿瘤的预测python代码实现

阅读量:

该代码使用Python和机器学习库(如pandas、matplotlib、numpy、sklearn)对乳腺癌数据集进行分析。首先,读入训练集和测试集数据,分别包含'Clump Thickness'和'Cell Size'特征。通过绘制散点图,展示了测试集中的正负分类样本分布。接着,利用numpy生成随机直线,并绘制在散点图上。然后,使用sklearn的LogisticRegression模型进行分类,分别使用前十个样本和所有样本进行训练,并绘制了相应的决策边界。最后,测试模型的准确率,结果显示使用所有数据训练的模型表现更好。

导入Python数据分析库pandas,并以简写pd引用;调用matplotlib库进行数据可视化;导入numpy库进行快速数值计算;从sklearn机器学习库中导入Logistic回归模型。

df_train = pd.read_csv(r'E:\BaiduNetdiskDownload\Datasets\Breast-Cancer\breast-cancer-train.csv')#通过pandas库导入训练数据集
df_test = pd.read_csv(r'E:\BaiduNetdiskDownload\Datasets\Breast-Cancer\breast-cancer-test.csv')#通过pandas库导入测试数据集

#选取ClumpThickness与cell size作为特征,构建测试集中的正负分类样本
df_test_negative = df_test.loc[df_test['Type']==0][['Clump Thickness','Cell Size']]
df_test_positive = df_test.loc[df_test['Type']==1][['Clump Thickness','Cell Size']]
plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
plt.show()
#利用numpy中的random函数随机采样直线的截距和系数
intercept =np.random.random([1])
coef =np.random.random([2])
lx =np.arange(0,12)
ly = (-intercept-lx*coef[0])/coef[1]
#绘制随机产生的直线
plt.plot(lx,ly,c='yellow')
plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
plt.show()

#利用sklearn中的LogisticRegression回归分类器
#使用前十个样本进行训练
lr = LogisticRegression()
lr.fit(df_train[['Clump Thickness','Cell Size']][:10],df_train['Type'][:10])
print('Testing accuracy(10 training samples):',lr.score(df_test[['Clump Thickness','Cell Size']],df_test['Type']))
intercept=lr.intercept_
coef =lr.coef_[0,:]
#原本的分类器是lxcoef[0]+lycoef[1]+intercept=0,在这里使用ly = (-intyercept-lxcoef[0])/coef[1]
ly = (-intercept-lx
coef[0])/coef[1]
plt.plot(lx,ly,c='green')
plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
plt.show()
#使用所有数据训练,学习直线的系数和截距
lr =LogisticRegression()
lr.fit(df_train[['Clump Thickness','Cell Size']],df_train['Type'])
print('Testing accuracy:',lr.score(df_test[['Clump Thickness','Cell Size']],df_test['Type']))
intercept=lr.intercept_
coef =lr.coef_[0,:]
#原本的分类器是lxcoef[0]+lycoef[1]+intercept=0,在这里使用ly = (-intyercept-lxcoef[0])/coef[1]
ly = (-intercept-lx
coef[0])/coef[1]
plt.plot(lx,ly,c='blue')
plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
plt.show()

全部评论 (0)

还没有任何评论哟~