Kaggle competition: House Prices - Advanced Regression Techniques
print(r2_score(y_val , y_pred))
print(np.sqrt(mean_squared_error(y_val , y_pred)) )<prepare_output>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
df1 = pd.DataFrame({'Actual': y_val, 'Predicted': y_pred})
 df1.head(25 )<predict_on_test>
train_id = train["Id"]
test_id = test["Id"]
train.drop("Id", axis=1, inplace= True)
 test.drop("Id", axis=1, inplace= True)
test_pred=gbrt.predict(test[X.columns])
 test_pred_inv=np.exp(test_pred)-1<save_to_csv>
train_salePrice = train.SalePrice
train.SalePrice = np.log1p(train.SalePrice)
 y_train = train.SalePrice.values
y_train_orig = train.SalePrice
prediction = pd.DataFrame(test_pred_inv)
 submission = pd.read_csv('.. /input/rossmann-store-sales/sample_submission.csv')
 prediction_df = pd.concat([submission['Id'], prediction], axis=1)
 prediction_df.columns=['Id','Sales']
prediction_df.to_csv('Sample_Submission.csv', index=False )<set_options>
data_features = pd.concat(( train, test), sort=True ).reset_index(drop=True)
 print(data_features.shape)
 data_features_na = data_features.isnull().sum()
data_features_na = data_features_na[data_features_na>0]
data_features_na.sort_values(ascending=False )
warnings.filterwarnings(action='ignore')
 plt.style.use('Solarize_Light2')
 print(os.listdir(".. /input"))<compute_test_metric>
str_vars = ['MSSubClass','YrSold','MoSold']
for var in str_vars:
data_features[var] = data_features[var].apply(str)
 common_vars = ['Exterior1st','Exterior2nd']
for var in common_vars:
data_features[var] = data_features[var].fillna('Other')
 common_vars = ['SaleType']
for var in common_vars:
data_features[var] = data_features[var].fillna('Oth')
 common_vars = ['Electrical']
for var in common_vars:
data_features[var] = data_features[var].fillna('FuseP')
 common_vars = ['KitchenQual']
for var in common_vars:
data_features[var] = data_features[var].fillna('Po')
 data_features['MSZoning'] = data_features['MSZoning'].fillna('I')
 for col in ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond','BsmtQual',
'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2',"PoolQC"
,'Alley','Fence','MiscFeature','FireplaceQu','MasVnrType','Utilities']:
data_features[col] = data_features[col].fillna('None')
 for col in('GarageYrBlt', 'GarageArea', 'GarageCars','MasVnrArea','BsmtFinSF1','BsmtFinSF2'
,'BsmtFullBath','BsmtHalfBath','FullBath','HalfBath','BsmtUnfSF','TotalBsmtSF'):
data_features[col] = data_features[col].fillna(0)
 data_features['LotFrontage'] = data_features.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.min()))
print('Features size:', data_features.shape)
 data_features['Functional'] = data_features['Functional'].fillna('Typ' )
def ispromomonth(rows):
months = {}
months = str(rows['PromoInterval'] ).split(',')
 if str(rows['month_str'])in months:
return 1
else:
return 0
def rmspe(y, yhat):
return np.sqrt(np.mean(( yhat/y-1)** 2))
def rmspe_xg(yhat, y):
y = np.expm1(y.get_label())
 yhat = np.expm1(yhat)
 return "rmspe", rmspe(y,yhat)
 class Rossmann_:
def __init__(self , train_data_path = '.. /input/train.csv' , test_data_path='.. /input/test.csv' , store_path='.. /input/store.csv' , nrows =100000):
self.train_data_path = train_data_path
self.test_data_path = test_data_path
self.store_path = store_path
self.read_size = nrows
self.train_data_original = pd.read_csv(self.train_data_path , low_memory = False , nrows = self.read_size)
 self.test_data_original = pd.read_csv(self.test_data_path ,low_memory = False , nrows = self.read_size)
 self.store_data_original = pd.read_csv(self.store_path)
 self.start_preprocessing_train(self.train_data_original , self.store_data_original)
 self.start_preprocessing_test(self.test_data_original , self.store_data_original)
 def start_preprocessing_train(self , train_data , store):
