第5次

from sklearn.datasets import load_wine
wine_dataset = load_wine()

from sklearn import preprocessing
x=wine_dataset.data

x = preprocessing.scale(x)(最后一个去掉注释)

y=wine_dataset.target

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(x,y,test_size=0.20,random_state=30,shuffle=True)

from sklearn.neighbors import KNeighborsClassifier
clf = KNeighborsClassifier(n_neighbors=10,weights='uniform',p=1)
clf = clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)

from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test,y_pred)
cm_display = ConfusionMatrixDisplay(cm)

import matplotlib.pyplot as plt
plt.figure(figsize=(9,7))
cm_display.plot(ax=plt.subplot(3,2,1),cmap='YlGnBu')
plt.title('KNN classifier')
from sklearn import svm
clf = svm.SVC(kernel='linear',C=0.01)
clf = clf.fit(X_train,y_train)
x_pred = clf.predict(X_test)
cm = confusion_matrix(y_test,x_pred)
cm_display_SVM = ConfusionMatrixDisplay(cm)
cm_display_SVM.plot(ax=plt.subplot(3,2,2),cmap='YlGnBu')
plt.title('SVM classifier')
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf = clf.fit(X_train,y_train)
x_pred = clf.predict(X_test)
cm = confusion_matrix(y_test,x_pred)
cm_display_NB = ConfusionMatrixDisplay(cm)
cm_display_NB.plot(ax=plt.subplot(3,2,5),cmap='YlGnBu')
plt.title('GaussiaNB classifier')

本文来自投稿,不代表本站立场,如若转载,请注明出处:https://blog.itliujia.cn/info/20211090.html
-- 展开阅读全文 --
实验3
« 上一篇 09-30
微信小程序UI组件库合集
下一篇 » 11-04

发表评论