<jupyter_start><jupyter_text>Homework 02<NAME><jupyter_code>import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.preprocessing import scale
%matplotlib inline
df = pd.read_csv('./Data/Boston_home_prices.csv')
df.head()
print(df.shape)
df.dtypes
features = df.values[:,:13].astype(np.float64)
medv = df.medv.values.astype(np.float64)

features_s = scale(features)
medv_s = scale(medv)

print(features_s.shape)
print(medv_s.shape)
# Tensorflow

# y = wx + b
x = tf.constant(features_s, dtype='float32', shape=[506, 13])
y = tf.constant(medv_s, dtype='float32', shape=[506, 1])

w = tf.Variable(tf.truncated_normal([13, 1], stddev=1))
b = tf.Variable(0.0)

# error loss function
MSE = tf.reduce_mean(tf.square(tf.matmul(x, w) + b - y))

# define optimizer
STEPSIZE = 0.1
optimizer = tf.train.GradientDescentOptimizer(STEPSIZE).minimize(MSE)

# prediction with current weights
y_pred = tf.matmul(x, w) + b

# init all variables
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
MAXSTEPS = 1000
for step in range(MAXSTEPS + 1):
    (_, mse, w0, b0) = sess.run([optimizer, MSE, w, b])
    if (step % 100) == 0:
        print('step = %-5d MSE = %-10f' % (step, mse))
print('done!')
# make predictions
pred_s = sess.run(y_pred)

# unstandardize weight predictions
pred = pred_s * medv.std() + medv.mean()

# create new prediction column in dataframe df
df['medv_pred'] = pred
df.head()
# compute mean square error
mse = ((medv - pred.flatten()) ** 2).mean()
print('MSE = ', mse)
print('RMSE = ', np.sqrt(mse))<jupyter_output>MSE =  21.8948311944
RMSE =  4.67919129705<jupyter_text>Check using sklearn<jupyter_code>from sklearn.linear_model import LinearRegression
LR = LinearRegression()
LR.fit(features, medv)
pred_LR = LR.predict(features)
# compute mean square error
mse = ((medv - pred_LR.flatten()) ** 2).mean()
print('MSE = ', mse)
print('RMSE = ', np.sqrt(mse))
plt.scatter(medv, pred_LR, alpha=0.1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1546134663341646, 'fraction_numerical': 0.03541147132169576, 'mean_word_length': 5.172307692307692, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '240368', 'license': 'MIT', 'n_tokens_mistral': 840, 'n_tokens_neox': 780, 'n_words': 175, 'path': 'Homework 02.ipynb', 'repo_name': 'derrowap/MA490-Deep-Learning', 'size': '2019'}