# 手把手教你机器学习算法：逻辑回归（附源码）

## 2、由来

Sigmoid Logistic Function

## 4、代价函数与梯度下降

Andrew Ng老师解释了一下这个代价函数的合理性，我们首先看当y=1的情况：

## 5、代码与实现

[python] view plaincopy

1. from numpy import loadtxt, where
2. from pylab import scatter, show, legend, xlabel, ylabel
5. X = data[:, 0:2]
6. y = data[:, 2]
7. pos = where(y == 1)
8. neg = where(y == 0)
9. scatter(X[pos, 0], X[pos, 1], marker='o', c='b')
10. scatter(X[neg, 0], X[neg, 1], marker='x', c='r')
11. xlabel('Feature1/Exam 1 score')
12. ylabel('Feature2/Exam 2 score')
13. legend(['Fail', 'Pass'])
14. show()

[python] view plaincopy

1. def sigmoid(X):
2. '''''Compute sigmoid function '''
3. den =1.0+ e **(-1.0* X)
4. gz =1.0/ den
5. return gz
6. def compute_cost(theta,X,y):
7. '''''computes cost given predicted and actual values'''
8. m = X.shape[0]#number of training examples
9. theta = reshape(theta,(len(theta),1))
10. J =(1./m)*(-transpose(y).dot(log(sigmoid(X.dot(theta))))- transpose(1-y).dot(log(1-sigmoid(X.dot(theta)))))
12. #optimize.fmin expects a single value, so cannot return grad
16. theta.shape =(1,3)
18. h = sigmoid(X.dot(theta.T))
19. delta = h - y
21. for i in range(l):
22. sumdelta = delta.T.dot(X[:, i])
24. theta.shape =(3,)

[python] view plaincopy

1. def predict(theta, X):
2. '''''Predict label using learned logistic regression parameters'''
3. m, n = X.shape
4. p = zeros(shape=(m,1))
5. h = sigmoid(X.dot(theta.T))
6. for it in range(0, h.shape[0]):
7. if h[it]>0.5:
8. p[it,0]=1
9. else:
10. p[it,0]=0
11. return p
12. #Compute accuracy on our training set
13. p = predict(array(theta), it)
14. print'Train Accuracy: %f'%((y[where(p == y)].size / float(y.size))*100.0)

[python] view plaincopy

1. def map_feature(x1, x2):
2. '''''
3. Maps the two input features to polonomial features.
4. Returns a new feature array with more features of
5. X1, X2, X1 ** 2, X2 ** 2, X1*X2, X1*X2 ** 2, etc...
6. '''
7. x1.shape =(x1.size,1)
8. x2.shape =(x2.size,1)
9. degree =6
10. mapped_fea = ones(shape=(x1[:,0].size,1))
11. m, n = mapped_fea.shape
12. for i in range(1, degree +1):
13. for j in range(i +1):
14. r =(x1 **(i - j))*(x2 ** j)
15. mapped_fea = append(out, r, axis=1)
16. return mapped_fea
17. mapped_fea = map_feature(X[:,0], X[:,1])

[python] view plaincopy

1. def cost_function_reg(theta, X, y, l):
2. '''''Compute the cost and partial derivatives as grads
3. '''
4. h = sigmoid(X.dot(theta))
5. thetaR = theta[1:,0]
6. J =(1.0/ m)*((-y.T.dot(log(h)))-((1- y.T).dot(log(1.0- h)))) \
7. +(l /(2.0* m))*(thetaR.T.dot(thetaR))
8. delta = h - y
9. sum_delta = delta.T.dot(X[:,1])
11. XR = X[:,1:X.shape[1]]
12. sum_delta = delta.T.dot(XR)
13. grad =(1.0/ m)*(sum_delta + l * thetaR)
17. return J.flatten(), out.T.flatten()
18. m, n = X.shape
19. y.shape =(m,1)
20. it = map_feature(X[:,0], X[:,1])
21. #Initialize theta parameters
22. initial_theta = zeros(shape=(it.shape[1],1))
23. #Use regularization and set parameter lambda to 1
24. l =1
25. # Compute and display initial cost and gradient for regularized logistic
26. # regression
27. cost, grad = cost_function_reg(initial_theta, it, y, l)
28. def decorated_cost(theta):
29. return cost_function_reg(theta, it, y, l)
30. print fmin_bfgs(decorated_cost, initial_theta, maxfun=500)

[python] view plaincopy

1. #Plot Boundary
2. u = linspace(-1,1.5,50)
3. v = linspace(-1,1.5,50)
4. z = zeros(shape=(len(u), len(v)))
5. for i in range(len(u)):
6. for j in range(len(v)):
7. z[i, j]=(map_feature(array(u[i]), array(v[j])).dot(array(theta)))
8. z = z.T
9. contour(u, v, z)
10. title('lambda = %f'% l)
11. xlabel('Microchip Test 1')
12. ylabel('Microchip Test 2')
13. legend(['y = 1','y = 0','Decision boundary'])
14. show()
15. def predict(theta, X):
16. '''''Predict whether the label
17. is 0 or 1 using learned logistic
18. regression parameters '''
19. m, n = X.shape
20. p = zeros(shape=(m,1))
21. h = sigmoid(X.dot(theta.T))
22. for it in range(0, h.shape[0]):
23. if h[it]>0.5:
24. p[it,0]=1
25. else:
26. p[it,0]=0
27. return p
28. #% Compute accuracy on our training set
29. p = predict(array(theta), it)
30. print'Train Accuracy: %f'%((y[where(p == y)].size / float(y.size))*100.0)

821 篇文章177 人订阅

0 条评论

## 相关文章

953

8057

4034

2921

1021

56010

### 目标检测（CVPR2017）：Feature Pyramid Networks

（a）使用图像金字塔构建特征金字塔网络：每个图像尺度上的特征都是独立计算的，速度缓慢。

1313

### 测试数据科学家聚类技术的40个问题（能力测验和答案）（下）

【AI100 导读】本次测试的重点主要集中在概念、聚类基本原理以及各种技术的实践知识等方面。本文为下部，包括21-40题。上部请查看： 测试数据科学家聚类技术的...

3504

2237

36210