In [1]:
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
%matplotlib


Using matplotlib backend: Qt5Agg

In [2]:
def read_data(file_path,Delimiter=None):
    return np.genfromtxt(file_path,Delimiter)

def load_data():
    boston = load_boston()
    features,labels = np.array(boston.data),np.array(boston.target)
    return features,labels
    
def normalize(dataset):
    mu = np.mean(dataset,axis=0)
    sigma = np.std(dataset,axis=0)
    return (dataset-mu)/sigma

In [3]:
features,labels = load_data()
normalized_fetures = normalize(features)

In [4]:
def append_bias_reshape(features,labels):
    n_training_samples = features.shape[0]
    n_dim = features.shape[1]
    f = np.reshape(np.c_[np.ones(n_training_samples),features],[n_training_samples,n_dim + 1])
    l = np.reshape(labels,[n_training_samples,1])
    return f, l

In [5]:
f,l = append_bias_reshape(normalized_fetures,labels)

In [6]:
print(f.shape)
print(normalized_fetures.shape)


(506, 14)
(506, 13)

In [24]:
print(normalized_fetures[0])
print(f[0])


[-0.41771335  0.28482986 -1.2879095  -0.27259857 -0.14421743  0.41367189
 -0.12001342  0.1402136  -0.98284286 -0.66660821 -1.45900038  0.44105193
 -1.0755623 ]
[ 1.         -0.41771335  0.28482986 -1.2879095  -0.27259857 -0.14421743
  0.41367189 -0.12001342  0.1402136  -0.98284286 -0.66660821 -1.45900038
  0.44105193 -1.0755623 ]

In [7]:
n_dim = f.shape[1]
rnd_indices = np.random.rand(len(f)) < 0.80
train_x = f[rnd_indices]
train_y = l[rnd_indices]
test_x = f[~rnd_indices]
test_y = l[~rnd_indices]

In [18]:
learning_rate = 0.1
epochs = 10000
cost_history = np.empty(shape=[1],dtype=float)

X = tf.placeholder(tf.float32,[None,n_dim])
Y = tf.placeholder(tf.float32,[None,1])
W = tf.Variable(tf.ones([n_dim,1]))

init = tf.global_variables_initializer()

In [19]:
#machine learning algorithm for Linear Regression
y_ = tf.matmul(X,W)
cost = tf.reduce_mean(tf.square(y_-Y))
training_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

sess = tf.Session()
sess.run(init)

for epoch in range(epochs):
    sess.run(training_step,feed_dict= {X:train_x,Y:train_y})
    cost_history = np.append(cost_history,sess.run(cost,feed_dict={X:train_x,Y:train_y}))

In [20]:
plt.plot(range(len(cost_history)),cost_history)
plt.axis([0,epochs,0,np.max(cost_history)])
plt.show()

In [21]:
pred_y = sess.run(y_,feed_dict={X:test_x})
mse = tf.reduce_mean(tf.square(pred_y-test_y))
print("MSE: ",sess.run(mse))


('MSE: ', 27.603915939497512)

In [ ]: