# 各算法决策边界的画法

## 未对难区分故障进行增加

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from  matplotlib.colors import ListedColormap
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_moons,make_circles,make_classification
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis as QDA
from sklearn import svm
import matplotlib as mpl
from sklearn import tree
from sklearn import neighbors
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import BaggingClassifier
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
from sklearn.model_selection import train_test_split,cross_val_score
from sklearn.metrics import accuracy_score,roc_curve,auc
from sklearn.ensemble import VotingClassifier

np.random.seed(555)

estimators0 = [("DecisionTree", tree.DecisionTreeClassifier()),
("SVC", SVC(kernel='rbf', gamma=1.0,decision_function_shape='ovo',probability=True)),#RBF支持向量机
("KNN",neighbors.KNeighborsClassifier(n_neighbors = 10, weights='distance')),
("RF",RandomForestClassifier(n_estimators=50,
max_depth=None,
min_samples_split=2,
oob_score=True,
random_state=0)),
("GNB",GaussianNB())]

X2=df.iloc[:,4:6]   #比值系数
#X2=df.iloc[:,6:8]   #比值系数
X3=df.iloc[:,8:11]  #单位向量
y1=df['guzhang']
XD=[]
yD=[]
datasets =[]
for i in range(len(y1)):
XX=X2[i:i+1]
yy=y1[i:i+1]
XXX = np.tile(np.array(XX),(30,1)) + abs(np.tile(np.array(XX),(30,1)))*0.1*np.random.randn(1*30,2)
yyy = np.tile(np.array(yy),(30))
XD.append(XXX)
yD.append(yyy)
data=(XXX,yyy)
datasets.append(data)

XXXX=np.r_[XD[0],XD[1],XD[2],XD[3],XD[4],XD[5],XD[6],XD[7],XD[8],XD[9],XD[10],XD[11],XD[12]]
yyyy=np.r_[yD[0],yD[1],yD[2],yD[3],yD[4],yD[5],yD[6],yD[7],yD[8],yD[9],yD[10],yD[11],yD[12]]

XD1=[]
yD1=[]
datasets1 =[]
for i in range(len(y1)):
XX=X2[i:i+1]
yy=y1[i:i+1]
XXX = np.tile(np.array(XX),(30,1)) + abs(np.tile(np.array(XX),(30,1)))*0.1*np.random.randn(1*30,2)
yyy = np.tile(np.array(yy),(30))
XD1.append(XXX)
yD1.append(yyy)
data1=(XXX,yyy)
datasets1.append(data1)

XXXX1=np.r_[XD1[0],XD1[1],XD1[2],XD1[3],XD1[4],XD1[5],XD1[6],XD1[7],XD1[8],XD1[9],XD1[10],XD1[11],XD1[12]]
yyyy1=np.r_[yD1[0],yD1[1],yD1[2],yD1[3],yD1[4],yD1[5],yD1[6],yD1[7],yD1[8],yD1[9],yD1[10],yD1[11],yD1[12]]

plt.figure(figsize=(15,9))
'#800080','#556B2F','#008000','#FF0000','#32CD32','#48D1CC']
marker = ['.','8','x','+','v','*','D','.','.','^','x','2','.']
label = ['故障1','故障2','故障3','故障4','故障5','故障6','故障7'
,'故障8','故障9','故障10','故障11','故障12','故障13']
for i in range(len(y1)):
X,y= datasets[i]
plt.scatter(X[:,0],X[:,1],color=colvalue[i], marker=marker[i],label=label[i])
import matplotlib.font_manager as fm
myfont=fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')
plt.legend(prop=myfont,loc='best',fontsize=25)
plt.title(U'比值系数故障数据散点图',fontsize=25,fontproperties=myfont)
plt.show()

