<jupyter_start><jupyter_text>IMPORT<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
sns.set()
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import Ridge
df = pd.read_csv("Real estate.csv")
df.head()
X=df.iloc[:,2:-1]
X.head()
y = df.iloc[:,[-1]]
y.head()
X_train,X_val,y_train, y_val = train_test_split(X, y, test_size = 0.25,random_state=0)
print(X_train.shape,X_val.shape)
y_train, y_val = y_train.to_numpy(), y_val.to_numpy()
y_train.shape<jupyter_output><empty_output><jupyter_text>Adding Polynomial Features<jupyter_code>degree = 5
poly = PolynomialFeatures(degree)
X_train_poly = poly.fit_transform(X_train)
X_val_poly = poly.transform(X_val)
print(X_train_poly.shape)<jupyter_output>(310, 252)<jupyter_text>Feature Scaling<jupyter_code>sc = StandardScaler()
sc.fit(X_train_poly)
X_train_scaled = sc.transform(X_train_poly)
X_val_scaled = sc.transform(X_val_poly)
X_train_scaled[:5]<jupyter_output><empty_output><jupyter_text>TRAINING<jupyter_code>model = Ridge(alpha=10)
model.fit(X_train_scaled,y_train)
train_score = model.score(X_train_scaled,y_train)
print("Train Score: ",train_score)
val_score = model.score(X_val_scaled,y_val)
print("Val Score: ",val_score)<jupyter_output>Val Score:  0.7110214795994969<jupyter_text>Hyparameter Tuning<jupyter_code>params={}
params['degrees']=[1,2,3,4,5,6,7,8,9,10]
params['alpha'] = [0.01,0.1,10,50]
def fit(X_train_scaled,y_train,X_val_scaled,y_val,degree,alpha,grid_search=False,exp_name=None):
    poly = PolynomialFeatures(degree)
    X_train_poly = poly.fit_transform(X_train)
    X_val_poly = poly.transform(X_val)

    sc = StandardScaler()
    sc.fit(X_train_poly)
    X_train_scaled = sc.transform(X_train_poly)
    X_val_scaled = sc.transform(X_val_poly)

    model = Ridge(alpha)
    model.fit(X_train_scaled,y_train)

    train_score = model.score(X_train_scaled,y_train)
    val_score = model.score(X_val_scaled,y_val)
 
    if grid_search:
        scores = [exp_name, degree, alpha, train_score, val_score]
        return scores
    else:
        print('Train Score: ',train_score)
        print('Val Score: ',val_score)
        return model,poly,sc
from tqdm.notebook import tqdm
result=[]
exp_id = 1
for i in tqdm(range(len(params['degrees']))):
    degree = params['degrees'][i]
    for j in range(len(params['alpha'])):
        alpha = params['alpha'][j]
        exp_name='exp '+str(exp_id)
        exp_id+=1
        result.append(fit(X_train_scaled,y_train,X_val_scaled,y_val,degree,alpha,grid_search=True,exp_name=exp_name))
result
result_df=pd.DataFrame(result,columns=['Experiment_id','Degree','Alpha','Train Score','Val Score'])
result_df
plt.figure(figsize=(10,5))
plt.title("R2 Score")
plt.plot(result_df['Train Score'],marker='o',label = 'Train Score')
plt.plot(result_df['Val Score'],marker='o', label = 'Val Score')
plt.xticks(list(range(len(result_df))),result_df['Experiment_id'],rotation=90)
plt.ylabel('Score')
plt.ylim((0.5,0.9))
plt.legend()
plt.show()
result_df.sort_values(by='Val Score', ascending=False)
result_df.loc[result_df['Experiment_id'] == 'exp 31']<jupyter_output><empty_output><jupyter_text>Training the model on selected hyperparameters<jupyter_code>best_model,poly,sc = fit(X_train_scaled,y_train,X_val_scaled,y_val,degree=8,alpha=10)<jupyter_output>Train Score:  0.6942452365630836
Val Score:  0.7280057578234933<jupyter_text>Saving and Loading the model<jupyter_code>import joblib
joblib.dump(best_model, 'best_model')
joblib.dump(poly, 'polynomial feature')
joblib.dump(sc, 'scaler')
loaded_model = joblib.load('best_model')
poly = joblib.load('polynomial feature')
sc = joblib.load('scaler')<jupyter_output><empty_output><jupyter_text>Deploying<jupyter_code>def prediction(model,poly,sc):
    l=[]
    l.append(float(input('Enter House Age: ')))
    l.append(float(input('distance to the nearest MRT station: ')))
    l.append(float(input('number of convenience stores: ')))
    l.append(float(input('Latitude: ')))
    l.append(float(input('Longitude: ')))
    arr = np.asarray([l])
    arr = poly.transform(arr)
    scaled_arr = sc.transform(arr)
    print('Price of the house per unit area: ', round(model.predict(scaled_arr)[0][0],2))
# 19.5 	306.59470 	9 	24.98034 	121.53951 	42.2
prediction(loaded_model,poly,sc)<jupyter_output>Enter House Age: 19.5
distance to the nearest MRT station: 306.59470
number of convenience stores: 9
Latitude: 24.98034
Longitude: 121.53951
Price of the house per unit area:  45.19
{'fraction_non_alphanumeric': 0.13863096523429064, 'fraction_numerical': 0.03649319801338804, 'mean_word_length': 6.771812080536913, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '340759', 'license': 'MIT', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1776, 'n_words': 280, 'path': 'House_Price_Prediction.ipynb', 'repo_name': 'jcestari/House_Price_Prediction_---', 'size': '4631'}