<jupyter_start><jupyter_text>1. Import Libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
import seaborn as sns
sns.set(color_codes = True)
%matplotlib inline
pd.pandas.set_option('display.max_columns', None)<jupyter_output><empty_output><jupyter_text>2. Import Data<jupyter_code>categorical = pd.read_csv("housing.csv",usecols=["MSZoning",'Street','Alley','LotShape','LandContour','Utilities','LotConfig','LandSlope','Neighborhood','Condition1','Condition2',
                                                'BldgType','HouseStyle','RoofStyle','RoofMatl','Exterior1st','Exterior2nd','MasVnrType','ExterQual','ExterCond','Foundation',
                                                 'BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','Heating','HeatingQC','CentralAir','Electrical','KitchenQual',
                                                'Functional','FireplaceQu','GarageType','GarageFinish','GarageQual','GarageCond','PavedDrive','PoolQC','Fence','MiscFeature',
                                                'SaleType','SaleCondition','SalePrice'])
categorical.head()<jupyter_output><empty_output><jupyter_text>3. Data Cleaning and Visualization<jupyter_code>for i in categorical.columns:
    print (i+": "+str(categorical[i].isna().sum()))
categorical.shape
categorical = categorical.drop("Alley",axis = 1)
categorical = categorical.drop('Fence',axis = 1)
categorical = categorical.drop('PoolQC',axis = 1)
categorical = categorical.drop('MiscFeature',axis = 1)
categorical = categorical.drop('FireplaceQu',axis = 1)

categorical.head()
categorical['MasVnrType'].fillna('missing',inplace = True)
categorical['MasVnrType'].value_counts()
categorical['MasVnrType'] = categorical.MasVnrType.replace('None','missing')
categorical['MasVnrType'].value_counts()
categorical['BsmtCond'].fillna('missing',inplace = True)
categorical['Electrical'].fillna('missing',inplace = True)
categorical['BsmtQual'].fillna('missing',inplace = True)
categorical['BsmtExposure'].fillna('missing',inplace = True)
categorical['BsmtFinType1'].fillna('missing',inplace = True)
categorical['BsmtFinType2'].fillna('missing',inplace = True)
categorical['GarageType'].fillna('missing',inplace = True)
categorical['GarageFinish'].fillna('missing',inplace = True)
categorical['GarageQual'].fillna('missing',inplace = True)
categorical['GarageCond'].fillna('missing',inplace = True)
for i in categorical.columns:
    print (i+": "+str(categorical[i].isna().sum()))
categorical = categorical.drop('SalePrice',axis = 1)
categorical = pd.get_dummies(categorical,drop_first = True)
categorical.head()
data = pd.read_csv('housing.csv')
data.head()
data = data.drop(["MSZoning",'Street','Alley','LotShape','LandContour','Utilities','LotConfig','LandSlope','Neighborhood','Condition1','Condition2',
                                                'BldgType','HouseStyle','RoofStyle','RoofMatl','Exterior1st','Exterior2nd','MasVnrType','ExterQual','ExterCond','Foundation',
                                                 'BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','Heating','HeatingQC','CentralAir','Electrical','KitchenQual',
                                                'Functional','FireplaceQu','GarageType','GarageFinish','GarageQual','GarageCond','PavedDrive','PoolQC','Fence','MiscFeature',
                                                'SaleType','SaleCondition','Id'],axis = 1)
data.shape
data.head()
for i in data.columns:
    print (i+": "+str(data[i].isna().sum()))
data['GarageYrBlt'] = data['GarageYrBlt'].fillna(data['GarageYrBlt'].median())
data['MasVnrArea'] = data['MasVnrArea'].fillna(data['MasVnrArea'].median())
data['LotFrontage'] = data['LotFrontage'].fillna(data['LotFrontage'].median())
## Temporal Variables (Date Time Variables)

for feature in ['YearBuilt','YearRemodAdd','GarageYrBlt']:
       
    data[feature]=data['YrSold']-data[feature]
