import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import Perceptron
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LogisticRegressionCV
from sklearn.linear_model import Perceptron
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import train_test_split
from sklearn import svm
#from sklearn.cluster import KMeans
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
#df=pd.read_csv('https://raw.githubusercontent.com/susanli2016/Machine-Learning-with-Python/master/diabetes.csv')
df=pd.read_csv('diabetes.csv')
#print(df.head())
X=df.loc[:,df.columns !='Outcome']
y=df['Outcome']
from sklearn.preprocessing import StandardScaler
std = StandardScaler()
std.fit(X)
X_std = std.transform(X)
#进行数据分割,测试数据占20%
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_std, y,test_size=0.25, random_state= 0)
#X_train, X_test, y_train, y_test = train_test_split(X_std, y, stratify=y, random_state=66)
models={
"MLPClassifier":MLPClassifier(max_iter=500),#神经网络
"LogisticRegression": LogisticRegression(solver='lbfgs', multi_class='multinomial',C=1000.0),#逻辑回归算法
"DecisionTreeClassifier":DecisionTreeClassifier(),#决策树分析
"svm":svm.SVC(),#支持向量机
"GaussianNB":GaussianNB(), #朴素贝叶斯
"LogisticRegressionCV":LogisticRegressionCV(Cs=np.logspace(-4,1,50), cv=3,fit_intercept=True, penalty='l2', solver='lbfgs',tol=0.01, multi_class='multinomial'),#逻辑回归算法
"KNeighborsClassifier":KNeighborsClassifier(n_neighbors=9),#K近邻法
"Perceptron":Perceptron(tol=1e-6),#感知机
# "KMeans":KMeans(),
"RandomForestClassifier":RandomForestClassifier(n_estimators=100, random_state=0),#随机森林分类
"GradientBoostingClassifier":GradientBoostingClassifier(random_state=0),#集成学习梯度提升决策树
}
for name,clf in models.items():
clf.fit(X_train, y_train)
#print(name+ 'train accuracy: %.3f' % clf.score(X_train, y_train))
print(name+ 'test accuracy: %.3f' % clf.score(X_test, y_test))
#score=cross_validation.cross_val_score(model,x,y,cv=5).mean()
#print(name,score)
MLPClassifiertest accuracy: 0.807
LogisticRegressiontest accuracy: 0.802
DecisionTreeClassifiertest accuracy: 0.719
svmtest accuracy: 0.776
GaussianNBtest accuracy: 0.766
LogisticRegressionCVtest accuracy: 0.755
KNeighborsClassifiertest accuracy: 0.781
Perceptrontest accuracy: 0.776
RandomForestClassifiertest accuracy: 0.771
GradientBoostingClassifiertest accuracy: 0.818
https://www.cnblogs.com/jlutiger/p/8931293.html