一、实验报告
二、实验代码
(1)KNN:
import pandas as pd
import numpy as np
from scipy.spatial import distance#用于计算欧式距离
from sklearn.preprocessing import StandardScaler#用于对数据进行标准化操作
from sklearn.neighbors import KNeighborsRegressor#KNN算法
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris #导入数据集
import matplotlib.pyplot as plt
# 鸢尾花
iris=load_iris()
df = pd.DataFrame(data=iris.data,columns=iris.feature_names)
df["class"]=iris.target
df["class"]=df["class"].map({0:iris.target_names[0], 1:iris.target_names[1], 2:iris.target_names[2]})
# 解析数据
x=iris.data
y=iris.target.reshape(-1,1)
# 划分训练集和测试集
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=30,stratify=y)
test = x_test,y_test
module = KNeighborsClassifier(n_neighbors=1)
module.fit(x_train, y_train.ravel())
y_predi = module.predict(x_test)
ans = 0
for i in range(len(y_test)):
if y_test[i] == y_predi[i]:
ans += 1
print(ans / len(y_test))
(2)朴素贝叶斯:
from sklearn.datasets import load_iris #导入数据集
from sklearn.naive_bayes import GaussianNB #朴素贝叶斯分类器#
from sklearn.model_selection import train_test_split
iris = load_iris()
# 解析数据
x=iris.data
y=iris.target.reshape(-1,1)
# 划分训练集和测试集
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=30,stratify=y)
module = GaussianNB()
module.fit(x_train, y_train.ravel())
y_predi = module.predict(x_test)
ans = 0
for i in range(len(y_test)):
if y_test[i] == y_predi[i]:
ans += 1
print(ans / len(y_test))
(3)决策树:
from sklearn import tree#决策树分类器
import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris #导入数据集
import pydotplus
import os
os.environ["PATH"] += os.pathsep + 'E:/graphviz/bin'
iris = load_iris()
# 解析数据
x=iris.data
y=iris.target.reshape(-1,1)
# 划分训练集和测试集
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3)
module = tree.DecisionTreeClassifier(criterion='gini',max_depth=3)
module.fit(x_train, y_train.ravel())
dot_data = tree.export_graphviz(module, out_file=None)
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf("iris3.pdf")
Z = module.predict(x_test)
plt.plot(x_test[0:45],y_test[0:45],c = 'b')
plt.plot(x_test[0:45],Z[0:45],c = 'r')
plt.show()