文档章节

sklearn常见分类器(二分类模板)

o
 osc_wws45aot
发布于 2019/08/20 21:37
字数 457
阅读 17
收藏 0

精选30+云产品,助力企业轻松上云!>>>

# -*- coding: utf-8 -*-
import pandas as pd
import matplotlib
matplotlib.rcParams['font.sans-serif']=[u'simHei']
matplotlib.rcParams['axes.unicode_minus']=False
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_breast_cancer

data_set = pd.read_csv('pima-indians-diabetes.csv')
data = data_set.values[:,:]

y = data[:,8]
X = data[:,:8]
X_train,X_test,y_train,y_test = train_test_split(X,y)

### 随机森林
print("==========================================")   
RF = RandomForestClassifier(n_estimators=10,random_state=11)
RF.fit(X_train,y_train)
predictions = RF.predict(X_test)
print("RF")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))


### Logistic Regression Classifier 
print("==========================================")      
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(penalty='l2')
clf.fit(X_train,y_train)
predictions = clf.predict(X_test)
print("LR")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))
 
 
### Decision Tree Classifier    
print("==========================================")   
from sklearn import tree
clf = tree.DecisionTreeClassifier()
clf.fit(X_train,y_train)
predictions = clf.predict(X_test)
print("DT")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))

 
### GBDT(Gradient Boosting Decision Tree) Classifier    
print("==========================================")   
from sklearn.ensemble import GradientBoostingClassifier
clf = GradientBoostingClassifier(n_estimators=200)
clf.fit(X_train,y_train)
predictions = clf.predict(X_test)
print("GBDT")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))

 
###AdaBoost Classifier
print("==========================================")   
from sklearn.ensemble import  AdaBoostClassifier
clf = AdaBoostClassifier()
clf.fit(X_train,y_train)
predictions = clf.predict(X_test)
print("AdaBoost")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))

 
### GaussianNB
print("==========================================")   
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(X_train,y_train)
predictions = clf.predict(X_test)
print("GaussianNB")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))

 
### Linear Discriminant Analysis
print("==========================================")   
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
clf = LinearDiscriminantAnalysis()
clf.fit(X_train,y_train)
predictions = clf.predict(X_test)
print("Linear Discriminant Analysis")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))

 
### Quadratic Discriminant Analysis
print("==========================================")   
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
clf = QuadraticDiscriminantAnalysis()
clf.fit(X_train,y_train)
predictions = clf.predict(X_test)
print("Quadratic Discriminant Analysis")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))


### SVM Classifier 
print("==========================================")   
from sklearn.svm import SVC
clf = SVC(kernel='rbf', probability=True)
clf.fit(X_train,y_train)
predictions = clf.predict(X_test)
print("SVM")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))


### Multinomial Naive Bayes Classifier
print("==========================================")       
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB(alpha=0.01)
clf.fit(X_train,y_train)
predictions = clf.predict(X_test)
print("Multinomial Naive Bayes")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))


### xgboost
import xgboost
print("==========================================")       
from sklearn.naive_bayes import MultinomialNB
clf = xgboost.XGBClassifier()
clf.fit(X_train,y_train)
predictions = clf.predict(X_test)
print("xgboost")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))


### voting_classify
from sklearn.ensemble import GradientBoostingClassifier, VotingClassifier, RandomForestClassifier
import xgboost
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
clf1 = GradientBoostingClassifier(n_estimators=200)
clf2 = RandomForestClassifier(random_state=0, n_estimators=500)
# clf3 = LogisticRegression(random_state=1)
# clf4 = GaussianNB()
clf5 = xgboost.XGBClassifier()
clf = VotingClassifier(estimators=[
    # ('gbdt',clf1),
    ('rf',clf2),
    # ('lr',clf3),
    # ('nb',clf4),
    # ('xgboost',clf5),
    ],
    voting='soft')
clf.fit(X_train,y_train)
predictions = clf.predict(X_test)
print("voting_classify")
print(classification_report(y_test,predictions))
print("AC",accuracy_score(y_test,predictions))

 

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。
机器学习系列(二十八)——sklearn中的Logistic回归与Logistic回归解决多分类

Logistic回归正则化 sklearn中的Logistic回归是加入了正则化的,在sklearn中,加入了正则项的损失函数表达式为: 其中L是超参数,可以指定使用L1正则还是L2正则。与我们在多项式回归中的正则...

Ice_spring
2019/07/22
0
0
MNIST手写数字识别

写在开头的话 这不是我原创的,我只是在《sklearn与tensorflow机器学习实用指南》第三章的基础上做一些总结。之所以打上原创标签,是因为如果我不打,那系统就要求我填入“正确的原文链接”。...

南七技校
2019/09/10
332
0
sklearn 学习 第一篇:分类

分类属于监督学习算法,是指根据已有的数据和标签(分类)进行学习,预测未知数据的标签。分类问题的目标是预测数据的类别标签(class label),可以把分类问题划分为二分类和多分类问题。二...

osc_arhujamz
2019/07/28
1
0
Sklearn分类树在合成数集上的表现

  小伙伴们大家好~o( ̄▽ ̄)ブ,今天我们开始来看一下Sklearn分类树的表现,我的开发环境是Jupyter lab,所用的库和版本大家参考:   Python 3.7.1(你的版本至少要3.4以上)   Sciki...

osc_k11w4vu5
2019/05/06
2
0
sklearn ROC曲线使用

目录 这篇文章中我将使用sklearn的ROC曲线官方示例代码进行讲解,当然主要目的还是在于记录,好记性不如烂键盘嘛。 ROC曲线定义 ROC曲线是Receiver Operating Characteristic Curve的简称,中...

hfutdog
03/31
0
0

没有更多内容

加载失败,请刷新页面

加载更多

dict.items()和dict.iteritems()有什么区别?

问题: Are there any applicable differences between dict.items() and dict.iteritems() ? dict.items()和dict.iteritems()之间是否有适用的区别? From the Python docs: 从Python文档中......

法国红酒甜
34分钟前
20
0
R中“ =”和“ <-”赋值运算符有什么区别?

问题: What are the differences between the assignment operators = and <- in R? R中赋值运算符=和<-之间有什么区别? I know that operators are slightly different, as this example ......

fyin1314
今天
20
0
之间的区别 和

问题: I'm learning Spring 3 and I don't seem to grasp the functionality behind <context:annotation-config> and <context:component-scan> . 我正在学习Spring 3,并且似乎不太了解<......

javail
今天
15
0
业内首款,百度工业视觉智能平台全新亮相

本文作者:y****n 业内首款全国产化工业视觉智能平台——百度工业视觉智能平台亮相中国机器视觉展(Vision China),该平台所具有的核心AI能力完全自主可控,在质检、巡检等场景中具有高效、...

百度开发者中心
昨天
7
0
我们如何制作xkcd样式图? - How can we make xkcd style graphs?

问题: Apparently, folk have figured out how to make xkcd style graphs in Mathematica and in LaTeX . 显然,民间已经想出了如何在Mathematica和LaTeX中制作xkcd风格的图形。 Can we d......

富含淀粉
今天
10
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部