机器学习事例程序员Python 运维

Sklearn各分类算法实现

2017-10-04  本文已影响299人  文哥的学习日记

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

有关xgboost在MAC上的安装,参考简书:http://www.jianshu.com/p/4cfa41ccf022

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))
上一篇 下一篇

猜你喜欢

热点阅读