print(len(datasets))
#X_train, X_test, y_train, y_test = train_test_split(XXXX, yyyy, test_size=0.4)
x_min,x_max = XXXX[:,0].min()-10,XXXX[:,0].max()+20
y_min,y_max = XXXX[:,1].min()-2,XXXX[:,1].max()+2
h = 0.2
xx,yy = np.meshgrid(np.arange(x_min,x_max,h),np.arange(y_min,y_max,h))
cm=plt.cm.RdBu# 绘图库中的颜色查找表。比如A1是红色,A2是浅蓝色。 这样一种映射关系
'#800080','#556B2F','#008000','#FF0000','#32CD32','#48D1CC'])

figure = plt.figure(figsize=(20,12))
BS= []#比值系数
for n,(name, clf) in enumerate(estimators0):

ax = plt.subplot(2,3,n+1)
clf.fit(XXXX,yyyy)
score=clf.score(XXXX1,yyyy1)

y_pre=clf.predict(XXXX1)
obj1 = confusion_matrix(yyyy1, y_pre)
print("\n")
print("{}的混淆矩阵为：\n{}".format(name, obj1))
print("\n")
BS.append(np.around(score, decimals=4))
Z =clf.predict(np.c_[xx.ravel(),yy.ravel()])
Z=Z.reshape(xx.shape)
ax.contourf(xx,yy,Z,camp = cm ,alpha=0.8)
ax.scatter(XXXX[:,0],XXXX[:,1],c=yyyy,cmap=cm_bright)
ax.scatter(XXXX1[:,0],XXXX1[:,1],c=yyyy1,cmap=cm_bright,alpha=0.6)
ax.set_title(name)
ax.text(xx.max()-0.3,yy.min()+0.3,('%.2f' % score).lstrip('0'),size=15,horizontalalignment='right')

print(BS)
p=[]
for i in BS:
b=np.log(i/(1-i))
p.append(np.around(b, decimals=5))
QZb=p/sum(p)
print(QZb)
estimators1 = [("软投票",VotingClassifier(estimators = estimators0,voting = 'soft',weights=QZb)),
("硬投票",VotingClassifier(estimators = estimators0,voting = 'hard',weights=QZb))]
import matplotlib.font_manager as fm
myfont=fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')
figure = plt.figure(figsize=(20,8))
for n,(name, clf) in enumerate(estimators1):
ax = plt.subplot(1,2,n+1)
clf.fit(XXXX,yyyy)
score=clf.score(XXXX1,yyyy1)

y_pre=clf.predict(XXXX1)
obj1 = confusion_matrix(yyyy1, y_pre)
print("\n")
print("{}的混淆矩阵为：\n{}".format(name, obj1))
print("\n")

Z =clf.predict(np.c_[xx.ravel(),yy.ravel()])
Z=Z.reshape(xx.shape)
ax.contourf(xx,yy,Z,camp = cm ,alpha=0.8)
ax.scatter(XXXX[:,0],XXXX[:,1],c=yyyy,cmap=cm_bright)
ax.scatter(XXXX1[:,0],XXXX1[:,1],c=yyyy1,cmap=cm_bright,alpha=0.6)
ax.set_title(name,fontproperties=myfont)
ax.text(xx.max()-0.3,yy.min()+0.3,('%.2f' % score).lstrip('0'),size=15,horizontalalignment='right')


## 增加难以区分数据的数据量

nqf = [5,10,11,12]
for i  in nqf:
XX=X2[i-1:i]
yy=y1[i-1:i]
XXX = np.tile(np.array(XX),(100,1)) + abs(np.tile(np.array(XX),(100,1)))*0.1*np.random.randn(1*100,2)
yyy = np.tile(np.array(yy),(100))
XD1.append(XXX)
yD1.append(yyy)

XXXX1=np.r_[XD1[0],XD1[1],XD1[2],XD1[3],XD1[4],XD1[5],XD1[6],XD1[7],XD1[8],XD1[9],XD1[10],XD1[11],XD1[12],XD1[13],XD1[14],XD1[15],XD1[16]]
yyyy1=np.r_[yD1[0],yD1[1],yD1[2],yD1[3],yD1[4],yD1[5],yD1[6],yD1[7],yD1[8],yD1[9],yD1[10],yD1[11],yD1[12],yD1[13],yD1[14],yD1[15],yD1[16]]


