California Housing Data
This data set contains information about all the block groups in California from the 1990 Census. In this sample a block group on average includes 1425.5 individuals living in a geographically compact area.
The task is to aproximate the median house value of each block from the values of the rest of the variables.
It has been obtained from the LIACC repository. The original page where the data set can be found is: http://www.liaad.up.pt/~ltorgo/Regression/DataSets.html.
The Features:
Import the cal_housing_clean.csv file with pandas. Separate it into a training (70%) and testing set(30%).
In [1]:
import pandas as pd
In [2]:
df = pd.read_csv('./data/cal_housing_clean.csv')
In [3]:
df.head()
Out[3]:
In [4]:
df.describe().T
Out[4]:
In [5]:
y = df['medianHouseValue']
x = df.drop('medianHouseValue', axis = 1)
In [6]:
from sklearn.model_selection import train_test_split
In [7]:
X_train, X_test, Y_train, Y_test = train_test_split(x, y,
test_size = 0.3,
random_state = 7)
In [8]:
X_train.head()
Out[8]:
In [9]:
Y_train.head()
Out[9]:
In [10]:
from sklearn.preprocessing import MinMaxScaler
In [11]:
scaler = MinMaxScaler()
In [12]:
scaler.fit(X_train)
Out[12]:
In [13]:
# Keeping Pandas DataFrame format after re-scaling
X_train = pd.DataFrame(data = scaler.transform(X_train),
columns = X_train.columns,
index = X_train.index)
In [14]:
X_test = pd.DataFrame(data = scaler.transform(X_test),
columns = X_test.columns,
index = X_test.index)
In [15]:
X_train.head()
Out[15]:
In [16]:
df.columns
Out[16]:
In [17]:
import tensorflow as tf
In [18]:
age = tf.feature_column.numeric_column('housingMedianAge')
rooms = tf.feature_column.numeric_column('totalRooms')
bedrooms = tf.feature_column.numeric_column('totalBedrooms')
pop = tf.feature_column.numeric_column('population')
households = tf.feature_column.numeric_column('households')
income = tf.feature_column.numeric_column('medianIncome')
In [19]:
feature_columns = [age, rooms, bedrooms, pop, households, income]
Create the input function for the estimator object. (play around with batch_size and num_epochs)
In [20]:
input_feature_func = tf.estimator.inputs.pandas_input_fn(x = X_train,
y = Y_train,
batch_size = 10,
num_epochs = 1000,
shuffle = True)
Create the estimator model. Use a DNNRegressor. Play around with the hidden units!
In [21]:
dnn_model = tf.estimator.DNNRegressor(hidden_units = [6, 5, 5], feature_columns = feature_columns)
In [22]:
dnn_model.train(input_fn = input_feature_func, steps = 5000)
Out[22]:
Create a prediction input function and then use the .predict method off your estimator model to create a list or predictions on your test data.
In [23]:
prediction_input_func = tf.estimator.inputs.pandas_input_fn(x = X_test,
batch_size = 10,
num_epochs = 1,
shuffle = False)
In [24]:
prediction_generator = dnn_model.predict(prediction_input_func)
In [25]:
precitions = list(prediction_generator)
Calculate the RMSE. You should be able to get around 100,000 RMSE (remember that this is in the same units as the label.) Do this manually or use sklearn.metrics
In [26]:
final_pred = []
for pred in precitions:
final_pred.append(pred['predictions'])
In [27]:
from sklearn.metrics import mean_squared_error, mean_absolute_error
In [28]:
# RMSE = sqrt(MSE) = MSE ** 0.5
mean_squared_error(Y_test, final_pred) ** 0.5
Out[28]: