This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Now to predict values we do the following | |
pred_input_func=tf.estimator.inputs.pandas_input_fn(x=X_eval,y=y_eval,batch_size=10,num_epochs=1,shuffle=False) | |
preds=model.predict(input_fn=pred_input_func) | |
predictions=list(preds) | |
final_pred=[] | |
for pred in predictions: | |
final_pred.append(pred["predictions"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Evaluating the model | |
train_metrics=model.evaluate(input_fn=input_func,steps=1000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Training the model | |
model.train(input_fn=input_func,steps=1000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#The estimator model | |
model=tf.estimator.DNNRegressor(hidden_units=[6,10,6],feature_columns=feat_cols) | |
#the input function | |
input_func=tf.estimator.inputs.pandas_input_fn(X_train,y_train,batch_size=10,num_epochs=1000,shuffle=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Creating Feature Columns | |
feat_cols=[] | |
for cols in df.columns[:-1]: | |
column=tf.feature_column.numeric_column(cols) | |
feat_cols.append(column) | |
print(feat_cols) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scaler_model = MinMaxScaler() | |
scaler_model.fit(X_train) | |
X_train=pd.DataFrame(scaler_model.transform(X_train),columns=X_train.columns,index=X_train.index) | |
scaler_model.fit(X_eval) | |
X_eval=pd.DataFrame(scaler_model.transform(X_eval),columns=X_eval.columns,index=X_eval.index) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
y_val= df["medianHouseValue"] | |
x_data=df.drop("medianHouseValue",axis=1) | |
X_train, X_eval,y_train,y_eval=train_test_split(x_data,y_val,test_size=0.3,random_state=101) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import tensorflow as tf | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import MinMaxScaler | |
df=pd.read_csv("cal_housing_clean.csv") | |
print(df.describe()) #to understand the dataset |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Mon Dec 04 17:59:48 2017 | |
@author: Tathagat Dasgupta | |
""" | |
# -*- coding: utf-8 -*- | |
""" | |
Created on Sat Dec 02 23:56:30 2017 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###Training the model | |
#Run the training function per mini-batches. | |
n_examples = X_train.shape[0] | |
n_batches = n_examples / batch_size | |
epochs=50 | |
for epoch in xrange(epochs): | |
for batch in xrange(n_batches): | |
x_batch = X_train[batch*batch_size: (batch+1) * batch_size] | |
y_batch = y_train[batch*batch_size: (batch+1) * batch_size] |