## 完整代码如下

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from  matplotlib.colors import ListedColormap
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_moons,make_circles,make_classification
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis as QDA
from sklearn import svm
import matplotlib as mpl
from sklearn import tree
from sklearn import neighbors
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import BaggingClassifier
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
from sklearn.model_selection import train_test_split,cross_val_score
from sklearn.metrics import accuracy_score,roc_curve,auc
from sklearn.ensemble import VotingClassifier

np.random.seed(555)

estimators0 = [("DecisionTree", tree.DecisionTreeClassifier()),
("SVC", SVC(kernel='rbf', gamma=1.0,decision_function_shape='ovo',probability=True)),#RBF支持向量机
("KNN",neighbors.KNeighborsClassifier(n_neighbors = 10, weights='distance')),
("RF",RandomForestClassifier(n_estimators=50,
max_depth=None,
min_samples_split=2,
oob_score=True,
random_state=0)),
("GNB",GaussianNB())]

X2=df.iloc[:,4:6]   #比值系数
#X2=df.iloc[:,6:8]   #比值系数
X3=df.iloc[:,8:11]  #单位向量
y1=df['guzhang']
XD=[]
yD=[]
datasets =[]
for i in range(len(y1)):
XX=X2[i:i+1]
yy=y1[i:i+1]
XXX = np.tile(np.array(XX),(30,1)) + abs(np.tile(np.array(XX),(30,1)))*0.1*np.random.randn(1*30,2)
yyy = np.tile(np.array(yy),(30))
XD.append(XXX)
yD.append(yyy)
data=(XXX,yyy)
datasets.append(data)

XXXX=np.r_[XD[0],XD[1],XD[2],XD[3],XD[4],XD[5],XD[6],XD[7],XD[8],XD[9],XD[10],XD[11],XD[12]]
yyyy=np.r_[yD[0],yD[1],yD[2],yD[3],yD[4],yD[5],yD[6],yD[7],yD[8],yD[9],yD[10],yD[11],yD[12]]

XD1=[]
yD1=[]
datasets1 =[]
for i in range(len(y1)):
XX=X2[i:i+1]
yy=y1[i:i+1]
XXX = np.tile(np.array(XX),(30,1)) + abs(np.tile(np.array(XX),(30,1)))*0.1*np.random.randn(1*30,2)
yyy = np.tile(np.array(yy),(30))
XD1.append(XXX)
yD1.append(yyy)
data1=(XXX,yyy)
datasets1.append(data1)

nqf = [5,10,11,12]
for i  in nqf:
XX=X2[i-1:i]
yy=y1[i-1:i]
XXX = np.tile(np.array(XX),(100,1)) + abs(np.tile(np.array(XX),(100,1)))*0.1*np.random.randn(1*100,2)
yyy = np.tile(np.array(yy),(100))
XD1.append(XXX)
yD1.append(yyy)

XXXX1=np.r_[XD1[0],XD1[1],XD1[2],XD1[3],XD1[4],XD1[5],XD1[6],XD1[7],XD1[8],XD1[9],XD1[10],XD1[11],XD1[12],XD1[13],XD1[14],XD1[15],XD1[16]]
yyyy1=np.r_[yD1[0],yD1[1],yD1[2],yD1[3],yD1[4],yD1[5],yD1[6],yD1[7],yD1[8],yD1[9],yD1[10],yD1[11],yD1[12],yD1[13],yD1[14],yD1[15],yD1[16]]

plt.figure(figsize=(15,9))
'#800080','#556B2F','#008000','#FF0000','#32CD32','#48D1CC']
marker = ['.','8','x','+','v','*','D','.','.','^','x','2','.']
label = ['故障1','故障2','故障3','故障4','故障5','故障6','故障7'
,'故障8','故障9','故障10','故障11','故障12','故障13']
for i in range(len(y1)):
X,y= datasets[i]
plt.scatter(X[:,0],X[:,1],color=colvalue[i], marker=marker[i],label=label[i])
import matplotlib.font_manager as fm
myfont=fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')
plt.legend(prop=myfont,loc='best',fontsize=25)
plt.title(U'比值系数故障数据散点图',fontsize=25,fontproperties=myfont)
plt.show()

