Kaggle competition: House Prices - Advanced Regression Techniques
xtrain,xvalid,ytrain,yvalid=train_test_split(train,target,test_size=0.25)
 models = {"KNN": KNeighborsClassifier() ,
"Logistic Regression": LogisticRegression(max_iter=10000),
"Random Forest": RandomForestClassifier() ,
"SVC" : SVC(probability=True),
"DecisionTreeClassifier" : DecisionTreeClassifier() ,
"AdaBoostClassifier" : AdaBoostClassifier(algorithm='SAMME', base_estimator=DecisionTreeClassifier() ,
learning_rate=1.5, n_estimators=2, random_state=7),
"GradientBoostingClassifier" : GradientBoostingClassifier(max_depth=4, max_features=0.3, min_samples_leaf=100,
n_estimators=300),
"GaussianNB" : GaussianNB() ,
"LinearDiscriminantAnalysis" : LinearDiscriminantAnalysis() ,
"QuadraticDiscriminantAnalysis" : QuadraticDiscriminantAnalysis() }
scores={}
cv=RepeatedStratifiedKFold(n_splits=10,n_repeats=3)
 np.random.seed(42)
 model_scores = {}
for name, model in models.items() :
score=cross_val_score(model,train,target,cv=cv,scoring='accuracy',n_jobs=-1)
 scores[name]=np.mean(score)
 print(scores)
<choose_model_class>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from lightgbm import LGBMRegressor
from xgboost import XGBRegressor
import sklearn.metrics as metrics
import math
a=GradientBoostingClassifier()
a.fit(train,target)
 b=a.predict(test)
 sub['Survived']=b<save_to_csv>
sample_submission = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/sample_submission.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 c_test = test.copy()
c_train = train.copy()
sub.to_csv('ver3.csv',index=False )<load_from_csv>
c_train['train'] = 1
c_test['train'] = 0
df = pd.concat([c_train, c_test], axis=0,sort=False )
train = pd.read_csv('.. /input/titanic/train.csv' )<prepare_x_and_y>
NAN = [(c, df[c].isna().mean() *100)for c in df]
NAN = pd.DataFrame(NAN, columns=["column_name", "percentage"] )
target_class = 'Survived'
x = train.drop(target_class,axis=1)
 y = train[target_class]<prepare_output>
NAN = NAN[NAN.percentage > 50]
NAN.sort_values("percentage", ascending=False )
es = ft.EntitySet(id = 'Titanic')
 es.entity_from_dataframe(entity_id = 'data', dataframe = x,
make_index = True, index = 'index')
 feature_matrix, feature_defs = ft.dfs(entityset = es, target_entity = 'data',
trans_primitives = ['add_numeric', 'multiply_numeric'])
 feature_matrix.head()<install_modules>
