def linear_regression(x_arr, y_arr, lam=0.2): x_mat = np.mat(x_arr).T y_mat =
np.mat(y_arr).T x_tx = x_mat.T * x_mat denom = x_tx +
np.eye(np.shape(x_mat)[1]) * lam # if lam == 0.0 if np.linalg.det(denom) ==
0.0: print('This matrix is singular, cannot do inverse') return ws = denom.I *
(x_mat.T * y_mat) return ws

if __name__ == '__main__': x_vals = np.linspace(0, 1, 1000) y_vals = x_vals +
np.random.normal(0, 1, 1000) ws = linear_regression(x_vals, y_vals) predict =
20 * ws print(predict.A[0][0])

19.690649736617942

import numpy as np import tensorflow as tf learning_rate = 0.05 batch_size =
50 x_vals = np.linspace(0, 1, 1000) y_vals = x_vals + np.random.normal(0, 1,
1000) x_vals.resize((x_vals.shape[0], 1)) y_vals.resize((y_vals.shape[0], 1))

sess = tf.Session() x_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32) w =
tf.Variable(tf.random_normal([1, 1])) model_output = tf.matmul(x_data, w)

loss = tf.reduce_mean(tf.square(y_target - model_output)) init =
tf.global_variables_initializer() sess.run(init) my_opt =
my_opt.minimize(loss)

for i in range(500): rand_index = np.random.choice(len(x_vals),
size=batch_size) rand_x = x_vals[rand_index] rand_y = y_vals[rand_index]
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y}) [k] =
sess.run(w) predict = 20 * k print(predict[0])

19.260855