Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<count_missing_values>
%matplotlib inline
print(df.isnull().sum() )<feature_engineering>
train_data = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test_data = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 train_data.shape,test_data.shape
Xtest.loc[:,'pickup_dayofweek'] = Xtest['pickup_datetime'].dt.dayofweek
Xtest.loc[:,'pickup_dayofyear'] = Xtest['pickup_datetime'].dt.dayofyear
Xtest.loc[:,'pickup_hour'] = Xtest['pickup_datetime'].dt.hour
Xtest.loc[:,'pickup_month'] = Xtest['pickup_datetime'].dt.month<feature_engineering>
corrmat = train_data.corr().sort_values('SalePrice', ascending=False ).index
corrmat
df.loc[:,'pickup_dayofweek'] = df['pickup_datetime'].dt.dayofweek
df.loc[:,'pickup_dayofyear'] = df['pickup_datetime'].dt.dayofyear
df.loc[:,'pickup_hour'] = df['pickup_datetime'].dt.hour
df.loc[:,'pickup_month'] = df['pickup_datetime'].dt.month
Dates = ['pickup_dayofweek', 'pickup_dayofyear', 'pickup_hour', 'pickup_month']
X_dates = df[Dates]
X_dates.head()<drop_column>
train_data = train_data.drop(train_data[(train_data['OverallQual'] > 9)&(train_data['SalePrice'] < 220000)].index)
 train_data = train_data.drop(train_data[(train_data['GrLivArea'] > 4000)&(train_data['SalePrice'] < 300000)].index)
 train_data = train_data.drop(train_data[(train_data['GarageArea'] > 1200)&(train_data['SalePrice'] < 300000)].index)
 train_data = train_data.drop(train_data[(train_data['TotalBsmtSF'] > 5000)&(train_data['SalePrice'] < 300000)].index)
 train_data = train_data.drop(train_data[(train_data['1stFlrSF'] > 4000)&(train_data['SalePrice'] < 300000)].index)
 train_data.shape
Xtest['flag'] = np.where(Xtest['store_and_fwd_flag']=='N', 0, 1)
 Xtest.drop(['store_and_fwd_flag'], axis = 1)
 df['flag'] = np.where(df['store_and_fwd_flag']=='N', 0, 1)
 df.drop(['store_and_fwd_flag'], axis = 1)
 df.head()<prepare_x_and_y>
all_data = pd.concat([train_data, test_data],keys=['train','test'])
 train_data.shape, test_data.shape, all_data.shape
Num_Var = ['passenger_count', 'pickup_longitude', 'pickup_latitude', 'dropoff_longitude', 'dropoff_latitude', 'flag']
X_num = df[Num_Var]
X = df[Num_Var + Dates]
X.head() 
<prepare_x_and_y>
all_data = all_data.drop(columns=['Id'], axis=1 )
y = df['trip_duration']
y.head()<split>
all_data['Age']=all_data['YrSold']-all_data['YearBuilt']+1
all_data['AgeRemodAdd']=all_data['YrSold']-all_data['YearRemodAdd']+1
all_data['AgeGarage']=all_data['YrSold']-all_data['GarageYrBlt']+1
all_data.drop(columns = 'YearBuilt')
 all_data.drop(columns = 'YearRemodAdd')
 all_data.drop(columns = 'GarageYrBlt' )
X_train, X_testing, y_train, y_testing = train_test_split(X,y, test_size=0.01, random_state=42)
 X_train.shape, X_testing.shape, y_train.shape, y_testing.shape<prepare_output>
for i in ['Alley','PoolQC','Fence','MiscFeature','FireplaceQu']:
all_data[i]=all_data[i].apply(lambda x: 'None' if str(x)=='nan' else x )
y_train = np.log(y_train)
 y_train.head()<choose_model_class>