df = df.drop(['Alley','PoolQC','Fence','MiscFeature'],axis=1 )
pip install evalml 
<import_modules>
null_counts = object_columns_df.isnull().sum()
print("Number of null values in each column:
{}".format(null_counts))
import evalml
from evalml import AutoMLSearch<split>
columns_None = ['BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','GarageType','GarageFinish','GarageQual','FireplaceQu','GarageCond']
object_columns_df[columns_None]= object_columns_df[columns_None].fillna('None' )
X_train, X_holdout, y_train, y_holdout = evalml.preprocessing.split_data(x, y, test_size=0.2, random_state=0)
<choose_model_class>
columns_with_lowNA = ['MSZoning','Utilities','Exterior1st','Exterior2nd','MasVnrType','Electrical','KitchenQual','Functional','SaleType']
object_columns_df[columns_with_lowNA] = object_columns_df[columns_with_lowNA].fillna(object_columns_df.mode().iloc[0] )
automl = AutoMLSearch(problem_type='binary',
objective='auto',
additional_objectives=["accuracy binary"],
max_batches=1,
ensembling=True,
optimize_thresholds=True)
<compute_test_metric>
null_counts = numerical_columns_df.isnull().sum()
print("Number of null values in each column:
{}".format(null_counts))
automl.search(X_train, y_train )<find_best_score>
numerical_columns_df['GarageYrBlt'] = numerical_columns_df['GarageYrBlt'].fillna(numerical_columns_df['YrSold']-35)
 numerical_columns_df['LotFrontage'] = numerical_columns_df['LotFrontage'].fillna(68 )
automl.rankings<find_best_params>
object_columns_df = object_columns_df.drop(['Heating','RoofMatl','Condition2','Street','Utilities'],axis=1 )
best_pipeline = automl.best_pipeline<train_model>
Negatif = numerical_columns_df[numerical_columns_df['Age_House'] < 0]
Negatif
best_pipeline.fit(X_train, y_train )<compute_test_metric>
numerical_columns_df['TotalBsmtBath'] = numerical_columns_df['BsmtFullBath'] + numerical_columns_df['BsmtFullBath']*0.5
numerical_columns_df['TotalBath'] = numerical_columns_df['FullBath'] + numerical_columns_df['HalfBath']*0.5
numerical_columns_df['TotalSA']=numerical_columns_df['TotalBsmtSF'] + numerical_columns_df['1stFlrSF'] + numerical_columns_df['2ndFlrSF']
best_pipeline.score(X_holdout, y_holdout, objectives=[ "accuracy binary"] )<compute_train_metric>
bin_map = {'TA':2,'Gd':3, 'Fa':1,'Ex':4,'Po':1,'None':0,'Y':1,'N':0,'Reg':3,'IR1':2,'IR2':1,'IR3':0,"None" : 0,
"No" : 2, "Mn" : 2, "Av": 3,"Gd" : 4,"Unf" : 1, "LwQ": 2, "Rec" : 3,"BLQ" : 4, "ALQ" : 5, "GLQ" : 6
}
object_columns_df['ExterQual'] = object_columns_df['ExterQual'].map(bin_map)
 object_columns_df['ExterCond'] = object_columns_df['ExterCond'].map(bin_map)
 object_columns_df['BsmtCond'] = object_columns_df['BsmtCond'].map(bin_map)
 object_columns_df['BsmtQual'] = object_columns_df['BsmtQual'].map(bin_map)
 object_columns_df['HeatingQC'] = object_columns_df['HeatingQC'].map(bin_map)
 object_columns_df['KitchenQual'] = object_columns_df['KitchenQual'].map(bin_map)
 object_columns_df['FireplaceQu'] = object_columns_df['FireplaceQu'].map(bin_map)
 object_columns_df['GarageQual'] = object_columns_df['GarageQual'].map(bin_map)
 object_columns_df['GarageCond'] = object_columns_df['GarageCond'].map(bin_map)
 object_columns_df['CentralAir'] = object_columns_df['CentralAir'].map(bin_map)
 object_columns_df['LotShape'] = object_columns_df['LotShape'].map(bin_map)
 object_columns_df['BsmtExposure'] = object_columns_df['BsmtExposure'].map(bin_map)
 object_columns_df['BsmtFinType1'] = object_columns_df['BsmtFinType1'].map(bin_map)
 object_columns_df['BsmtFinType2'] = object_columns_df['BsmtFinType2'].map(bin_map)
 PavedDrive = {"N" : 0, "P" : 1, "Y" : 2}
object_columns_df['PavedDrive'] = object_columns_df['PavedDrive'].map(PavedDrive )
permutation_importance = evalml.model_understanding.calculate_permutation_importance(best_pipeline,X_holdout, y_holdout, objective= "accuracy binary" )<load_from_csv>
rest_object_columns = object_columns_df.select_dtypes(include=['object'])
 object_columns_df = pd.get_dummies(object_columns_df, columns=rest_object_columns.columns )
test = pd.read_csv('.. /input/titanic/test.csv' )<create_dataframe>
df_final = pd.concat([object_columns_df, numerical_columns_df], axis=1,sort=False)
 df_final.head()
test_es = ft.EntitySet(id = 'Titanic_test')
 test_es.entity_from_dataframe(entity_id = 'data', dataframe = test,
make_index = True, index = 'index' )<compute_test_metric>
df_final = df_final.drop(['Id',],axis=1)
 df_train = df_final[df_final['train'] == 1]
df_train = df_train.drop(['train',],axis=1)
 df_test = df_final[df_final['train'] == 0]
df_test = df_test.drop(['SalePrice'],axis=1)
 df_test = df_test.drop(['train',],axis=1 )
test_feature = ft.calculate_feature_matrix(feature_defs, test_es)
<predict_on_test>
target= df_train['SalePrice']
df_train = df_train.drop(['SalePrice'],axis=1 )
ypred = best_pipeline.predict(test ).astype(int )<save_to_csv>
x_train,x_test,y_train,y_test = train_test_split(df_train,target,test_size=0.33,random_state=0 )
predictions = pd.DataFrame(ypred, columns = ['Survived'])
 submission = pd.concat([test.PassengerId,predictions],axis=1)
 submission.to_csv('submission.csv',index=False )<import_modules>
xgb =XGBRegressor(booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=0.6, gamma=0,
importance_type='gain', learning_rate=0.01, max_delta_step=0,
max_depth=4, min_child_weight=1.5, n_estimators=2400,
n_jobs=1, nthread=None, objective='reg:linear',
reg_alpha=0.6, reg_lambda=0.6, scale_pos_weight=1,
silent=None, subsample=0.8, verbosity=1)
 lgbm = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=12000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.4,
)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.pipeline import Pipeline
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import LabelEncoder,MinMaxScaler
from sklearn.ensemble import GradientBoostingClassifier<load_from_csv>
xgb.fit(x_train, y_train)
 lgbm.fit(x_train, y_train,eval_metric='rmse' )
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 target=train['Survived']
submit=pd.DataFrame(test['PassengerId'] )<count_missing_values>
predict1 = xgb.predict(x_test)
 predict = lgbm.predict(x_test )
print(train.isnull().sum())
 train['Age'].fillna(train['Age'].mean() ,inplace=True)
 train['Embarked'].fillna('S',inplace=True)
 train.drop('Cabin',axis=1,inplace=True)
 print(train.isnull().sum() )<count_missing_values>
print('Root Mean Square Error test = ' + str(math.sqrt(metrics.mean_squared_error(y_test, predict1))))
print('Root Mean Square Error test = ' + str(math.sqrt(metrics.mean_squared_error(y_test, predict))))
print(test.isnull().sum())
 test['Age'].fillna(test['Age'].mean() ,inplace=True)
 test['Fare'].fillna(test['Fare'].median() ,inplace=True)
 test.drop('Cabin',axis=1,inplace=True)
 print(test.isnull().sum() )<feature_engineering>
xgb.fit(df_train, target)
 lgbm.fit(df_train, target,eval_metric='rmse' )
title=[]
for i in range(len(train['Name'])) :
a=train.loc[i,'Name'].split() [1]
title.append(a)
 title=pd.Series(title)
 train['Title']=title
title1=[]
for i in range(len(test['Name'])) :
a=test.loc[i,'Name'].split() [1]
title1.append(a)
 title1=pd.Series(title1)
 test['Title']=title1
train['Passenger']=train['SibSp']+train['Parch']+1
test['Passenger']=test['SibSp']+test['Parch']+1<categorify>
predict4 = lgbm.predict(df_test)
 predict3 = xgb.predict(df_test)
 predict_y =(predict3*0.45 + predict4 * 0.55 )
encode=LabelEncoder()
train['Sex']=encode.fit_transform(train['Sex'])
 test['Sex']=encode.fit_transform(test['Sex'])
 train['Title']=encode.fit_transform(train['Title'])
 test['Title']=encode.fit_transform(test['Title'])
 train['Embarked']=encode.fit_transform(train['Embarked'])
 test['Embarked']=encode.fit_transform(test['Embarked'] )<feature_engineering>
submission = pd.DataFrame({
"Id": test["Id"],
"SalePrice": predict_y
})
 submission.to_csv('submission.csv', index=False )