train_data.StateHoliday = train_data.StateHoliday.replace('0',0)
 train_data.StateHoliday = train_data.StateHoliday.replace('a',1)
 train_data.StateHoliday = train_data.StateHoliday.replace('b',2)
 train_data.StateHoliday = train_data.StateHoliday.replace('c',3)
 train_data['Date_Year'] = train_data['Date'].apply(lambda x: int(x[:4]))
train_data['Date_Month'] = train_data['Date'].apply(lambda x: int(x[5:7]))
train_data['Date_Day'] = train_data['Date'].apply(lambda x: int(x[8:]))
train_data_m = pd.merge(train_data, store, on='Store')
 mappings = {'0':0, 'a':1, 'b':2, 'c':3, 'd':4}
train_data_m.StoreType.replace(mappings, inplace=True)
 train_data_m.Assortment.replace(mappings, inplace=True)
 train_data_m['Date'] = pd.to_datetime(train_data_m['Date'], errors='coerce')
 train_data_m['date_WeekOfYear'] = train_data_m.Date.dt.weekofyear
train_data_m['Competition_Weeks'] = 12*(train_data_m.Date_Year - train_data_m.CompetitionOpenSinceYear)+(train_data_m.Date_Month - train_data_m.CompetitionOpenSinceMonth)
 train_data_m['Promo_Weeks'] = 12*(train_data_m.Date_Year - train_data_m.Promo2SinceYear)+(train_data_m.Date_Month - train_data_m.Promo2SinceWeek)
 train_data_m['Competition_Weeks'] = train_data_m['Competition_Weeks'].apply(lambda x: x if x > 0 else 0)
 train_data_m['Promo_Weeks'] = train_data_m['Promo_Weeks'].apply(lambda x: x if x > 0 else 0)
 month2str = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug', 9:'Sept', 10:'Oct', 11:'Nov', 12:'Dec'}
train_data_m['month_str'] = train_data_m.Date_Month.map(month2str)
 train_data_m['IsPromoMonth'] = train_data_m[[ 'PromoInterval' , 'month_str' ]].apply(ispromomonth , axis = 1)
 train_data_m.fillna(0, inplace=True)
 train_data_updated = train_data_m[train_data_m['Sales']>0]
train_data_updated = train_data_updated[train_data_updated['Customers']>0]
features = ['Store', 'DayOfWeek', 'Sales', 'Customers', 'Open', 'Promo','StateHoliday', 'SchoolHoliday', 'Date_Year', 'Date_Month', 'Date_Day','StoreType', 'Assortment', 'CompetitionDistance','CompetitionOpenSinceMonth', 'CompetitionOpenSinceYear', 'Promo2','Promo2SinceWeek', 'Promo2SinceYear', 'date_WeekOfYear', 'Competition_Weeks', 'Promo_Weeks', 'IsPromoMonth']
self.train_final = train_data_updated[features]
cols = self.train_final.columns
self.train_final = pd.DataFrame(StandardScaler().fit_transform(self.train_final), columns = cols)
 def start_preprocessing_test(self , test_data , store):
test_data.fillna(1 , inplace=True)
 test_data['Date_Year'] = test_data['Date'].apply(lambda x: int(x[:4]))
test_data['Date_Month'] = test_data['Date'].apply(lambda x: int(x[5:7]))
test_data['Date_Day'] = test_data['Date'].apply(lambda x: int(x[8:]))
test_data_m = pd.merge(test_data, store, on='Store')
 mappings = {'0':0, 'a':1, 'b':2, 'c':3, 'd':4}
