机器学习——softmax回归(Iris数据集)
第五周课程课后作业
用梯度下降的方法,实现softmax回归多分类算法,数据采用iris,数据取全部三类,并采用交叉验证法进行模型评估。
# -*- coding:utf-8 -*-
import numpy as np
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import f1_score
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score
from sklearn.metrics import accuracy_score
# 将第五列通过转换函数映射成浮点类型的数据
def iris_type(s):
it = {
b'Iris-setosa': 0, b'Iris-versicolor': 1, b'Iris-virginica': 2}
return it[s]
def softmax(z):
# 防止溢出,不改变结果
M = max(z)
z = z - M
return np.exp(z) / np.sum(np.exp(z))
def trainning(x_train, y_train):
global learningrate, d
c = 3 # 类的数量
t