手写softmax回归

from scipy.io import arff
import numpy as np
import pandas as pd

radius = 4  # search radius


def distance(point1, point2):
    return np.sqrt(np.sum([(point1[i] - point2[i]) ** 2 for i in range(4)]))


iris = arff.loadarff('../dataset/iris.arff')
df = pd.DataFrame(iris[0])
length = df.shape[0]
classes = list(set(df['class']))
classes_length = len(classes)
classes_dict = dict()
for i in range(classes_length):
    classes_dict[classes[i]] = i
for i in range(length):
    df.iloc[i, 4] = classes_dict[df.iloc[i, 4]]
# df = df.sample(frac=1)  # shuffle data randomly
train_data = df.iloc[0:100]
test_data = df.iloc[100:]
train_length = train_data.shape[0]
test_length = test_data.shape[0]
accuracy = 0


def my_iter(data, batch):
    X = []
    y = []
    length = data.shape[0]
    start = 0
    while length - batch > 0:
        X = data.iloc[start:start + batch, 0:4]
        y = data.iloc[start:start + batch, 4]
        yield X, y
        start += batch
        length -= batch
    X = data.iloc[start:, 0:4]
    y = data.iloc[start:, 4]
    yield X, y


model_class_4 = np.random.rand(classes_length, 4)
learning_rate = 0.1
batch = 30
lam = 1e-4


# predict
def h(x):
    global model_class_4
    y_predict = []
    batch = x.shape[0]
    for i in range(batch):
        x_i = np.array(x.iloc[i])
        exp_array = np.sum(np.exp(model_class_4 * x_i), axis=1)  # 3
        y_predict_i = exp_array / np.sum(exp_array, axis=0)
        y_predict.append(y_predict_i)
    y_predict = np.array(y_predict)  # batch 3
    return y_predict


def one(x):
    ones = np.ones(classes_length)
    ones[x] = 1
    return ones


# loss function
def J(x, y_true, batch):
    global model_class_4
    loss_sum = 0
    derivative_loss_sum = 0
    derivative_lam_sum = 0
    y_predict = h(x)
    lam_sum = 0
    lam_sum_2 = 0
    for i in range(batch):
        y_true_i = y_true.iloc[i]
        one_i = one(y_true_i)
        loss_sum += np.sum(one_i * np.log(y_predict[y_true_i] / np.sum(y_predict)))
        lam_sum += np.sum(model_class_4)
        lam_sum_2 += np.sum(model_class_4 ** 2)
        derivative_loss_sum += (1 - y_predict[i][y_true_i])
        derivative_lam_sum += model_class_4[y_true_i]
    return -(1 / batch) * derivative_loss_sum + lam * derivative_lam_sum, -(
            1 / batch) * loss_sum + lam / 2 * lam_sum_2


for i, (X, y) in enumerate(my_iter(train_data, batch)):
    y_predict = np.array([np.argmax(y_p) for y_p in h(X)])
    delta, loss = J(X, y, X.shape[0])
    print('loss ', loss)
    print('acc ', np.sum(y_predict == y) / X.shape[0])
    model_class_4 -= learning_rate * delta
print('test accuracy')
y_predict_test = np.array([np.argmax(y_p) for y_p in h(test_data.iloc[:, 0:4])])
print('acc %.2f' % (np.sum(y_predict_test == test_data.iloc[:, 4]) / test_length))

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值