1.向量机
from sklearn import svm#svm函数需要的
import numpy as np#numpy科学计算库
from sklearn import model_selection
import matplotlib.pyplot as plt
#如果想要好的结果需要进行参数的调整,同时选取的用于训练的参数也可以增加到四个(本文用了前两个)
#数据每一行一个鸢尾花的观察结果,前四个数据代表鸢尾花的生物属性比如大小等,
#最后一个数据是鸢尾花的类别,共三类。主要是对最后这个类别进行处理。
#–IrisSetosa
#–IrisVersicolour
#–IrisVirginica
#定义一个函数,将三个类别分别与三个不同数字相对应
def iris_type(s):
it={b'Iris-setosa':0,b'Iris-versicolor':1,b'Iris-virginica':2}
return it[s]
#导入数据
path ='C:/Users/HP/Desktop/模式识别大作业/IRIS/1.txt' # 之前保存的文件路径
data = np.loadtxt(path, # 路径
dtype=float, # 数据类型
delimiter=',', # 数据以什么分割符号分割数据
converters={4: iris_type}) # 对某一列数据(第四列)进行某种类型的转换()
#将原始数据分成训练集和测试集
X,y=np.split(data,(4,),axis=1)
x=X[:,0:2]
y=y.reshape((-1))
x_train,x_test,y_train,y_test=model_selection.train_test_split(x,y,random_state=1,test_size=0.3)
#np.split按照列(axis=1)进行分割,从第四列开始往后的作y数据,之前作为X数据。
#在X中我们取前两列作为特征(为了后面的可视化)。
#用train_test_split将数据分为训练集和测试集,测试集占总数据的30%(test_size=0.3),random_state是随机数种子
#(随机数种子:其实就是该组随机数的编号,在需要重复试验的时候,保证得到一组一样的随机数。
#比如你每次都填1,其他参数一样的情况下你得到的随机数组是一样的。但填0或不填,每次都会不一样。
#随机数的产生取决于种子,随机数和种子之间的关系遵从以下两个规则:种子不同,产生不同的随机数;种子相同,即使实例不同也产生相同的随机数。)
#搭建模型选择不同的核函数可能有不同的效果poly rbf sigmo idlinear
clf = svm.SVC(kernel='rbf', # 核函数
gamma=0.1,
decision_function_shape='ovo', # one vs one 分类问题
C=0.8)
clf.fit(x_train, y_train)
#输出训练集的准确率
print(clf.score(x_train,y_train))
#原始结果与训练集预测结果进行对比
y_train_hat=clf.predict(x_train)
y_train_1d=y_train.reshape((-1))
#zip把原始结果和预测结果放在一起
comp=zip(y_train_1d,y_train_hat)
print(list(comp))
print(clf.score(x_test,y_test))
#训练好的模型对测试集的数据进行预测
y_test_hat=clf.predict(x_test)
y_test_1d=y_test.reshape((-1))
comp=zip(y_test_1d,y_test_hat)
print(list(comp))
#print(clf.score(x_test,y_test))
#图像进行可视化左图为训练数据,右图为对训练数据的预测
plt.figure()
plt.subplot(121)
plt.scatter(x_train[:,0],x_train[:,1],c=y_train.reshape((-1)),edgecolors='k',s=50)
plt.subplot(122)
plt.scatter(x_train[:,0],x_train[:,1],c=y_train_hat.reshape((-1)),edgecolors='k',s=50)
plt.show()
修改不同的核函数会有不同的结果
准确率 0.8380952380952381
2.贝叶斯决策
%贝叶斯
clc
clear
data=xlsread('C:/Users/HP/Desktop/模式识别大作业/IRIS/iris.csv');
Iris1=data(1:50,1:4);
Iris2=data(51:100,1:4);
Iris3=data(101:150,1:4);
%类均值向量
m1 = mean(Iris1);
m2 = mean(Iris2);
m3 = mean(Iris3);
%各类内离散度矩阵
s1 = zeros(4);
s2 = zeros(4);
s3 = zeros(4);
for i=1:1:30
s1 = s1 + (Iris1(i,:) - m1)'*(Iris1(i,:) - m1);
end
for i=1:1:30
s2 = s2 + (Iris2(i,:) - m2)'*(Iris2(i,:) - m2);
end
for i=1:1:30
s3 = s3 + (Iris3(i,:) - m3)'*(Iris3(i,:) - m3);
end
%总类内离散矩阵
sw12 = s1 + s2;
sw13 = s1 + s3;
sw23 = s2 + s3;
%投影方向
w12 = ((sw12^-1)*(m1 - m2)')';
w13 = ((sw13^-1)*(m1 - m3)')';
w23 = ((sw23^-1)*(m2 - m3)')';
%判别函数以及阈值T(即w0)
T12 = -0.5 * (m1 + m2)*inv(sw12)*(m1 - m2)';
T13 = -0.5 * (m1 + m3)*inv(sw13)*(m1 - m3)';
T23 = -0.5 * (m2 + m3)*inv(sw23)*(m2 - m3)';
%% 请补充输出判别函数
%% 请给下面代码添加必要注释
kind1 = 0;
kind2 = 0;
kind3 = 0;
newiris1=[];
newiris2=[];
newiris3=[];
for i=31:50
x = Iris1(i,:);
g12 = w12 * x' + T12;
g13 = w13 * x' + T13;
g23 = w23 * x' + T23;
if((g12 > 0)&(g13 > 0))
newiris1=[newiris1;x];
kind1=kind1+1;
elseif((g12 < 0)&(g23 > 0))
newiris2=[newiris2;x];
elseif((g13 < 0)&(g23 < 0))
newiris3=[newiris3;x];
end
end
for i=31:50
x = Iris2(i,:);
g12 = w12 * x' + T12;
g13 = w13 * x' + T13;
g23 = w23 * x' + T23;
if((g12 > 0)&(g13 > 0))
newiris1=[newiris1;x];
elseif((g12 < 0)&(g23 > 0))
kind2=kind2+1;
newiris2=[newiris2;x];
elseif((g13 < 0)&(g23 < 0))
newiris3=[newiris3;x];
end
end
for i=31:50
x = Iris3(i,:);
g12 = w12 * x' + T12;
g13 = w13 * x' + T13;
g23 = w23 * x' + T23;
if((g12 > 0)&(g13 > 0))
newiris1=[newiris1;x];
elseif((g12 < 0)&(g23 > 0))
newiris2=[newiris2;x];
elseif((g13 < 0)&(g23 < 0))
kind3=kind3+1;
newiris3=[newiris3;x];
end
end
correct=(kind1+kind2+kind3)/60;
fprintf('\n综合正确率:%.2f%%\n\n',correct* 100);
综合正确率:96.67%
3.决策树
#决策树分析 iris_data分为70%的训练,30%的进行预测 然后进行优化 输出准确率、召回率等
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris #导入数据集iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
iris = load_iris() #载入数据集
x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target, test_size=0.3)
clf = DecisionTreeClassifier()
clf.fit(x_train,y_train)
predict_target = clf.predict(x_test)
print(sum(predict_target == y_test)) #预测结果与真实结果比对
print(metrics.classification_report(y_test,predict_target))
print(metrics.confusion_matrix(y_test,predict_target))
L1 = [n[0] for n in x_test]
L2 = [n[1] for n in x_test]
plt.scatter(L1,L2, c=predict_target,marker='x')
plt.title('DecisionTreeClassifier')
plt.show()
4.fisher准则
#fisher准则
#导入包
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 导入数据集
df = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=0)
Iris1 = df.values[0:50, 0:4]
Iris2 = df.values[50:100, 0:4]
Iris3 = df.values[100:150, 0:4]
# 定义类均值向量
m1 = np.mean(Iris1, axis=0)
m2 = np.mean(Iris2, axis=0)
m3 = np.mean(Iris3, axis=0)
# 定义类内离散度矩阵
s1 = np.zeros((4, 4))
s2 = np.zeros((4, 4))
s3 = np.zeros((4, 4))
for i in range(0, 30, 1):
a = Iris1[i, :] - m1
a = np.array([a])
b = a.T
s1 = s1 + np.dot(b, a)
for i in range(0, 30, 1):
c = Iris2[i, :] - m2
c = np.array([c])
d = c.T
s2 = s2 + np.dot(d, c)
# s2=s2+np.dot((Iris2[i,:]-m2).T,(Iris2[i,:]-m2))
for i in range(0, 30, 1):
a = Iris3[i, :] - m3
a = np.array([a])
b = a.T
s3 = s3 + np.dot(b, a)
# 定义总类内离散矩阵
sw12 = s1 + s2;
sw13 = s1 + s3;
sw23 = s2 + s3;
# 定义投影方向
a = np.array([m1 - m2])
sw12 = np.array(sw12, dtype='float')
sw13 = np.array(sw13, dtype='float')
sw23 = np.array(sw23, dtype='float')
# 判别函数以及阈值T(即w0)
a = m1 - m2
a = np.array([a])
a = a.T
b = m1 - m3
b = np.array([b])
b = b.T
c = m2 - m3
c = np.array([c])
c = c.T
w12 = (np.dot(np.linalg.inv(sw12), a)).T
w13 = (np.dot(np.linalg.inv(sw13), b)).T
w23 = (np.dot(np.linalg.inv(sw23), c)).T
T12 = -0.5 * (np.dot(np.dot((m1 + m2), np.linalg.inv(sw12)), a))
T13 = -0.5 * (np.dot(np.dot((m1 + m3), np.linalg.inv(sw13)), b))
T23 = -0.5 * (np.dot(np.dot((m2 + m3), np.linalg.inv(sw23)), c))
# 计算正确率
kind1 = 0
kind2 = 0
kind3 = 0
newiris1 = []
newiris2 = []
newiris3 = []
for i in range(30, 49):
x = Iris1[i, :]
x = np.array([x])
g12 = np.dot(w12, x.T) + T12
g13 = np.dot(w13, x.T) + T13
g23 = np.dot(w23, x.T) + T23
if g12 > 0 and g13 > 0:
newiris1.extend(x)
kind1 = kind1 + 1
elif g12 < 0 and g23 > 0:
newiris2.extend(x)
elif g13 < 0 and g23 < 0:
newiris3.extend(x)
for i in range(30, 49):
x = Iris2[i, :]
x = np.array([x])
g12 = np.dot(w12, x.T) + T12
g13 = np.dot(w13, x.T) + T13
g23 = np.dot(w23, x.T) + T23
if g12 > 0 and g13 > 0:
newiris1.extend(x)
elif g12 < 0 and g23 > 0:
newiris2.extend(x)
kind2 = kind2 + 1
elif g13 < 0 and g23 < 0:
newiris3.extend(x)
for i in range(30, 49):
x = Iris3[i, :]
x = np.array([x])
g12 = np.dot(w12, x.T) + T12
g13 = np.dot(w13, x.T) + T13
g23 = np.dot(w23, x.T) + T23
if g12 > 0 and g13 > 0:
newiris1.extend(x)
elif g12 < 0 and g23 > 0:
newiris2.extend(x)
elif g13 < 0 and g23 < 0:
newiris3.extend(x)
kind3 = kind3 + 1
correct = (kind1 + kind2 + kind3) / 60
print('判断出来的综合正确率:', correct * 100, '%')
判断出来的综合正确率: 91.66666666666666 %