本文实例讲述了python实现的逻辑回归算法。分享给大家供大家参考,具体如下:
使用python实现逻辑回归
using python to implement logistic regression algorithm
菜鸟写的逻辑回归,记录一下学习过程
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#encoding:utf-8 """ author: njulpy version: 1.0 data: 2018/04/10 project: using python to implement logisticregression algorithm """ import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split #建立sigmoid函数 def sigmoid(x): x = x.astype( float ) return 1. / ( 1 + np.exp( - x)) #训练模型,采用梯度下降算法 def train(x_train,y_train,num,alpha,m,n): beta = np.ones(n) for i in range (num): h = sigmoid(np.dot(x_train,beta)) #计算预测值 error = h - y_train.t #计算预测值与训练集的差值 delt = alpha * (np.dot(error,x_train)) / m #计算参数的梯度变化值 beta = beta - delt #print('error',error) return beta def predict(x_test,beta): y_predict = np.zeros( len (y_test)) + 0.5 s = sigmoid(np.dot(beta,x_test.t)) y_predict[s < 0.34 ] = 0 y_predict[s > 0.67 ] = 1 return y_predict def accurancy(y_predict,y_test): acc = 1 - np. sum (np.absolute(y_predict - y_test)) / len (y_test) return acc if __name__ = = "__main__" : data = pd.read_csv( 'iris.csv' ) x = data.iloc[:, 1 : 5 ] y = data.iloc[:, 5 ].copy() y.loc[y = = 'setosa' ] = 0 y.loc[y = = 'versicolor' ] = 0.5 y.loc[y = = 'virginica' ] = 1 x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.3 ,random_state = 15 ) m,n = np.shape(x_train) alpha = 0.01 beta = train(x_train,y_train, 1000 ,alpha,m,n) pre = predict(x_test,beta) t = np.arange( len (x_test)) plt.figure() p1 = plt.plot(t,pre) p2 = plt.plot(t,y_test,label = 'test' ) label = [ 'prediction' , 'true' ] plt.legend(label, loc = 1 ) plt.show() acc = accurancy(pre,y_test) print ( 'the predicted value is ' ,pre) print ( 'the true value is ' ,np.array(y_test)) print ( 'the accuracy rate is ' ,acc) |
输出结果:
the predicted value is [ 0. 0.5 1. 0. 0. 1. 1. 0.5 1. 1. 1. 0.5 0.5 0.5 1.
0. 0.5 1. 0. 1. 0.5 0. 0.5 0.5 0. 0. 1. 1. 1. 1.
0. 1. 1. 1. 0. 0. 1. 0. 0. 0.5 1. 0. 0. 0.5 1. ]
the true value is [0 0.5 0.5 0 0 0.5 1 0.5 0.5 1 1 0.5 0.5 0.5 1 0 0.5 1 0 1 0.5 0 0.5 0.5 0
0 1 1 1 0.5 0 1 0.5 1 0 0 1 0 0 0.5 1 0 0 0.5 1]
the accuracy rate is 0.9444444444444444
附:上述示例中的iris.csv文件点击此处本站下载。
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/njulpy/article/details/79899352