test_data_m.StoreType.replace(mappings, inplace=True)
 test_data_m.Assortment.replace(mappings, inplace=True)
 test_data_m.StateHoliday.replace(mappings, inplace=True)
 test_data_m['Date'] = pd.to_datetime(test_data_m['Date'], errors='coerce')
 test_data_m['date_WeekOfYear'] = test_data_m.Date.dt.weekofyear
test_data_m['Competition_Weeks'] = 12*(test_data_m.Date_Year - test_data_m.CompetitionOpenSinceYear)+(test_data_m.Date_Month - test_data_m.CompetitionOpenSinceMonth)
 test_data_m['Promo_Weeks'] = 12*(test_data_m.Date_Year - test_data_m.Promo2SinceYear)+(test_data_m.Date_Month - test_data_m.Promo2SinceWeek)
 test_data_m['Competition_Weeks'] = test_data_m['Competition_Weeks'].apply(lambda x: x if x > 0 else 0)
 test_data_m['Promo_Weeks'] = test_data_m['Promo_Weeks'].apply(lambda x: x if x > 0 else 0)
 month2str = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug', 9:'Sept', 10:'Oct', 11:'Nov', 12:'Dec'}
test_data_m['month_str'] = test_data_m.Date_Month.map(month2str)
 test_data_m['IsPromoMonth'] = test_data_m[[ 'PromoInterval' , 'month_str' ]].apply(ispromomonth , axis = 1)
 test_data_m.fillna(0, inplace=True)
 features = ['Store', 'DayOfWeek', 'Open', 'Promo','StateHoliday', 'SchoolHoliday', 'Date_Year', 'Date_Month', 'Date_Day','StoreType', 'Assortment', 'CompetitionDistance','CompetitionOpenSinceMonth', 'CompetitionOpenSinceYear', 'Promo2','Promo2SinceWeek', 'Promo2SinceYear', 'date_WeekOfYear', 'Competition_Weeks', 'Promo_Weeks', 'IsPromoMonth']
self.test_final = test_data_m[features]
def prepare_sample_data(self , limit =100 , testing_limit = 30):
self.data = self.train_final.sample(frac = 1 , random_state = 98 ).head(limit)
 self.test_data = self.train_final.sample(frac = 1 , random_state = 98 ).tail(testing_limit)
 def Linear_Regression(self):
print('Creating Linear Regression Model Between Sales and Customers...')
 lr = LinearRegression()
lr.fit(self.data['Customers'].values.reshape(-1,1), self.data['Sales'].values.reshape(-1,1))
print('Fitting Done on Model...')
 print(lr)
 r2_score = lr.score(self.data['Customers'].values.reshape(-1,1), self.data['Sales'].values.reshape(-1,1))
print('Coefficients for the linear regression problem is ',lr.coef_)
 print('Intersect Value is ',lr.intercept_)
 y_pred = lr.predict(self.data['Customers'].values.reshape(-1, 1))
rms = sqrt(mean_squared_error(self.data['Sales'].values.reshape(-1,1), y_pred))
ty_pred = lr.predict(self.test_data['Customers'].values.reshape(-1, 1))
trms = sqrt(mean_squared_error(self.test_data['Sales'].values.reshape(-1,1), ty_pred))
print('Root Mean Squared Error of Training Set is ',rms)
 print('Root Mean Squared Error of Testing Set is ',trms)
 plt.figure(figsize=(15,10))
plt.scatter(self.data['Customers'].values.reshape(-1, 1), self.data['Sales'].values.reshape(-1,1), color ='r' , label = 'Actual Values')
 plt.scatter(self.data['Customers'].values.reshape(-1, 1), y_pred , color ='b' , label = 'Predicted')
 plt.plot(self.data['Customers'].values.reshape(-1, 1), y_pred , color ='k' , label = 'Predicted Line')
 plt.xlabel('Customers Index')
 plt.ylabel('Sales Index')
 plt.legend()
plt.savefig('Linear Regression Training.png')
 plt.figure(figsize=(15,10))
