Skip to content

Instantly share code, notes, and snippets.

@lawrencechen0921
Created September 23, 2019 13:47
Show Gist options
  • Save lawrencechen0921/ab7fa0d104d145d43a0a9ddb426a5e1d to your computer and use it in GitHub Desktop.
Save lawrencechen0921/ab7fa0d104d145d43a0a9ddb426a5e1d to your computer and use it in GitHub Desktop.
Machine Learning of Boston-House-Pricing pred
import tensorflow as tf
import keras
(x_train, y_train), (x_test, y_test) = keras.datasets.boston_housing.load_data()
%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams['font size'] = 10*3
plt.rcParams['figure.figsize'] = [18, 12]
plt.rcParams['font.family'] = ['IPAexGothic']
plt.hist(y_train, bins=20)
plt.xlabel('house_price ($1000 per unit)')
plt.ylabel('datasets')
plt.show()
plt.plot(x_train[:, 5], y_train, 'O')
plt.xlabel('the number of room')
plt.ylabel('house_price ($1000 per unit)')
x_train_mean = x_train.mean(axis=0)
x_train_std = x_train.std(axis=0)
y_train_mean = y_train.mean()
y_train_std = y_train.std()
x_train = (x_train - x_train_mean)/x_train_std
y_train = (y_train - y_train_mean)/y_train_std
#even x_test use standardization
x_test = (x_test - x_train_mean)/x_train_std
#even y_test use standardization
y_test = (y_test- y_train_mean)/y_train_std
plt.plot(x_train[:, 5]), y_train, '0')
plt.xlabel('the number of room(after standarization)')
plt.ylabel('house_price(after standarization)')
#explain variable placeholder
x = tf.placeholder(tf.float = 32, (None, 13), name='x')
#the right anwser of data
y = tf.placeholder(tf.float = 32, (None, 1), name='y')
#use weight mean
w = tf.variable(tf.random_normal((13, 1)))
pred = tf.matmul(x, w)
#loss function
loss = tf.reduce_mean((y-pred)**2)
optimizer = tf.train.GradientDescentOptimizer(
learning_rate=0.1
)
train_step = optimizer.minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(100):
train_loss, _ = sess.run(
[loss, train_step],
feed_dict={
x: x_train,
y: y_train.reshape((-1,1))
}
)
print('step:{}, train_loss:{}'. format(
step, train_loss
))
# after learning , evaluate the data
pred_ = sess.run(
pred,
feed_dict={
x: x_test
}
)
import numpy as np
def get_batches(x, y, batch_size):
n_data = len(x)
indices = np.arange(n_data)
np.random.shuffle(indices)
x_shuffled = x[indices]
y_shuffled = y[indices]
# collect data of original batch_size
for i in range(0, n_data, batch_size):
x_batch = x_shuffled[i: i+batch_size]
y_batch = y_shuffled[i: i+batch_size]
yield x_batch, y_batch
# batch_size
BATCH_SIZE = 32
step = 0
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# repeat 100 times
for epoch in range(100):
for x_batch, y_batch in get_batches(x_train, y_train, 32):
train_loss, _ =sess.run(
[loss, train_step],
feed_dict={
x: x_batch,
y: y_batch.reshape((-1, 1))
}
)
print('step:{}, train_loss:{}'.format(
step, train_loss
))
step +=1
pred_ = sess.run(
pred,
feed_dict={
x: x_test
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment