(一)监督学习模型之线性分类器

一、解决问题

使用线性分类模型从事良/恶性肿瘤的预测任务

二、数据地址

http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data

三、代码实现

1、获得数据

import pandas as pd
import numpy as np


column_names = ['Sample code number', 'Clump Thickness', 'Uniformity of Call Size'
               'Uniformity of Cell Shape', 'Marginal Adhesion', 'Single Epithelial Cell Size',
               'Bare Nuclei', 'Bland Chromatin', 'Normal Nucleoli', 'Mitoses', 'class']

data = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data', names = column_names)

data.shape

# drop nan
# 去掉缺失值
data = data.replace(to_replace='?', value=np.nan)
data = data.dropna(how = 'any')
data.shape

2、准备数据

from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data[column_names[1:9]], data[column_names[9]], test_size = 0.25, random_state = 33)

y_train.value_counts()

y_test.value_counts()

3、标准化数据

# 标准化数据,保证每个维度的特征数据方差为1,均值为0,
# 让预测的结果不会被每个过大的特征值主导
ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)

4、建立模型预测数据

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)

5、性能分析

from sklearn.metrics import classification_report
print(lr.score(X_test, y_test))

print(classification_report(y_test, lr_y_predict, target_names = ['Benign', 'Malignant']))

print(classification_report(y_test, sgdc_y_predict, target_names = ['Benign', 'Malignant']))

**

四、代码地址
——

**

你可能感兴趣的:((一)监督学习模型之线性分类器)