原理参考:【DL笔记3】一步步用python实现Logistic回归
logistic回归——PYTHON实现
第二篇原理比较易懂,代码好像有点问题。
python实现:
import numpy as np
#Logistic回归模型
class LogisticRegression():
def __init__(self, x, y):
self.x = x
self.y = y
self.w = np.zeros(self.x.shape[1])
self.b = 0
def Logistic_sigmoid(self, y):
#非线性层,将值域空间映射为(0,1)
return np.exp(y)/(1+np.exp(y))
def Logistic_cost(self, p, y):
#损失函数
return np.sum(-y*np.log(p)-(1-y)*np.log(1-p))
def Logistic_BP(self, alpha, iters):
#反向传播函数
for i in range(iters):
p = np.dot(self.x, self.w.T)+self.b
a = self.Logistic_sigmoid(p)
print('iters:', i,' cost:', self.Logistic_cost(a, y))
dz = a -self.y
self.w -= alpha*np.dot(dz.T, self.x)
self.b -= alpha*sum(dz)
return self.w, self.b
def Logistic_predict(self, x):
#预测函数
return self.Logistic_sigmoid(np.dot(x, self.w.T)+self.b)
if __name__ == '__main__':
x = np.array([[0], [1], [2], [3]])
y = np.array([0, 0, 1, 1])
lg = LogisticRegression(x, y)
w, b = lg.Logistic_BP(alpha=0.1, iters=100)
print('最终训练得到的w和b为:', w, ',', b)
pre = lg.Logistic_predict(np.array([[2.9]]))
print('预测结果为:', pre)
python调包:
import numpy as np
from sklearn.linear_model import LogisticRegression
#Logistic回归模型
class MyLogisticRegression():
def __init__(self, x, y):
self.x = x
self.y = y
self.clf = LogisticRegression()
def train(self):
self.clf.fit(self.x, self.y)
w = self.clf.coef_
b = self.clf.intercept_
return w, b
def predict(self, x):
return self.clf.predict_proba(x)
if __name__ == '__main__':
x = np.array([[0], [1], [2], [3]])
y = np.array([[0], [0], [1], [1]])
lr = MyLogisticRegression(x, y)
w, b = lr.train()
print('最终训练得到的w和b为:', w, ',', b)
print('预测结果为:', lr.predict([[2.9]]))
C++实现:
#include#include #include



