利用基於線性假設的線性分類器LogisticRegression/SGDClassifier進行二類分類(複習1)
阿新 • • 發佈:2019-01-03
本文是個人學習筆記,內容主要涉及LR(LogisticRegression)和SGD(SGDClassifier)對breast-cancer資料集進行線性二分類。
線性分類器:假設資料特徵與分類目標之間是線性關係的模型,通過累加計算每個維度的特徵與各自權重的乘積來幫助類別決策。
二類分類任務的評估指標:混淆矩陣
import numpy as np
import pandas as pd
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.to_csv(r'data.csv',index=None)
data=pd.read_csv(r'data.csv')
data
data=data.replace(to_replace='?',value=np.nan) #將?替換為標準缺失值
data=data.dropna(how='any') #丟棄帶有缺失值的資料樣本
data.shape
#Output:(683, 11)
from distutils.version import LooseVersion as Version
from sklearn import __version__ as sklearn_version
from sklearn import datasets
if Version(sklearn_version) < '0.18':
from sklearn.cross_validation import train_test_split
else:
from sklearn.model_selection 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)
y_train.value_counts()
#Output:2 344
# 4 168
# Name: Class, dtype: int64
y_test.value_counts()
#Output:2 100
# 4 71
# Name: Class, dtype: int64
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression,SGDClassifier
ss=StandardScaler() #資料幅度標準化
X_train=ss.fit_transform(X_train)
X_test=ss.transform(X_test) #注意,對測試集不需要fit,用和訓練集一樣的變換
lr=LogisticRegression() #初始化
lr.fit(X_train,y_train) #fit訓練模型引數
lr_y_predict=lr.predict(X_test) #用訓練好的模型lr進行預測,結果儲存在變數lr_y_predict裡
sgdc=SGDClassifier()
sgdc.fit(X_train,y_train)
sgdc_y_predict=sgdc.predict(X_test)
from sklearn.metrics import classification_report #Accuracy,Precision,Recall,f1-score
print('Accuracy of LR Classifier:',lr.score(X_test,y_test))
print(classification_report(y_test,lr_y_predict,target_names=['Benign','Malignant']))
print('Accuarcy of SGD Classifier:',sgdc.score(X_test,y_test))
print(classification_report(y_test,sgdc_y_predict,target_names=['Benign','Malignant']))