當(dāng)前位置:
首頁(yè) >
《Python machine learning and practice》—— 良\恶性乳腺癌肿瘤预测
發(fā)布時(shí)間:2024/5/6
71
豆豆
生活随笔
收集整理的這篇文章主要介紹了
《Python machine learning and practice》—— 良\恶性乳腺癌肿瘤预测
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
數(shù)據(jù)集
良\惡性乳腺癌腫瘤預(yù)測(cè)數(shù)據(jù)集
代碼分析
第三方庫(kù)文件
from sklearn.linear_model import LogisticRegression #導(dǎo)入sklearn中的邏輯斯蒂回歸分類器 import matplotlib.pyplot as plt import pandas as pd import numpy as np都是在機(jī)器學(xué)習(xí)中常用的第三方庫(kù)。
導(dǎo)入文件
df_train=pd.read_csv(r'F:\code\AI\Python machine learning and practice\Datasets\Breast-Cancer\breast-cancer-train.csv') df_test=pd.read_csv(r'F:\code\AI\Python machine learning and practice\Datasets\Breast-Cancer\breast-cancer-test.csv')通過(guò)pandas的read_csv導(dǎo)入數(shù)據(jù)集。
確定正負(fù)分類樣本
df_test_nagative=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']]選取“Clump Thickness”與“Cell Size”作為特征,構(gòu)件測(cè)試集中的正負(fù)分類樣本。
繪制圖形
#繪制良性腫瘤樣本點(diǎn),標(biāo)記為紅色的o plt.scatter(df_test_nagative['Clump Thickness'],df_test_nagative['Cell Size'],marker='o',s=200,c='red') #繪制惡性腫瘤樣本點(diǎn),標(biāo)記為黑色的× plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')#繪制x、y軸的說(shuō)明 plt.xlabel('Clump Thickness') plt.ylabel('Cell Size')#顯示圖 plt.show()numpy確定參數(shù)
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')利用numpy中的random函數(shù)隨機(jī)采樣直線的截距和系數(shù)。
訓(xùn)練
lr=LogisticRegression(solver='liblinear')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,:]# 原本這個(gè)分類面應(yīng)該是lx*coef[0]+ly*coef[1]+intercept=0,映射到2維平面上之后,應(yīng)該是: ly=(-intercept-lx*coef[0])/coef[1]plt.plot(lx,ly,c='green')使用前10條訓(xùn)練樣本學(xué)習(xí)直線的系數(shù)和截距。
plt.plot(lx,ly,c='green') plt.scatter(df_test_nagative['Clump Thickness'],df_test_nagative['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()再訓(xùn)練
lr=LogisticRegression(solver='liblinear') #使用所有的訓(xùn)練樣本學(xué)習(xí)直線的系數(shù)和截距 lr.fit(df_train[['Clump Thickness','Cell Size']],df_train['Type']) print('Testing accuracy(10 training samples):',lr.score(df_test[['Clump Thickness','Cell Size']],df_test['Type']))intercept=lr.intercept_ coef=lr.coef_[0,:] ly=(-intercept-lx*coef[0])/coef[1]plt.plot(lx,ly,c='blue') plt.scatter(df_test_nagative['Clump Thickness'],df_test_nagative['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()總結(jié)
以上是生活随笔為你收集整理的《Python machine learning and practice》—— 良\恶性乳腺癌肿瘤预测的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux基础 —— Linux终端命令
- 下一篇: PAT (Basic Level) Pr