for i in('GarageType','GarageFinish','GarageQual','GarageCond'):
all_data[i].fillna('None',inplace=True)
 all_data['GarageArea'] =(np.where(( all_data['GarageType']=='None'), 0, all_data['GarageArea']))
all_data['GarageCars'] =(np.where(( all_data['GarageType']=='None'), 0, all_data['GarageCars']))
all_data['AgeGarage'] =(np.where(( all_data['GarageType']=='None'), 0, all_data['AgeGarage']))
rf = RandomForestRegressor(random_state=42, n_estimators=10 )<train_model>
for i in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
all_data[i].fillna('None',inplace=True)
 for i in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
all_data[i] =(np.where(( all_data['BsmtQual']=='None'), 0, all_data[i]))
rf.fit(X_train, y_train )<predict_on_test>
x= { 20:'1-STORY 1946 & NEWER ALL STYLES',
30:'1-STORY 1945 & OLDER',
40:'1-STORY W/FINISHED ATTIC ALL AGES',
45:'1-1/2 STORY - UNFINISHED ALL AGES',
50:'1-1/2 STORY FINISHED ALL AGES',
60:'2-STORY 1946 & NEWER',
70:'2-STORY 1945 & OLDER',
75:'2-1/2 STORY',
80:'SPLIT',
85:'SPLIT FOYER',
90:'DUPLEX',
120:'1-STORY PUD',
150:'1-1/2 STORY PUD',
160:'2-STORY PUD',
180:'PUD - MULTILEVEL',
190:'2 FAMILY CONVERSION'}
all_data['MSSubClass']=all_data['MSSubClass'].map(x )
y_pred = rf.predict(test)
<load_from_csv>
total = all_data.isnull().sum().sort_values(ascending = False)
 percent = round(all_data.isnull().sum().sort_values(ascending = False)/ len(all_data)*100, 2)
 missingValues = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missingValues.loc[(missingValues['Total'] > 0)]
test_id = pd.read_csv(".. /input/test.csv", parse_dates = ["pickup_datetime"])
 test_id.head()<create_dataframe>
def HandleMissingValues(df):
num_cols = df.select_dtypes(include = ['int64', 'float64'])
 cat_cols = df.select_dtypes(include = 'object')
 values = {}
for a in cat_cols:
values[a] = 'unknown'
for a in num_cols:
_index = df[df[a].isna() ].index
_value = np.random.normal(loc = df[a].mean() , scale = df[a].std() , size = df[a].isna().sum())
 df[a].fillna(pd.Series(_value, index=_index), inplace=True)
 df.fillna(value=values, inplace=True)
 HandleMissingValues(all_data)
 all_data.isnull().sum().sum()
my_submission = pd.DataFrame({'id': test_id.id, 'trip_duration': np.expm1(y_pred)})
 my_submission.shape<save_to_csv>
X = all_data.copy()
y = X.pop("SalePrice")
 mi_scores = make_mi_scores(X, y)
 mi_scores
my_submission.to_csv('submission.csv',index=False )<feature_engineering>
def drop_uninformative(df, mi_scores):
return df.loc[:, mi_scores > 0.0]
<set_options>
mi_scores = mi_scores.append(pd.Series(0.01, index=["SalePrice"]))
all_data = drop_uninformative(all_data, mi_scores )
%matplotlib inline 
<load_from_csv>
all_data = pd.get_dummies(all_data)
 all_data.head()
train = '.. /input/train.csv'
test = '.. /input/test.csv'
dfTrain = pd.read_csv(train)
 dfTest = pd.read_csv(test )<count_missing_values>
target = train_data['SalePrice']
train_data = train_data.drop(['SalePrice'], axis=1)
 test_data = test_data.drop(['SalePrice'], axis=1)
 X, y = train_data, target
train_data.shape,test_data.shape
dfTrain.isna().sum()<count_missing_values>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 42 )
dfTest.isna().sum()<filter>
rf_best = RandomForestRegressor(random_state = 0,
criterion = 'mae',
max_depth = 11,
n_estimators = 85,
min_samples_leaf = 1,
min_samples_split = 2)
 rf_best.fit(X_train, y_train )