print(len(datasets))
#X_train, X_test, y_train, y_test = train_test_split(XXXX, yyyy, test_size=0.4)
x_min,x_max = XXXX[:,0].min()-10,XXXX[:,0].max()+20
y_min,y_max = XXXX[:,1].min()-2,XXXX[:,1].max()+2
h = 0.2
xx,yy = np.meshgrid(np.arange(x_min,x_max,h),np.arange(y_min,y_max,h))
cm=plt.cm.RdBu# 绘图库中的颜色查找表。比如A1是红色,A2是浅蓝色。 这样一种映射关系
'#800080','#556B2F','#008000','#FF0000','#32CD32','#48D1CC'])

figure = plt.figure(figsize=(20,12))
BS= []#比值系数
for n,(name, clf) in enumerate(estimators0):

ax = plt.subplot(2,3,n+1)
clf.fit(XXXX,yyyy)
score=clf.score(XXXX1,yyyy1)

y_pre=clf.predict(XXXX1)
obj1 = confusion_matrix(yyyy1, y_pre)
print("\n")
print("{}的混淆矩阵为：\n{}".format(name, obj1))
print("\n")
BS.append(np.around(score, decimals=4))
Z =clf.predict(np.c_[xx.ravel(),yy.ravel()])
Z=Z.reshape(xx.shape)
ax.contourf(xx,yy,Z,camp = cm ,alpha=0.8)
ax.scatter(XXXX[:,0],XXXX[:,1],c=yyyy,cmap=cm_bright)
ax.scatter(XXXX1[:,0],XXXX1[:,1],c=yyyy1,cmap=cm_bright,alpha=0.6)
ax.set_title(name)
ax.text(xx.max()-0.3,yy.min()+0.3,('%.2f' % score).lstrip('0'),size=15,horizontalalignment='right')

print(BS)
p=[]
for i in BS:
b=np.log(i/(1-i))
p.append(np.around(b, decimals=5))
QZb=p/sum(p)
print(QZb)
estimators1 = [("软投票",VotingClassifier(estimators = estimators0,voting = 'soft',weights=QZb)),
("硬投票",VotingClassifier(estimators = estimators0,voting = 'hard',weights=QZb))]
import matplotlib.font_manager as fm
myfont=fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')
figure = plt.figure(figsize=(20,8))
for n,(name, clf) in enumerate(estimators1):
ax = plt.subplot(1,2,n+1)
clf.fit(XXXX,yyyy)
score=clf.score(XXXX1,yyyy1)

y_pre=clf.predict(XXXX1)
obj1 = confusion_matrix(yyyy1, y_pre)
print("\n")
print("{}的混淆矩阵为：\n{}".format(name, obj1))
print("\n")

Z =clf.predict(np.c_[xx.ravel(),yy.ravel()])
Z=Z.reshape(xx.shape)
ax.contourf(xx,yy,Z,camp = cm ,alpha=0.8)
ax.scatter(XXXX[:,0],XXXX[:,1],c=yyyy,cmap=cm_bright)
ax.scatter(XXXX1[:,0],XXXX1[:,1],c=yyyy1,cmap=cm_bright,alpha=0.6)
ax.set_title(name,fontproperties=myfont)
ax.text(xx.max()-0.3,yy.min()+0.3,('%.2f' % score).lstrip('0'),size=15,horizontalalignment='right')



11-07

12-23 151

10-31 3843

04-23 1万+

10-31 56

03-24 3054

03-27 583

12-13 245

05-23 16

10-18 509

08-25 17

04-14 61万+

03-13 15万+

03-12 2629

02-28 1万+

03-01 14万+

03-08 1万+

03-08 8万+

03-10 13万+

03-10 19万+

03-12 12万+

03-13 12万+

03-13 1万+

03-18 4万+

03-18 1万+

03-19 6万+

03-19 9万+

03-19 4万+

03-20 1万+

03-22 5万+

#### 男生更看重女生的身材脸蛋，还是思想？

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客