plt.scatter(self.test_data['Customers'].values.reshape(-1, 1), self.test_data['Sales'].values.reshape(-1,1), color ='g' , label = 'Actual Values')
 plt.scatter(self.test_data['Customers'].values.reshape(-1, 1), ty_pred , color ='y' , label = 'Predicted')
 plt.plot(self.test_data['Customers'].values.reshape(-1, 1), ty_pred , color ='k' , label = 'Predicted Line')
 plt.xlabel('Customers Index')
 plt.ylabel('Sales Index')
 plt.legend()
plt.savefig('Linear Regression Testing.png')
 def display_graphs(self,simple = False , orders = 1):
for i in range(1, orders+1,1):
lm = sns.lmplot(x ="Customers", y ="Sales", data = self.data, scatter = True, order = i, fit_reg = True, ci = 95)
 lm.fig.suptitle("Scatter plot with Order = "+str(i), fontsize=16)
 def Mulitple_Linear_Regression(self):
print('Creating Multiple Linear Regression Model...')
 print('Using Columns -> ',self.data.drop(columns = ['Sales','Customers'] ).columns)
 lr = LinearRegression()
lr.fit(self.data.drop(columns = ['Sales','Customers'] ).values , self.data['Sales'].values)
 print(lr)
 print('Fitting Done on Model...')
 print('Coefficients for the linear regression problem is ',lr.coef_)
 print('Intersect Value is ',lr.intercept_)
 y_pred = lr.predict(self.data.drop(columns = ['Sales','Customers'] ).values)
 rms = sqrt(mean_squared_error(self.data['Sales'].values, y_pred))
ty_pred = lr.predict(self.test_data.drop(columns = ['Sales','Customers'] ).values)
 trms = sqrt(mean_squared_error(self.test_data['Sales'].values, ty_pred))
print('Root Mean Squared Error of Training Set is ',rms)
 print('Root Mean Squared Error of Testing Set is ',trms)
 self.data['pred'] = y_pred
self.test_data['pred'] = ty_pred
plt.figure(figsize=(15,10))
sns.jointplot(x = 'Sales' , y = 'pred' , data = self.data, height=10, ratio=3 , color='g')
 plt.savefig('Multiple Linear Regression Training.png')
 plt.figure(figsize=(15,10))
sns.jointplot(x = 'Sales' , y = 'pred' , data = self.test_data, height=10, ratio=3 , color='r')
 plt.savefig('Multiple Linear Regression Testing.png')
 def Polynomial_Regression(self , degrees = 4):
