import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn import neighbors
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.datasets import make_gaussian_quantiles
def draw(x_data,y_data,model):
x_min,x_max=x_data[:,0].min()-1,x_data[:,0].max()+1
y_min,y_max=x_data[:,1].min()-1,x_data[:,1].max()+1
xx,yy = np.meshgrid(np.arange(x_min,x_max,0.02),np.arange(y_min,y_max,0.02))
z = model.predict(np.c_[xx.ravel(),yy.ravel()])
z = z.reshape(xx.shape)
cs = plt.contourf(xx,yy,z)
plt.scatter(x_data[:,0],x_data[:,1],c=y_data)
plt.show()
def set_knn(x_data,y_data):#knn 模型
knn = neighbors.KNeighborsClassifier()
knn.fit(x_data,y_data)
return knn
def set_tree(x_data,y_data):#决策树 模型
Tree = tree.DecisionTreeClassifier()
Tree.fit(x_data,y_data)
return Tree
def bigg_model(model,x_data,y_data,n):#Bagging 模型
#建立若干个model模型,之后组合(按照概率)起来的模型
Bagging=BaggingClassifier(model,n_estimators = n)
Bagging.fit(x_data,y_data)
return Bagging
def set_random_forest(x_data,y_data,n):#随机森林 模型
forest = RandomForestClassifier(n_estimators = n)
forest.fit(x_data,y_data)
return forest
def set_adaboost(x_data,y_data,n):#adaboost 模型
adaboost = AdaBoostClassifier(tree.DecisionTreeClassifier(max_depth=3),n_estimators = n)
adaboost.fit(x_data,y_data)
return adaboost
def main():
data_way = 0
if data_way==1:
print ("----------ing-------------")
data = np.genfromtxt("testSet2.txt",delimiter=",")
x_data= data[:,:-1]
y_data= data[:,-1]
x_train,x_test,y_train,y_test=train_test_split(x_data,y_data,test_size=0.3)
else:
x1,y1 = make_gaussian_quantiles(n_samples=500,n_features=2,n_classes=2)
x2,y2 = make_gaussian_quantiles(mean=(3,3),n_samples=500,n_features=2,n_classes=2)
x_data =np.concatenate((x1,x2))
y_data=np.concatenate((y1,-y2+1))
x_train,x_test,y_train,y_test=train_test_split(x_data,y_data,test_size=0.3)
tree_model = set_tree(x_train,y_train)
score=tree_model.score(x_test,y_test)
print("score=",score)
draw(x_data,y_data,tree_model)
forest_model = set_random_forest(x_train,y_train,50)
score=forest_model.score(x_test,y_test)
print("score=",score)
draw(x_data,y_data,forest_model)
adaboost_model = set_adaboost(x_data,y_data,10)
score=adaboost_model.score(x_test,y_test)
print("score=",score)
draw(x_data,y_data,adaboost_model)
print ("----------end-------------")
main()
三次分别是:决策树,随机森林,adaboost