原理参考:【DL笔记3】一步步用python实现Logistic回归
logistic回归——PYTHON实现
第二篇原理比较易懂,代码好像有点问题。
代码传送门:https://github.com/taifyang/machine-learning
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 <iostream>
#include <vector>
#include <map>
#include <algorithm>
//k邻近模型
class KNN
{
public:
KNN(std::vector<std::vector<float>> x, std::vector<float> y, int k, float p) : m_x(x), m_y(y), m_k(k), m_p(p) {};
int predict(std::vector<std::vector<float>> x)
{
x.resize(m_x.size());
for (size_t i = 0; i < x.size(); i++)
{
x[i] = x[0];
}
//计算预测数据和训练数据的差值
std::vector<std::vector<float>> diff = x;
for (size_t i = 0; i < diff.size(); i++)
{
for (size_t j = 0; j < diff[0].size(); j++)
{
diff[i][j] -= m_x[i][j];
}
}
//计算范数
std::vector<float> dist(diff.size(), 0);
for (size_t i = 0; i < diff.size(); i++)
{
for (size_t j = 0; j < diff[0].size(); j++)
{
dist[i] += pow(diff[i][j], m_p);
}
dist[i] = pow(dist[i], 1.0 / m_p);
}
//返回从小到大排序的索引
std::vector<int> dist_sorted(dist.size());
for (size_t i = 0; i != dist_sorted.size(); ++i) dist_sorted[i] = i;
std::sort(dist_sorted.begin(), dist_sorted.end(), [&dist](size_t i, size_t j) {return dist[i] < dist[j]; });
//分类投票
std::map<float, int> count;
for (size_t i = 0; i < m_k; i++)
{
float vote = m_y[dist_sorted[i]];
count[vote] += 1;
}
//返回投票最多的类别标签
return count.rbegin()->first;
}
private:
std::vector<std::vector<float>> m_x;
std::vector<float> m_y;
int m_k;
float m_p;
};
int main(int argc, char* argv[])
{
std::vector<std::vector<float>> x = { { 0, 10 },{ 1, 8 },{ 10, 1 },{ 7, 4 } };
std::vector<float> y = { 0, 0, 1, 1 };
KNN knn = KNN(x, y, 3, 2);
std::cout << "预测值为:" << knn.predict({ {6,2} }) << std::endl;
system("pause");
return EXIT_SUCCESS;
}