本文主要是介绍通俗读完《Python机器学习及实战:从零开始通往Kaggle竞赛之路》,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
线性分类器
线性分类:假设特征与分类结果存在线性关系的模型。
良/恶性肿瘤数据预处理:
原始数据共有699条,11列不同的数值:第一列是id,中间9列为肿瘤的主要特征,并且被量化到1~10之间,最后一列为肿瘤的类型:2表示良性,4为恶性。数据中包含16个缺失值,并且用“?”标出。
导入工具包
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_reportcolumn_names = ['Sample code number','Clump Thickness','Uniformity of Cell Size','Uniformity of Cell shape','Marginal Adhesion','Single Epithelial Cell Size','Brae 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 = data.replace(to_replace='?',value = np.nan)
#用np.nan替换掉数据中的? 丢弃数据中数据缺失的行
data = data.replace('?',np.nan)
data = data.dropna(how ='any')
#data.shape
#print(data)
#print(data.shape)
#print(data[column_names[10]])
#将数据切分成1:3,分别做测试及训练集
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())
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)lr = LogisticRegression()
sgdc =SGDClassifier()
lr.fit(X_train,y_train)
lr_y_predict = lr.predict(X_test)
print(lr_y_predict)
sgdc.fit(X_train,y_train)
sgdc_y_predict = sgdc.predict(X_test)
print(sgdc_y_predict)print('Accuracy of LR Classifier:', lr.score(X_test,y_test))
print(classification_report(y_test,lr_y_predict,target_names=['Benign','Malignant']))print('Accuracy of SGD Classifier:',sgdc.score(X_test,y_test))
print(classification_report(y_test,sgdc_y_predict,target_names=['Bengin','Malignant']))
这篇关于通俗读完《Python机器学习及实战:从零开始通往Kaggle竞赛之路》的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!