随机梯度下降
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def weight(x_train,y_train):
m,n = x_train.shape
# 初始化参数
theta = np.random.rand(n) # 随机样本位于[0, 1)中
# 学习率
alpha = 0.001
# 迭代次数
cnt = 0
max_iter = 50000
# 误差
error0 = error1 = 0
# 阈值
threshold = 0.01
while cnt<max_iter:
cnt += 1
diff = np.full(n,0)
for i in range(m):
diff = (y_train[i]-sigmoid(theta.T@x_train[i]))*x_train[i]
theta = theta + alpha * diff
if (abs(diff)<threshold).all():
break
return theta
def predict(x_test,weights):
if sigmoid(weights.T@x_test)>0.5:
return 1
else:
return 0
if __name__ =="__main__":
x_train = np.array([[1, 2.697, 6.254],
[1, 1.872, 2.014],
[1, 2.312, 0.812],
[1, 1.983, 4.990],
[1, 0.932, 3.920],
[1, 1.321, 5.583],
[1, 2.215, 1.560],
[1, 1.659, 2.932],
[1, 0.865, 7.362],
[1, 1.685, 4.763],
[1, 1.786, 2.532]])
y_train = np.array([1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1])
weights = weight(x_train, y_train)
print(predict([1, 1.321, 5.583], weights))
sklearn逻辑回归
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import numpy as np
dataset = np.loadtxt('data.txt')
x_train,x_test,y_train,y_test = train_test_split(dataset[:,0:-1],dataset[:,-1],test_size=0.3)
model = LogisticRegression()
model.fit(x_train,y_train)
print(y_test == model.predict(x_test))