使用LogisticRegression和SGDClassifier對良/惡性腫瘤進行分類,並計算出準確率召回率和F1的值
阿新 • • 發佈:2018-12-30
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 24 10:08:40 2017
@author: liuyajun
"""
import pandas as pd
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import classification_report
column_names = ['Sample code number','Clump Thickness','Uniformity of Cell Size','Uniformity of Cell shape','Marginal Adhesion','Single Epithelial cell Size','Bare Nuclei','Bland Chromation','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)#用numpy中的nan來代替資料中的空值(之前使用?來表示空)
data = data.dropna(how='any')#刪除所有帶有空值的資料
#print(data.shape)#輸出刪除空值後資料的形狀
#使用函式對資料進行切分,訓練資料75%,測試資料25%
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()
y_test.value_counts()
#首先使用線性模型從事良、惡性腫瘤的預測任務
# 1 標準化資料,保證每個維度的特徵資料方差為1,均值為0.使得預測結果不會被某些維度過大的特徵值主導
ss = StandardScaler()
X_train=ss.fit_transform(X_train)
X_test=ss.transform(X_test)
# 2 初始化Logis提出Regression和SGDClassifier(隨機引數梯度估計)
lr = LogisticRegression()
sgdc = SGDClassifier()
#使用LogisticRegression 中的fit方法訓練引數
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)
#利用LogisticRegression模組自帶的score獲得模型在測試機上的準確性
print('Accuracy of LR Classifier:',lr.score(X_test,y_test))
#利用Classification_report模組獲得LogisticRegression的準確率召回率和F1
print(classification_report(y_test,lr_y_predict,target_names=['Benign','Malignant']))#malignant惡性的benign良性的
print('Accuracy of SGD Classifier:',sgdc.score(X_test,y_test))
print(classifier_report(y_test,y_sgdc_test,target_names=['Bengin','Malignant']))
"""
Created on Tue Oct 24 10:08:40 2017
@author: liuyajun
"""
import pandas as pd
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import classification_report
column_names = ['Sample code number','Clump Thickness','Uniformity of Cell Size','Uniformity of Cell shape','Marginal Adhesion','Single Epithelial cell Size','Bare Nuclei','Bland Chromation','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)#用numpy中的nan來代替資料中的空值(之前使用?來表示空)
data = data.dropna(how='any')#刪除所有帶有空值的資料
#print(data.shape)#輸出刪除空值後資料的形狀
#使用函式對資料進行切分,訓練資料75%,測試資料25%
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()
y_test.value_counts()
#首先使用線性模型從事良、惡性腫瘤的預測任務
# 1 標準化資料,保證每個維度的特徵資料方差為1,均值為0.使得預測結果不會被某些維度過大的特徵值主導
ss = StandardScaler()
X_train=ss.fit_transform(X_train)
X_test=ss.transform(X_test)
# 2 初始化Logis提出Regression和SGDClassifier(隨機引數梯度估計)
lr = LogisticRegression()
sgdc = SGDClassifier()
#使用LogisticRegression 中的fit方法訓練引數
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)
#利用LogisticRegression模組自帶的score獲得模型在測試機上的準確性
print('Accuracy of LR Classifier:',lr.score(X_test,y_test))
#利用Classification_report模組獲得LogisticRegression的準確率召回率和F1
print(classification_report(y_test,lr_y_predict,target_names=['Benign','Malignant']))#malignant惡性的benign良性的
print('Accuracy of SGD Classifier:',sgdc.score(X_test,y_test))
print(classifier_report(y_test,y_sgdc_test,target_names=['Bengin','Malignant']))