Sklearn各分类算法实现

本文通过实战案例,详细介绍了逻辑回归、支持向量机、朴素贝叶斯、K近邻、决策树、随机森林、GBDT及XGBoost等八种机器学习分类算法的应用,涵盖癌症患者分类、手写数字识别、新闻分类、鸢尾花分类、泰坦尼克号生存预测等多个场景。
摘要由CSDN通过智能技术生成

1、逻辑回归

使用逻辑回归来实现对癌症患者的分类:

import pandas as pd
import numpy as np

column_name = ['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_name)
#将?替换为标准缺失值表示
data = data.replace(to_replace='?',value=np.nan)
#丢弃带有缺失值的数据
data = data.dropna(how='any')
from sklearn.cross_validation import train_test_split
X_train,X_test,y_train,y_test = train_test_split(data[column_name[1:10]],data[column_name[10]],test_size=0.25,random_state=33)

from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
#标准化数据,保证每个维度的特征数据方差为1,均值为0,使得预测结果不会被某些维度过大的特征值而主导
ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)
#初始化LogisticRegression和SgdClassifier
lr = LogisticRegression()
sgdc = SGDClassifier()

#调用LogisticRegression的fit函数来训练模型,并使用predict函数进行预测
lr.fit(X_train,y_train)
lr_y_predict = lr.predict(X_test)
#调用SGDClassifier中的fit函数来训练模型,并用predict函数来进行预测
sgdc.fit(X_train,y_train)
sgdc_y_predict = sgdc.predict(X_test)

from sklearn.metrics import classification_report
#利用score方法计算准确性
print('Accruacy of LR Classifier:',lr.score(X_test,y_test))
#利用classification_report模块获得召回率,精确率和F1值三个指标
print(classification_report(y_test,lr_y_predict,target_names=['Benign','Malignant']))

print('Accruacy of SGD Classifier',sgdc.score(X_test,y_test))
print(classification_report(y_test,sgdc_y_predict,target_names=['Benign','Maligant']))

 

2、支持向量机

本节使用支持向量机实现对手写数字的分类

from sklearn.datasets import load_digits
digits= load_digits()
from sklearn.cross_validation import train_test_split
X_train,X_test,y_train,y_test = train_test_split(digits.data,digits.target,test_size=0.25,random_state=33)

from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC

ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)

lsvc = LinearSVC()
lsvc.fit(X_train,y_train)
y_predict = lsvc.predict(X_test)

from sklearn.metrics import classification_report
print('The Accruacy of Linear SVC is',lsvc.score(X_test,y_test))
print (classification_report(y_test,y_predict,target_names=digits.target_names.astype(str)))

 

3、朴素贝叶斯

本节使用朴素贝叶斯实现对20类新闻的分类

from sklearn.datasets import fetch_20newsgroups
news = fetch_20newsgroups(subset='all')
print(len(news.data))
print(news.data[0])

from sklearn.cross_validation import train_test_split
X_train,X_test,y_train,y_test = train_test_split(news.data,news.target,test_size=0.25,random_state=33)
#文本特征向量转化模块
from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer()
X_train = vec.fit_transform(X_train)
X_test = vec.transform(X_test)

from sklearn.naive_bayes import MultinomialNB
mnb = MultinomialNB()
mnb.fit(X_train,y_train)
y_predict = mnb.predict(X_test)
from sklearn.metrics import classification_report
print('The accruacy of Naive Bayes is ',mnb.score(X_test,y_test))
print(classification_report(y_test,y_predict,target_names=news.target_names))

 

4、K近邻

本文使用K近邻算法实现对鸢尾花数据的分类

from sklearn.datasets import load_iris
iris=load_iris()
from sklearn.cross_validation import train_test_split
X_train,X_test,y_train,y_test = train_test_split(iris.data,iris.target,test_size=0.25,random_state=33)
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)

knc = KNeighborsClassifier()
knc.fit(X_train,y_train)
y_predict = knc.predict(X_test)

print('The Accruacy of kNN is',knc.score(X_test,y_test))
from sklearn.metrics import classification_report
print(classification_report(y_test,y_predict,target_names=iris.target_names))

 

5、决策树

本文使用决策树对泰坦尼克号乘客能否逃生进行分类:

titanic = pd.read_csv('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt')

X = titanic[['pclass','age','sex']]
y = titanic['survived']
X['age'].fillna(X['age'].mean(),inplace=True)

from sklearn.cross_validation import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=33)
#将非数值型数据转换为数值型数据
from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer(sparse=False)
X_train = vec.fit_transform(X_train.to_dict(orient='record'))
print(vec.feature_names_)
X_test = vec.transform(X_test.to_dict(orient='record'))

from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier()
dtc.fit(X_train,y_train)
y_predict = dtc.predict(X_test)
from sklearn.metrics import classification_report
print (dtc.score(X_test,y_test))
print(classification_report(y_predict,y_test,target_names=['died','survivied']))

 

6、集成模型-随机森林和GBDT

仍然使用上节的泰坦尼克号数据:

titanic = pd.read_csv('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt')

X = titanic[['pclass','age','sex']]
y = titanic['survived']
X['age'].fillna(X['age'].mean(),inplace=True)

from sklearn.cross_validation import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=33)
#将非数值型数据转换为数值型数据
from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer(sparse=False)
X_train = vec.fit_transform(X_train.to_dict(orient='record'))
print(vec.feature_names_)
X_test = vec.transform(X_test.to_dict(orient='record'))

from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier()
rfc.fit(X_train,y_train)
rfc_y_pred = rfc.predict(X_test)
from sklearn.ensemble import GradientBoostingClassifier
gbc = GradientBoostingClassifier()
gbc.fit(X_train,y_train)
gbc_y_pred = gbc.predict(X_test)

from sklearn.metrics import classification_report
print('The accuracy of randomforest is ',rfc.score(X_test,y_test))
print(classification_report(rfc_y_pred,y_test))
print('The accuracy of gbdt is',gbc.score(X_test,y_test))
print(classification_report(gbc_y_pred,y_test))

 

7、XGBoost

titanic = pd.read_csv('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt')
titanic.head()
titanic.info()
X = titanic[['pclass','age','sex']]
y = titanic['survived']
X['age'].fillna(X['age'].mean(),inplace=True)
X.info()
from sklearn.cross_validation import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=33)
#将非数值型数据转换为数值型数据
from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer(sparse=False)
X_train = vec.fit_transform(X_train.to_dict(orient='record'))
print(vec.feature_names_)
X_test = vec.transform(X_test.to_dict(orient='record'))
X_train[:2]

from xgboost import XGBClassifier
xgbc = XGBClassifier()
xgbc.fit(X_train,y_train)
print("The accuracy of xgboost is" ,xgbc.score(X_test,y_test))

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值