print(dfTrain.loc[dfTrain['trip_duration'] > 350000] )<filter>
rf_pred = rf_best.predict(X_test)
 np.sqrt(mean_squared_log_error(rf_pred, y_test))
dfTrain = dfTrain[dfTrain['passenger_count']>= 1]<feature_engineering>
gbr_best = GradientBoostingRegressor(learning_rate = 0.05,
max_depth = 3,
n_estimators = 3000,
min_samples_split = 10,
min_samples_leaf = 2,
loss = 'huber')
 gbr_best.fit(X_train, y_train )
dfTrain['pickup_datetime'] = pd.to_datetime(dfTrain['pickup_datetime'])
 dfTest['pickup_datetime'] = pd.to_datetime(dfTest['pickup_datetime'])
 dfTrain['month'] = dfTrain['pickup_datetime'].dt.month
dfTrain['day'] = dfTrain['pickup_datetime'].dt.day
dfTrain['weekday'] = dfTrain['pickup_datetime'].dt.weekday
dfTrain['hour'] = dfTrain['pickup_datetime'].dt.hour
dfTrain['minute'] = dfTrain['pickup_datetime'].dt.minute
dfTest['month'] = dfTest['pickup_datetime'].dt.month
dfTest['day'] = dfTest['pickup_datetime'].dt.day
dfTest['weekday'] = dfTest['pickup_datetime'].dt.weekday
dfTest['hour'] = dfTest['pickup_datetime'].dt.hour
dfTest['minute'] = dfTest['pickup_datetime'].dt.minute
dfTrain['trip_duration'] = np.log1p(dfTrain['trip_duration'].values)
 dfTrain.head()<prepare_x_and_y>
gbr_pred = gbr_best.predict(X_test)
 np.sqrt(mean_squared_log_error(gbr_pred, y_test))
selection_train = ["passenger_count", "pickup_longitude", "pickup_latitude", "dropoff_longitude","dropoff_latitude","month", "day", "weekday", "hour", "minute"]
selection_test = ["passenger_count", "pickup_longitude", "pickup_latitude", "dropoff_longitude","dropoff_latitude", "month", "day", "weekday", "hour", "minute"]
y_train = dfTrain["trip_duration"]
X_train = dfTrain[selection_train]
X_test = dfTest[selection_test]<train_model>
ridge = Ridge()
n_alphas = 200
alphas = np.logspace(-5, 5, n_alphas)
 ridge_params = {
'alpha': alphas
}
ridge_grid= GridSearchCV(ridge, ridge_params, scoring='neg_mean_squared_error', n_jobs = -1, cv=4)
 ridge_grid.fit(X_train, y_train)
 ridge_best = ridge_grid.best_estimator_
print("Training...")
 m = RandomForestRegressor()
m.fit(X_train, y_train)
 %timeit
print('Done!!!' )<predict_on_test>
ridge_pred = ridge_best.predict(X_test)
 np.sqrt(mean_squared_log_error(ridge_pred, y_test))
pred = m.predict(X_test)
 print('Done!!!' )<prepare_output>
vc = VotingRegressor([("gbr", gbr_best), 
("rf", rf_best), 
("ridge", ridge_best)],
weights = [0.6, 0.1, 0.3])
 vc.fit(X_train, y_train)
 vc_pred = vc.predict(X_test)
 np.sqrt(mean_squared_log_error(vc_pred, y_test))
pred = np.expm1(pred )<create_dataframe>
vc.fit(X, y)
 pred = vc.predict(test_data )
<save_to_csv><EOS> 
submission = pd.DataFrame({
"Id": list(range(1461, 1461+len(test_data))),
"SalePrice": pred
})
 submission.to_csv('./submission.csv', index=False )