机器学习---分类学习 笔记

机器学习监督学习---分类学习 笔记

二分类
多类分类
多标签分类

良/恶性乳腺癌肿瘤数据预处理

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)

准备良/恶性乳腺癌肿瘤训练,测试数据

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

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

使用逻辑斯蒂回归与随机梯度参数估计对训练数据进行学习,并且根据测试样本特征进行预测

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: ? ,还没解决

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

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'])

你可能感兴趣的:(机器学习---分类学习 笔记)