用tensorflow写一个简单的线性回归示例

代码如下:

 1 #Linear Regression
 2 import numpy as np
 3 import tensorflow as tf
 4 from sklearn.datasets import fetch_california_housing 
 5 
 6 
 7 housing = fetch_california_housing()
 8 m, n = housing.data.shape
 9 housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]#merge
10 
11 X = tf.constant(housing_data_plus_bias, dtype = tf.float32, name = 'X')
12 y = tf.constant(housing.target.reshape(-1,1), dtype = tf.float32, name = 'y')
13 XT = tf.transpose(X)
14 theta = tf.matmul(tf.matmul(tf.matrix_inverse(tf.matmul(XT, X)), XT), y)
15 
16 with tf.Session() as sess:
17     theta_value = theta.eval()

公式是这个样子的:

猜你喜欢

转载自www.cnblogs.com/bigstrawberrywakaka/p/9321279.html