機器學(xué)習(xí)---分類學(xué)習(xí) 筆記

機器學(xué)習(xí)監(jiān)督學(xué)習(xí)---分類學(xué)習(xí) 筆記

二分類
多類分類
多標(biāo)簽分類

良/惡性乳腺癌腫瘤數(shù)據(jù)預(yù)處理

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')
print data.shape

output:
(699, 11)

準(zhǔn)備良/惡性乳腺癌腫瘤訓(xùn)練,測試數(shù)據(jù)

from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data[column_names[1:10]], data[column_names[10]], test_size=0.25, random_state=33)
print y_train.value_counts()
print y_test.value_counts()
output:
2    341
4    183
Name: Class, dtype: int64
2    117
4     58
Name: Class, dtype: int64

使用線性分類模型從事良/惡性腫瘤預(yù)測任務(wù)

使用邏輯斯蒂回歸與隨機梯度參數(shù)估計對訓(xùn)練數(shù)據(jù)進(jìn)行學(xué)習(xí),并且根據(jù)測試樣本特征進(jìn)行預(yù)測

from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier

ss = StandardScaler()
X_train = X_train.astype(float)
X_test = X_test.astype(float)

X_train = ss.fit_transform(X_train)
X_test = ss.fit_transform(X_test)

lr = LogisticRegression()
sgdc = SGDClassifier()
lr.fit(X_train, y_train)
lr_y_predict = lr.predict(X_test)
sgdc.fit(X_train, y_train)
sgdc_y_predict = sgdc.predict(X_test)

這里一直報錯 ValueError: could not convert string to float: ? ,還沒解決

使用線性分類模型從事良/惡性腫瘤預(yù)測任務(wù)的性能分析

from sklearn.metrics import classification_report

print 'Accuracy of LR Classifier: ', lr.score(X_test, y_test)
print classification_report(y_test, lr_y_predict, target_names=['Benign','Malignant'])
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容