#Importing the libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd

from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.callbacks import EarlyStopping, LambdaCallback

%matplotlib inline


import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))


df = pd.read_csv('/kaggle/input/real-estate-price-prediction/Real estate.csv') 
df.head()
df = df.iloc[:,1:]
df_norm = (df - df.mean()) / (df.max()-df.min())
df_norm.head()
df.info()
df_norm.corr()
df_norm["X1 transaction date"][100:150].plot()
#converting back the normalized price to real value of price

y_mean = df['Y house price of unit area'].mean()
y_std = df['Y house price of unit area'].std()

def convert_label_value(pred):
    return int(pred * y_std + y_mean)
#testing the function

print(convert_label_value(0.12))
"""
# Creating Training and Testing sets
"""
#input features

X = df_norm.iloc[:, :6]
X.head()
# y value

y = df_norm.iloc[:, -1]
y.head()
#Feature and label values

X_arr = X.values
y_arr = y.values

print('X_arr shape: ', X_arr.shape)
print('Y_arr shape: ', y_arr.shape)
X_arr[0:5]
y_arr[0:5]
X_train, X_test, y_train, y_test = train_test_split(X_arr, y_arr, test_size = 0.2, shuffle = True, random_state=7)

print('X_train shape: ', X_train.shape)
print('y_train shape: ', y_train.shape)
print('X_test shape: ', X_test.shape)
print('y_test shape: ', y_test.shape)
"""
# Create the model
"""
def get_model():
    
    model = Sequential([
        Dense(5, input_shape = (6,), activation = 'relu'),
        Dense(12, activation = 'sigmoid'),
        Dense(6, activation = 'relu'),
        Dense(1)
    ])

    model.compile(
        loss='mse',
        optimizer='adadelta'
    )
    
    return model

model = get_model()
model.summary()
"""
# Using the untrained model to get dummy predictions
"""
model = get_model()

preds_on_untrained = model.predict(X_test)
"""
# Training the model using Train data
"""
early_stopping = EarlyStopping(monitor='accuracy', patience = 5)


history = model.fit(
    X_train, y_train,
    validation_data = (X_test, y_test),
    epochs = 100,
    callbacks = [early_stopping]
)
"""
# Testing
"""
df_norm= df_norm.iloc[:,1:]
df_norm
#mean vales
mean_val=df.mean()
mean_val
std_val=df.std()
std_val
"""
# Taking Sample Values
"""
df.head(5)
#Take the year of sale

date=2012.500

date=(date- mean_val[0] )/ std_val[0]
#Take house age in years

age= 23

age= (age- mean_val[1] )/ std_val[1]
#Distance to nearest metro staion (in m)

mrt= 1200
mrt= (mrt- mean_val[2] )/ std_val[2]
#number of stores in the locality

stores=5

stores=(stores- mean_val[3] )/ std_val[3]
#latitude

latitude=24.97

latitude=(latitude- mean_val[4] )/ std_val[4]
#longitude

longitude=121.53

longitude=(longitude- mean_val[5] )/ std_val[5]
"""
# Test Prediction
"""
#numpy array

test_input= np.array( [[ date, age, mrt, stores, latitude, longitude]] )
#prediction

val= model.predict(test_input)
res=val[0][0]
#testing the function

print("The predicted price is=",convert_label_value(res))