data[['YearBuilt','YearRemodAdd','GarageYrBlt']].head()
dataset = pd.merge(categorical,data,left_index = True,right_index = True)
dataset
scaler = dataset.iloc[:,:-1]
scaler
from sklearn.preprocessing import StandardScaler


std = StandardScaler()

x_std = std.fit_transform(scaler)
x_std.shape
y = dataset.loc[:,["SalePrice"]]
y
from sklearn.model_selection import train_test_split


# create train and test set data
x_train, x_test, y_train, y_test = train_test_split(x_std,y,test_size=0.20,random_state=21)

print('Shape of Training Xs:{}'.format(x_train.shape))
print('Shape of Test Xs:{}'.format(x_test.shape))
from sklearn.linear_model import LinearRegression,SGDRegressor,BayesianRidge
from sklearn.linear_model import LogisticRegression,SGDClassifier
from sklearn.linear_model import RidgeClassifier,Ridge,Lasso,LassoLars<jupyter_output><empty_output><jupyter_text>4.Feature Selection<jupyter_code>from sklearn.linear_model import Lasso
from sklearn.feature_selection import SelectFromModel
model=SelectFromModel(Lasso(alpha=0.005,random_state=0))
model.fit(x_train,y_train)
model.get_support()<jupyter_output><empty_output><jupyter_text>5.Build Model<jupyter_code>from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier, export_graphviz,DecisionTreeRegressor
clf = DecisionTreeRegressor()
from sklearn.model_selection import RandomizedSearchCV
#Randomized Search CV

criterion= ["mse", "friedman_mse", "mae", "poisson"]
# Number of features to consider at every split
max_features = ['auto', 'sqrt']
splitter = ["best", "random"]
# Maximum number of levels in tree
max_depth = [int(x) for x in np.linspace(5, 30, num = 6)]
# max_depth.append(None)
# Minimum number of samples required to split a node
min_samples_split = [2, 5, 10, 15, 100]
# Minimum number of samples required at each leaf node
min_samples_leaf = [1, 2, 5, 10]

# Create the random grid
random_grid = {'criterion': criterion,
               'max_features': max_features,
               'splitter':splitter,
               'max_depth': max_depth,
               'min_samples_split': min_samples_split,
               'min_samples_leaf': min_samples_leaf}

print(random_grid)

# Random search of parameters, using 3 fold cross validation, 
# search across 50 different combinations
rf_random = RandomizedSearchCV(estimator = clf, param_distributions = random_grid,scoring='neg_mean_squared_error', n_iter = 50, cv = 3, verbose=2, random_state=42, n_jobs = 1)
rf_random.fit(x_train,y_train)
print(rf_random.best_params_)
print(rf_random.best_score_)
best_params = DecisionTreeRegressor(splitter= 'best', min_samples_split= 5, min_samples_leaf= 10, max_features= 'auto', max_depth= 25, criterion = 'friedman_mse')

best_params.fit(x_train,y_train)
score = best_params.score(x_test,y_test)
print(score)
clf = Ridge()
parameters={'alpha':[1e-15,1e-10,1e-8,1e-3,1e-2,1,5,10,20,30,35,40],
           'max_iter':[1000,10000,2000,20000,250000,30000,350000,400000]}
ridge_regressor=GridSearchCV(clf,parameters,scoring='neg_mean_squared_error',cv=5)

ridge_regressor.fit(x_train,y_train)

ridge_regressor.best_params_
clf = Ridge(alpha = 40)

clf.fit(x_train,y_train)
y_predicted = clf.predict(x_test)
score = clf.score(x_train,y_train)
print(score)<jupyter_output>0.9207301500792402
{'fraction_non_alphanumeric': 0.14623032311516157, 'fraction_numerical': 0.02140292736813035, 'mean_word_length': 5.858901515151516, 'pattern_counts': {'":': 3, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '286711', 'license': 'MIT', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2388, 'n_words': 367, 'path': 'House Price prediction.ipynb', 'repo_name': 'kedar750/House-Price-Prediction', 'size': '7242'}