print('To Reduce Complexity...
 Using Single Data Column Customers Rather than All...')
 Input=[('polynomial',PolynomialFeatures(degree=degrees)) ,('modal',LinearRegression())]
lr=Pipeline(Input)
 lr.fit(self.data['Customers'].values.reshape(-1,1), self.data['Sales'].values.reshape(-1,1))
print('Fitting Done on Model...')
 r2_score = lr.score(self.data['Customers'].values.reshape(-1,1), self.data['Sales'].values.reshape(-1,1))
self.data.sort_values(by='Customers' , inplace = True)
 self.test_data.sort_values(by='Customers' , inplace = True)
 y_pred = lr.predict(self.data['Customers'].values.reshape(-1, 1))
rms = sqrt(mean_squared_error(self.data['Sales'].values.reshape(-1,1), y_pred))
ty_pred = lr.predict(self.test_data['Customers'].values.reshape(-1, 1))
trms = sqrt(mean_squared_error(self.test_data['Sales'].values.reshape(-1,1), ty_pred))
print('Root Mean Squared Error of Training Set is ',rms)
 print('Root Mean Squared Error of Testing Set is ',trms)
 plt.figure(figsize=(15,10))
plt.scatter(self.data['Customers'].values.reshape(-1, 1), self.data['Sales'].values.reshape(-1,1), color ='r',label = 'Actual Values')
 plt.scatter(self.data['Customers'].values.reshape(-1, 1), y_pred , color ='b', label = 'Predicted')
 plt.plot(self.data['Customers'].values.reshape(-1, 1), y_pred , color ='k' , label = 'Predicted Line')
 plt.xlabel('Customers Index')
 plt.ylabel('Sales Index')
 plt.legend()
plt.savefig('Polynomial Regression Training {}.png'.format(degrees))
plt.figure(figsize=(15,10))
plt.scatter(self.test_data['Customers'].values.reshape(-1, 1), self.test_data['Sales'].values.reshape(-1,1), color ='g',label = 'Actual Values')
 plt.scatter(self.test_data['Customers'].values.reshape(-1, 1), ty_pred , color ='y', label = 'Predicted')
 plt.plot(self.test_data['Customers'].values.reshape(-1, 1), ty_pred , color ='k' , label = 'Predicted Line')
 plt.xlabel('Customers Index')
 plt.ylabel('Sales Index')
 plt.legend()
plt.savefig('Polynomial Regression Testing {}.png'.format(degrees))
def return_model(self,reg = 'Ridge' , alpha = 0.01):
if reg == 'Ridge':
lr = Ridge(alpha=alpha)
 elif reg =='Lasso':
lr = Lasso(alpha=alpha)
 elif reg =='Elastic':
lr = ElasticNet(alpha = alpha)
 else:
lr = Ridge(alpha=alpha , solver = 'cholesky', tol =.005)
 return lr
def Other_Regression(self , reg = 'Ridge'):
print('Creating Multiple {} Regression Model...'.format(reg))
print('Using Columns -> ',self.data.drop(columns = ['Sales','Customers'] ).columns)
 lr = self.return_model(reg = reg)
 lr.fit(self.data.drop(columns = ['Sales','Customers'] ).values , self.data['Sales'].values)
 print(lr)
 print('Fitting Done on Model...')
 print('Coefficients for the linear regression problem is ',lr.coef_)
 print('Intersect Value is ',lr.intercept_)
 y_pred = lr.predict(self.data.drop(columns = ['Sales','Customers'] ).values)
 rms = sqrt(mean_squared_error(self.data['Sales'].values, y_pred))
ty_pred = lr.predict(self.test_data.drop(columns = ['Sales','Customers'] ).values)
 trms = sqrt(mean_squared_error(self.test_data['Sales'].values, ty_pred))
print('Root Mean Squared Error of Training Set is ',rms)
 print('Root Mean Squared Error of Testing Set is ',trms)
 print('Creating Alpha VS Mean Squared Error Graph for Alpha')
 alphas = []
train_loss = []
test_loss = []
for i in range(10000):
alphas.append(i*0.0015 +0.0001)
 lr = self.return_model(reg = reg , alpha =(i*0.0015 +0.0001))
lr.fit(self.data.drop(columns = ['Sales','Customers'] ).values , self.data['Sales'].values)
 y_pred = lr.predict(self.data.drop(columns = ['Sales','Customers'] ).values)
 rms = sqrt(mean_squared_error(self.data['Sales'].values, y_pred))
ty_pred = lr.predict(self.test_data.drop(columns = ['Sales','Customers'] ).values)
 trms = sqrt(mean_squared_error(self.test_data['Sales'].values, ty_pred))
train_loss.append(rms)
 test_loss.append(trms)
 plt.figure(figsize=(15,10))
plt.plot(alphas , train_loss , color ='r' , label = 'Training Loss')
 plt.xlabel('Alpha')
 plt.ylabel('Loss(RMSE)')
 plt.legend()
plt.savefig('{} Regression Alpha Training.png'.format(reg))
plt.figure(figsize=(15,10))
plt.plot(alphas , test_loss , color ='g' , label = 'Testing Loss')
 plt.xlabel('Alpha')
 plt.ylabel('Loss(RMSE)')
 plt.legend()
plt.savefig('{} Regression Alpha Testing.png'.format(reg))
print('Using Single Column now.... ')
 lr = self.return_model(reg = reg)
 lr.fit(self.data['Customers'].values.reshape(-1,1), self.data['Sales'].values.reshape(-1,1))
print('Fitting Done on Model...')
 print(lr)
 r2_score = lr.score(self.data['Customers'].values.reshape(-1,1), self.data['Sales'].values.reshape(-1,1))
print('Coefficients for the linear regression problem is ',lr.coef_)
 print('Intersect Value is ',lr.intercept_)
 y_pred = lr.predict(self.data['Customers'].values.reshape(-1, 1))
rms = sqrt(mean_squared_error(self.data['Sales'].values.reshape(-1,1), y_pred))
ty_pred = lr.predict(self.test_data['Customers'].values.reshape(-1, 1))
trms = sqrt(mean_squared_error(self.test_data['Sales'].values.reshape(-1,1), ty_pred))
print('Root Mean Squared Error of Training Set is ',rms)
 print('Root Mean Squared Error of Testing Set is ',trms)
 plt.figure(figsize=(15,10))
plt.scatter(self.data['Customers'].values.reshape(-1, 1), self.data['Sales'].values.reshape(-1,1), color ='r' , label = 'Actual Values')
 plt.scatter(self.data['Customers'].values.reshape(-1, 1), y_pred , color ='b' , label = 'Predicted')
 plt.plot(self.data['Customers'].values.reshape(-1, 1), y_pred , color ='k' , label = 'Predicted Line')
 plt.xlabel('Customers Index')
 plt.ylabel('Sales Index')
 plt.legend()
plt.savefig('{} Regression Training.png'.format(reg))
plt.figure(figsize=(15,10))
plt.scatter(self.test_data['Customers'].values.reshape(-1, 1), self.test_data['Sales'].values.reshape(-1,1), color ='g' , label = 'Actual Values')
 plt.scatter(self.test_data['Customers'].values.reshape(-1, 1), ty_pred , color ='y' , label = 'Predicted')
 plt.plot(self.test_data['Customers'].values.reshape(-1, 1), ty_pred , color ='k' , label = 'Predicted Line')
 plt.xlabel('Customers Index')
 plt.ylabel('Sales Index')
 plt.legend()
plt.savefig('{} Regression Testing.png'.format(reg))<choose_model_class>
skewness = skewness[abs(skewness)> 0.5]
print("There are {} skewed numerical features to Box Cox transform".format(skewness.shape[0]))
print("Mean skewnees: {}".format(np.mean(skewness)))
 skewed_features = skewness.index
lam = 0.15
for feat in skewed_features:
feat_num[feat] = boxcox1p(feat_num[feat], boxcox_normmax(feat_num[feat] + 1))
data_features[feat] = boxcox1p(data_features[feat], boxcox_normmax(data_features[feat] + 1))
skewness.sort_values(ascending=False )
ross = Rossmann_()<choose_model_class>
skewness = feat_num.apply(lambda x: skew(x))
skewness = skewness[abs(skewness)> 0.5]
print("There are {} skewed numerical features after Box Cox transform".format(skewness.shape[0]))
print("Mean skewnees: {}".format(np.mean(skewness)))
 skewness.sort_values(ascending=False )
ross.prepare_sample_data(limit =200 , testing_limit = 40)
 ross.Linear_Regression()<choose_model_class>
data_features['TotalSF']=data_features['TotalBsmtSF'] + data_features['1stFlrSF'] + data_features['2ndFlrSF']
data_features['Total_Bathrooms'] =(data_features['FullBath'] +(0.5 * data_features['HalfBath'])+
data_features['BsmtFullBath'] +(0.5 * data_features['BsmtHalfBath']))
data_features['Total_porch_sf'] =(data_features['OpenPorchSF'] + data_features['3SsnPorch'] +
data_features['EnclosedPorch'] + data_features['ScreenPorch'] +
data_features['WoodDeckSF'])
 data_features['haspool'] = data_features['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 data_features['hasgarage'] = data_features['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 data_features['hasbsmt'] = data_features['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 data_features['hasfireplace'] = data_features['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
ross.prepare_sample_data(limit =2000 , testing_limit = 400)
 ross.Mulitple_Linear_Regression()<train_model>
data_features = data_features.drop(['MasVnrArea', 'OpenPorchSF', 'WoodDeckSF', 'BsmtFinSF1','2ndFlrSF'], axis=1)
 print('data_features size:', data_features.shape)
ross.prepare_sample_data(limit =10000 , testing_limit = 4000)
 ross.Polynomial_Regression(degrees = 3 )<train_model>
y_train = train['SalePrice']
ross.prepare_sample_data(limit =10000 , testing_limit = 4000)
 ross.Polynomial_Regression(degrees = 2 )<choose_model_class>
data_features = data_features.drop("SalePrice", axis = 1)
 final_features = pd.get_dummies(data_features)
 print(final_features.shape)
 X = final_features.iloc[:len(y), :]
X_test = final_features.iloc[len(y):, :]
X.shape, y_train.shape, X_test.shape
print(X.shape,y_train.shape,X_test.shape )
ross.prepare_sample_data(limit =1000 , testing_limit = 400)
 ross.Other_Regression(reg = 'Ridge' )<choose_model_class>
overfit = []
for i in X.columns:
counts = X[i].value_counts()
zeros = counts.iloc[0]
if zeros / len(X)* 100 > 99.95:
overfit.append(i)
 overfit = list(overfit)
 overfit.append('MSZoning_C(all)')
 X = X.drop(overfit, axis=1 ).copy()
X_test = X_test.drop(overfit, axis=1 ).copy()
print(X.shape,y_train.shape,X_test.shape )
ross.prepare_sample_data(limit =1000 , testing_limit = 400)
 ross.Other_Regression(reg = 'Lasso' )<train_model>
from datetime import datetime
from sklearn.preprocessing import RobustScaler
from sklearn.model_selection import KFold, cross_val_score
from sklearn.metrics import mean_squared_error , make_scorer
from sklearn.linear_model import ElasticNetCV, LassoCV, RidgeCV
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import GradientBoostingRegressor
from mlxtend.regressor import StackingCVRegressor
from sklearn.linear_model import LinearRegression
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
ross.prepare_sample_data(limit =1000 , testing_limit = 400)
 ross.Other_Regression(reg = 'Elastic' )<train_model>
kfolds = KFold(n_splits=10, shuffle=True, random_state=42)
 def cv_rmse(model, X=X):
rmse = np.sqrt(-cross_val_score(model, X, y,scoring="neg_mean_squared_error",cv=kfolds))
return(rmse)
 def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
ross.prepare_sample_data(limit =1000 , testing_limit = 400)
 ross.Other_Regression(reg = 'Bridge' )<init_hyperparams>
lightgbm = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=8000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.2,
feature_fraction_seed=7,
verbose=-1, 
)
 e_alphas = [0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007]
e_l1ratio = [0.8, 0.85, 0.9, 0.95, 0.99, 1]
alphas_alt = [14.5, 14.6, 14.7, 14.8, 14.9, 15, 15.1, 15.2, 15.3, 15.4, 15.5]
alphas2 = [5e-05, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008]
ridge = make_pipeline(RobustScaler() , RidgeCV(alphas=alphas_alt, cv=kfolds))
lasso = make_pipeline(RobustScaler() , LassoCV(max_iter=1e7,
alphas=alphas2,random_state=42, cv=kfolds))
elasticnet = make_pipeline(RobustScaler() , ElasticNetCV(max_iter=1e7,
alphas=e_alphas, cv=kfolds, l1_ratio=e_l1ratio))
stack_gen = StackingCVRegressor(regressors=(ridge, lasso, elasticnet, lightgbm),
meta_regressor=elasticnet,
use_features_in_secondary=True)
 models = {'Ridge': ridge,
'Lasso': lasso,
'ElasticNet': elasticnet,
'lightgbm': lightgbm}
predictions = {}
scores = {}
num_sample_stores=1115
valid_days=0
s= Namespace(**{
"l1":4497,
"l2":2328,
"ps1":0.2771132028380148,
"ps2":0.15631474446268287,
"emb_drop":0.14301109844119272,
"batchsize":64,
"lrate":0.0660858230905056,
"lrate_ratio":9,
"wd":0.17305139150930285,
"l1epoch":4,
"l2epoch":3,
"l3epoch":8,
})
<import_modules>
for name, model in models.items() :
model.fit(X, y)
 predictions[name] = np.expm1(model.predict(X))
score = cv_rmse(model, X=X)
 scores[name] =(score.mean() , score.std() )
from pathlib import Path
from datetime import datetime, timedelta
import numpy as np
import pandas as pd
from fastai import *
from fastai.tabular import *
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import cufflinks as cf 
<set_options>
scoreList = []
print('---- Score with CV_RMSE-----')
 score = cv_rmse(ridge)
 print("Ridge score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
scoreList.append(1 - score.mean())
 score = cv_rmse(lasso)
 print("Lasso score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
scoreList.append(1 - score.mean())
 score = cv_rmse(elasticnet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
scoreList.append(1 - score.mean())
 score = cv_rmse(lightgbm)
 print("lightgbm score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
scoreList.append(1 - score.mean())
 score = cv_rmse(stack_gen)
 print("stack_gen score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
scoreList.append(1 - score.mean())
 ".format(score.mean() , score.std()))
print('----START Fit----',datetime.now())
 print('Elasticnet')
 elastic_model = elasticnet.fit(X, y)
 print('Lasso')
 lasso_model = lasso.fit(X, y)
 print('Ridge')
 ridge_model = ridge.fit(X, y)
 print('lightgbm')
 lgb_model_full_data = lightgbm.fit(X, y)
 print('stack_gen')
 stack_gen_model = stack_gen.fit(np.array(X), np.array(y))
plotly.offline.init_notebook_mode(connected=False)
 cf.go_offline()
%matplotlib inline
%reload_ext autoreload
%autoreload 2
pd.set_option('display.max_columns', 0)
 pd.set_option('display.max_rows', 500 )<load_from_disk>
def blend_models_predict(X):
return(( 0.25 * elastic_model.predict(X)) + \ 
(0.25 * lasso_model.predict(X)) + \ 
(0.2 * ridge_model.predict(X)) + \ 
(0.10 * lgb_model_full_data.predict(X)) + \ 
(0.2 * stack_gen_model.predict(np.array(X))))
def blend_models_predict_ABL(X, _scoreList):
print("scoreList~>|%s|" %(_scoreList,))
ScoreSum = sum(_scoreList)
 print("ScoreSum~>|%s|" %(ScoreSum,))
return(( scoreList[2]/ScoreSum * elastic_model.predict(X)) + \ 
(scoreList[1]/ScoreSum * lasso_model.predict(X)) + \ 
(scoreList[0]/ScoreSum * ridge_model.predict(X)) + \ 
(scoreList[3]/ScoreSum * lgb_model_full_data.predict(X)) + \ 
(scoreList[4]/ScoreSum* stack_gen_model.predict(np.array(X))))
path=Path(".. /input/rossmann-data-engineering/")
 traindf=pd.read_feather(path/"train.feather")
 testdf=pd.read_feather(path/"test.feather" )<feature_engineering>
print('RMSLE score on train data:')
 print(rmsle(y, blend_models_predict(X)))
 print('RMSLE score on train data:(blend_models_predict2)')
 print(rmsle(y, blend_models_predict_ABL(X, scoreList)) )
<feature_engineering><EOS> 
print('Predict submission')
 submission = pd.read_csv(".. /input/sample_submission.csv")
 submission.iloc[:,1] =(np.expm1(blend_models_predict_ABL(X_test, scoreList)) )