To predict house prices, those were sold more than median price using keras.
House price data set(CSV file).
Precision, recall and f-measure.
Accuracy score.
Load the data set.
Fix independent and dependent variables.
Split the data into train and test.
Build the deep learning model.
Fit train data.
Print the results.
#import necessary libraries
import warnings
warnings.filterwarnings(“ignore”)
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from keras.utils import np_utils
from sklearn.metrics import classification_report, confusion_matrix
#load the data set
data = pd.read_csv(‘/home/soft50/soft50/Sathish/practice/housepricedata.csv’)
#Make it as a data frame
df = pd.DataFrame(data)
#shape of data
print(“Rows and columns of data\n”,df.shape)
print(“\n”)
#feature selection
X = df.iloc[:,0:10]
y = df.iloc[:,10]
#Split the data into train and testing
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.1, random_state=0)
#Print training data
print(“Training data\n”,X_train,”\n”,Y_train)
print(“\n\n”)
#Print testing data
print(“Testing data\n”,X_test)
print(“\n\n”)
#make dependent variable categorical
Y_train = np_utils.to_categorical(Y_train,num_classes=2)
Y_test = np_utils.to_categorical(Y_test,num_classes=2)
batch_size = 32
#shape of input
n_cols_2 = X_train.shape[1]
#create deep neural networks
model_2 = Sequential()
#add layers to model
model_2.add(Dense(32, activation=’relu’, input_shape=(n_cols_2,)))
model_2.add(Dense(36, activation=’relu’))
model_2.add(Dense(20, activation=’relu’))
model_2.add(Dense(2, activation=’sigmoid’))
#Compile the model
model_2.compile(optimizer=’adam’, loss=’binary_crossentropy’, metrics=[‘accuracy’])
#Here we train the Network.
start_time = time.time()
model_2.fit(X_train, Y_train,validation_data=(X_test,Y_test), batch_size = batch_size, epochs = 10, verbose = 1)
end_time = time.time()
elapsed_time = end_time – start_time
print(“Time to train model: %.3f seconds” % elapsed_time)
#Evaluate the network
start_time = time.time()
score,acc = model_2.evaluate(X_test,Y_test,verbose = 1)
print(acc)
end_time = time.time()
elapsed_time = end_time – start_time
print(“Time to evaluate model: %.3f seconds” % elapsed_time)
print(“\n”)
#Predict the test results
prediction = model_2.predict(X_test)
length = len(prediction)
y_label = np.argmax(Y_test,axis=1)
predict_label = np.argmax(prediction,axis=1)
#classification report
print(“Confusion Matrix\n”,confusion_matrix(y_label,predict_label))
print(“\n”)
print(“Classification Report\n”,classification_report(y_label,predict_label))
print(“\n”)
accuracy = np.sum(y_label==predict_label)/length * 100
print(“Accuracy : “,accuracy)