线性规划是机器学习中的最基本问题,根据提供的x来预测y。

设输入变量为$x_i$,对应的输出变量为$y_i$,线性拟合问题及找出最好的线性函数$h_\theta(x)=\theta_0+\theta_1x$,使得其跟输出变量之间的差最小,我们定义这个差值为
$$J_(\theta_0,\theta_1)=\frac{1}{m}\sum_{i=1}^m(h_\theta(x_i)-y_i)^2$$
我们利用梯度下降算法,来找出最合适的参数$\theta_0,\theta_1$

$$\begin{aligned}\theta_0&:=\theta_0-\alpha\frac{\partial}{\partial\theta_0}J(\theta_0,\theta_1)\\&:=\theta_0-\frac{\alpha}{m}\sum_{i=1}^m(\theta_0+\theta_1x_i-y_i)\end{aligned}$$
$$\begin{aligned}\theta_1&:=\theta_1-\alpha\frac{\partial}{\partial \theta_1}J(\theta_0,\theta_1)\\&:=\theta_1-\frac{\alpha}{m}\sum_{i=1}^m(\theta_0+\theta_1x_i-y_i)x_i\end{aligned}$$

这里我们利用三种方法来实现,分别为自己写的、利用sklearn程序包和tensorflow机器学习工具包。

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from sklearn.datasets import load_boston
import pandas as pd
import matplotlib.pyplot as plt
boston = load_boston()
data = pd.DataFrame(boston.data)
data['target'] = boston.target
plt.plot(data[5], data['target'], '.')
plt.show()
# method 1
def reg(x, y, alpha, step, theta0, theta1):
m = len(x)
assert m == len(y)
for i in range(step):
temp = theta0 + theta1 * x - y
theta0 = theta0 - alpha / m * sum(temp)
theta1 = theta1 - alpha / m * sum(temp * x)
return theta0, theta1
def plot_data(x, y, theta0, theta1):
plt.plot(x, y, '.')
plt.plot(x, theta0 + theta1 * x)
plt.show()
theta0 = 0
theta1 = 0
theta0, theta1 = reg(data[5], data['target'], 0.02, 20000, theta0, theta1)
print theta0, theta1
plot_data(data[5], data['target'], theta0, theta1)
# method 2
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(data[5].values.reshape(-1, 1), data['target'].values.reshape(-1, 1))
y = model.predict(data[5].values.reshape(-1, 1))
plt.plot(data[5], data['target'], '.', label='data')
plt.plot(data[5], y, label='sklearn')
plt.plot(data[5], theta0 + data[5] * theta1, label='myself')
plt.legend()
plt.show()
model.score(data[5].values.reshape(-1, 1), data['target'].values.reshape(-1, 1))
print model.intercept_, model.coef_
# method 3
import tensorflow as tf
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
w = tf.Variable(0.0, name='weights')
b = tf.Variable(0.0, name='biases')
y = X * w + b
cost = tf.square(Y - y)
optim = tf.train.GradientDescentOptimizer(0.02).minimize(cost)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for j in range(200):
for i in range(100):
sess.run(optim, feed_dict={X: data[5].values[i], Y: data['target'].values[i]})
print sess.run(w)
print sess.run(b)
learning_rate = 0.02
training_epochs = 200
display_step = 50
# Training Data
train_X = data[5].values
train_y = data['target'].values
n_samples = train_X.shape[0]
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
W = tf.Variable(np.random.randn())
b = tf.Variable(np.random.randn())
activation = W * X + b
# activation = tf.add(tf.multiply(X,W), b)
cost = tf.square(Y - activation)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
for i in range(100):
sess.run(optimizer, feed_dict={X: train_X[i], Y: train_y[i]})
if epoch % display_step == 0:
print('Epoch:%d' % (epoch + 1), 'cost:',
'W:', sess.run(W), 'b:', sess.run(b))
print("Optimization Finished!")
print('Epoch:%d' % (epoch + 1),
'W:', sess.run(W), 'b:', sess.run(b))
plt.scatter(train_X, train_y, c='r', marker='o')
plt.plot(train_X, sess.run(W) * train_X + sess.run(b))
plt.show()