import numpy as np
filepath = r'C:\Users\Administrator\Desktop\ML\machinelearninginaction-master\machinelearninginaction-master\Ch05'
def load_dataset():
data_mat = []
label_mat = []
fr = open(filepath + '/testSet.txt')
for line in fr.readlines():
line = line.strip().split()
data_mat.append([1.0, float(line[0]), float(line[1])]) # 第一个是常数项
label_mat.append(int(line[2]))
return data_mat, label_mat
def sigmoid(in_x): # RuntimeWarning: overflow encountered in exp
# return 1.0/(1 + np.exp(-in_x))
# 优化方法
if in_x >= 0:
return 1.0/(1+np.exp(-in_x))
else:
return np.exp(in_x)/(1+np.exp(in_x))
# # 梯度上升算法
# def grad_ascent(datamat_in, class_labels):
# datamat = np.mat(datamat_in)
# label_mat = np.mat(class_labels).transpose()
# m, n = np.shape(datamat)
# alpha = 0.001
# max_cycle = 500
# weights = n
使用Python实现LR算法_ RuntimeWarning: overflow encountered in exp问题解决方案
最新推荐文章于 2024-04-25 11:17:09 发布