import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn import model_selection
from sklearn.model_selection import train_test_split
from sklearn import neighbors
from sklearn.ensemble import BaggingClassifier
from sklearn.linear_model import LogisticRegression
from mlxtend.classifier import StackingClassifier
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()
#predictions = model.predict(x_data)
#return (classification_report(predictions,y_data))
def set_knn(x_data,y_data):
knn = neighbors.KNeighborsClassifier(n_neighbors=1)
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 set_logistic(x_data,y_data):
logistic = LogisticRegression()
logistic.fit(x_data,y_data)
return logistic
def set_stack(x_data,y_data):
knn = neighbors.KNeighborsClassifier(n_neighbors=1)
Tree = tree.DecisionTreeClassifier()
logistic = LogisticRegression()
lr = LogisticRegression()
stack = StackingClassifier(classifiers=[knn,Tree,logistic],meta_classifier=lr)
stack.fit(x_data,y_data)
return stack
def main():
print ("----------ing-------------")
iris_data = load_iris()
x_data= iris_data.data[:,1:3]
y_data= iris_data.target
knn = neighbors.KNeighborsClassifier(n_neighbors=1)
Tree = tree.DecisionTreeClassifier()
logistic = LogisticRegression()
lr = LogisticRegression()
stack = StackingClassifier(classifiers=[knn,Tree,logistic],meta_classifier=lr)
for model,label in zip([knn,Tree,logistic,stack],["knn","decision tree","logisticregression","stackingclassifier"]) :
scores = model_selection.cross_val_score(model,x_data,y_data,cv=3,scoring='accuracy')
print("Accuracy:%0.3f,%s"%(scores.mean(),label))
print ("----------end-------------")
main()
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn import model_selection
from sklearn.model_selection import train_test_split
from sklearn import neighbors
from sklearn.ensemble import BaggingClassifier
from sklearn.linear_model import LogisticRegression
from mlxtend.classifier import StackingClassifier
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()
#predictions = model.predict(x_data)
#return (classification_report(predictions,y_data))
def set_knn(x_data,y_data):
knn = neighbors.KNeighborsClassifier(n_neighbors=1)
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 set_logistic(x_data,y_data):
logistic = LogisticRegression()
logistic.fit(x_data,y_data)
return logistic
def set_stack(x_data,y_data):
knn = neighbors.KNeighborsClassifier(n_neighbors=1)
Tree = tree.DecisionTreeClassifier()
logistic = LogisticRegression()
lr = LogisticRegression()
stack = StackingClassifier(classifiers=[knn,Tree,logistic],meta_classifier=lr)
stack.fit(x_data,y_data)
return stack
def main():
print ("----------ing-------------")
iris_data = load_iris()
x_data= iris_data.data[:,1:3]
y_data= iris_data.target
x_train,x_test,y_train,y_test=train_test_split(x_data,y_data)
knn_model=set_knn(x_train,y_train)
knn_score=knn_model.score(x_test,y_test)
print("knn_model score=",knn_score)
draw(x_data,y_data,knn_model)
tree_model=set_tree(x_train,y_train)
tree_score=tree_model.score(x_test,y_test)
print("tree_model score=",tree_score)
draw(x_data,y_data,tree_model)
logistic_model=set_logistic(x_train,y_train)
logistic_score=logistic_model.score(x_test,y_test)
print("logistic_model score=",logistic_score)
draw(x_data,y_data,logistic_model)
stack_model=set_stack(x_train,y_train)
stack_score=stack_model.score(x_test,y_test)
print("stack_model score=",stack_score)
draw(x_data,y_data,stack_model)
print ("----------end-------------")
main()