其他分享
首页 > 其他分享> > 鸢尾花的分类三分类问题

鸢尾花的分类三分类问题

作者:互联网

鸢尾花的分类

首先先导入会使用到的包

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split 
from numpy import random
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
import math

然后对数据进行划分为测试集与训练集

X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target)

然后尝试打印出X_train.shape来查看其有多少特征值

print(X_train.shape)
	(112, 4)

可知一共有112组数据有4个特征值
再打印y_train来查看有多少中分类

print(y_train)
array([2, 1, 0, 0, 0, 2, 0, 1, 1, 1, 1, 0, 1, 1, 2, 1, 2, 1, 0, 2, 0, 0,
       2, 2, 1, 1, 0, 1, 2, 0, 1, 2, 2, 2, 2, 1, 0, 0, 1, 0, 1, 2, 2, 2,
       2, 2, 2, 0, 0, 1, 2, 1, 0, 2, 2, 0, 0, 1, 1, 2, 0, 1, 2, 0, 1, 0,
       0, 0, 0, 0, 2, 2, 0, 1, 1, 0, 0, 2, 2, 2, 2, 1, 2, 1, 2, 1, 0, 0,
       0, 0, 1, 1, 2, 2, 1, 2, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 1, 2, 1, 0,
       1, 0])

可知一共有三种类型 分别为0,1,2
对每一组数据我们可以通过给出对0,1,2三种类型的概率来体现出是哪种类型的鸢尾花的概率大 比如[0.25,0.2,0.55]分别表面有0.25的概率为0型的鸢尾花有0.2的概率为1型的鸢尾花有0.55的概率为2型的鸢尾花,对此我们判定其为2型的鸢尾花。但是其结果并不一定就是2型的鸢尾花,如果不是,那么我们应该是的0.55的概率尽可能的减小让正确的那个概率尽可能的变大。
我们可以使用y=kx+b y为预测的值 x为训练集 k与b为系数
y应该是112
3的矩阵 已知x为1124的矩阵 b为0
y可以变化为x
k+b
所以k为4*3的矩阵
因为一开始我们并不能确定k的值的大小 所以我们可以随机生成

k,b =  np.random.rand(4,3), 0
print(k)
array([[0.31635924, 0.2329209 , 0.22559666],
       [0.73691415, 0.86794294, 0.37659187],
       [0.27317436, 0.9867018 , 0.09955569],
       [0.6701651 , 0.69010967, 0.78548111]])

因为y中的值表示的意思为其对应的类型的概率所以可知其>=0 且这三个值应该相加为1.但是有x*k+b所得到的y有极大的可能不满足这要求,但是我们可以通过softmax()函数来达到这要求
Sofmax()介绍:
softmax函数,又称归一化指数函数。它是二分类函数sigmoid在多分类上的推广,目的是将多分类的结果以概率的形式展现出来。下图展示了softmax的计算方法:

softmax直白来说就是将原来输出是3,1,-3通过softmax函数一作用,就映射成为(0,1)的值,而这些值的累和为1(满足概率的性质),那么我们就可以将它理解成概率,在最后选取输出结点的时候,我们就可以选取概率最大(也就是值对应最大的)结点,作为我们的预测目标!
以下为softmax函数的定义
def softmax(x):
return np.exp(x) / np.tile(np.sum(np.exp(x), axis=1).reshape(112, 1), 3)
有上面打印出来的y_train可知y_train的值为0,1,2 而我们输出的预测值为[?,?,?]所以我们需要将其改为1*3的形式

y_train_1=[]
for i in range(len(y_train)):
    if y_train[i]==0:
        y_train_1.append([1,0,0])
    if y_train[i]==1:
        y_train_1.append([0,1,0])
    if y_train[i]==2:
        y_train_1.append([0,0,1])

y=x*k+b得出的值是随机的 而我们应该使其变得有意义,那么就要改变k与b的值而这应该通过y与y_train_1来改变。交叉熵函数表示预测值y与y_train_1的区别的大小,即我们需要改变k,b是的交叉熵函数的值变小。其值的变小意味着误差的减小 而交叉熵函数我们将其的定义为

def Loss(y,yhat):#y与测试集 yhat 为预测值
    return np.mean(-1*np.sum(y*np.log(yhat)))
yhat=x*k+b 对其中的k与b分别求导得
def Loss_k(yhat,X_train,y):
    return np.dot(X_train.T,(yhat-y))

def Loss_b(yhat,y):
    return np.mean(yhat-y)
而k,b通过下述公式改变:
	 k = k + (-1) * k1 * learning_rate#k1为Loss对k求导的值
   	 b = b + (-1) * b1 * learning_rate#b1为Loss对k求导的值

接下来我们可以通过对训练集的不断尝试来使得交叉熵减少得出合适的k与b的值

learning_rate = 0.0001
epoch = 10000
min_loss = float('inf')
for e in range(epoch):
    yhat=softmax(np.dot(X_train,k)+b)
    loss = Loss(y_train_1,yhat)  
    b1=Loss_b(yhat,y_train_1)
    k1 = np.dot(X_train.T, yhat-y_train_1)    
    k = k + (-1) * k1 * learning_rate
    b = b + (-1) * b1 * learning_rate    
    if loss < min_loss:
        min_loss = loss
        best_W, best_b = k, b
        print("Loss updated to {} at the {}th epoch.".format(loss, e))

可以发现最开始时因为是随机的k、b所以Loss很大 而经过不断的训练Loss不断的减小。

通过一下代码打印出测试集

def maxk(y):
    yy=[]
    for i in range(len(y)):
        x=y[i][0]
        xx=0
        for ii in range(3):
            if x<y[i][ii]:
                x=y[i][ii]
                xx=ii
        yy.append(xx)
    return yy
yy=maxk(yhat)   
xx=np.linspace(1,38,num=38)
plt.scatter(xx,yy)

在这里插入图片描述

通过以下代码打印出我们的预测值

y_test_1=[]
for i in range(len(y_test)):
    if y_test[i]==0:
        y_test_1.append([1,0,0])
    if y_test[i]==1:
        y_test_1.append([0,1,0])
    if y_test[i]==2:
        y_test_1.append([0,0,1])
y_test_1=np.array(y_test_1)
plt.scatter(xx,y_test,color='red')

在这里插入图片描述

通过以下代码查看两次的结果

plt.scatter(xx,y_test,color='red')
plt.scatter(xx,yy)

在这里插入图片描述

可以发现在38个测试中只有一个出现了错误。正确率达到了37/38

代码:`在这里插入代码片

## 标题from sklearn.datasets import load_iris 
from sklearn.model_selection import train_test_split
from numpy import random
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
import math
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target)
def softmax(x):
    return np.exp(x) / np.tile(np.sum(np.exp(x), axis=1).reshape(112, 1), 3)
y_train_1=[]
for i in range(len(y_train)):
    if y_train[i]==0:
        y_train_1.append([1,0,0])
    if y_train[i]==1:
        y_train_1.append([0,1,0])
    if y_train[i]==2:
        y_train_1.append([0,0,1])
def Loss(y,yhat):
    return np.mean(-1*np.sum(y*np.log(yhat)))
def Loss_k(yhat,X_train,y):
    return np.dot(X_train.T,(yhat-y))
def Loss_b(yhat,y):
    return np.mean(yhat-y)
k,b =  np.random.rand(4,3), 0
learning_rate = 0.0001
epoch = 10000
min_loss = float('inf')
for e in range(epoch):
    yhat=softmax(np.dot(X_train,k)+b)
    #yhat=softmax(yhat)
    #y=np.matmul(X_train,k)+b
    #y1=np.argmax(y,axis=1)
    #print("y_hat = {} * x + {}".format(w1, w0))
    loss = Loss(y_train_1,yhat)  
    b1=Loss_b(yhat,y_train_1)
    k1 = np.dot(X_train.T, yhat-y_train_1)
    
    
    k = k + (-1) * k1 * learning_rate
    b = b + (-1) * b1 * learning_rate
    
    if loss < min_loss:
        min_loss = loss
        best_W, best_b = k, b
        print("Loss updated to {} at the {}th epoch.".format(loss, e))
def maxk(y):
    yy=[]
    for i in range(len(y)):
        x=y[i][0]
        xx=0
        for ii in range(3):
            if x<y[i][ii]:
                x=y[i][ii]
                xx=ii
        yy.append(xx)
    return yy
yy=maxk(yhat)   

xx=np.linspace(1,38,num=38)
y_test_1=[]
for i in range(len(y_test)):
    if y_test[i]==0:
        y_test_1.append([1,0,0])
    if y_test[i]==1:
        y_test_1.append([0,1,0])
    if y_test[i]==2:
        y_test_1.append([0,0,1])
y_test_1=np.array(y_test_1)
plt.scatter(xx,y_test,color='red')
plt.scatter(xx,yy)

标签:Loss,loss,yhat,分类,问题,test,train,np,鸢尾花
来源: https://blog.csdn.net/weixin_45038497/article/details/110734809