Kaggle competition: Digit Recognizer
for model in [stacking_model, model_xgb, model_lgb]:
model.fit(train.values, y_train)
 stacking_pred = np.expm1(stacking_model.predict(test.values))
xgb_pred = np.expm1(model_xgb.predict(test.values))
lgb_pred = np.expm1(model_lgb.predict(test.values))
ensemble = stacking_pred * 0.7 + xgb_pred * 0.15 + lgb_pred * 0.15
submission = pd.DataFrame({'Id': test_id, 'SalePrice': ensemble})
 submission.to_csv('submission.csv', index=False )<install_modules>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
!pip install lightautoml<import_modules>
from fastai.imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
from lightautoml.reader.base import PandasToPandasReader
from lightautoml.tasks import Task
from lightautoml.automl.presets.tabular_presets import TabularAutoML<load_from_csv>
train=pd.read_csv(f'{PATH}/train.csv')
 test=pd.read_csv(f'{PATH}/test.csv' )
df_train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 df_train.head()<choose_model_class>
image=train.iloc[:,1:]
lbl=train.iloc[:,0:1]
autoML = TabularAutoML(task=task,
general_params = {"nested_cv":False, "use_algos":[["linear_l1", "lgb", "lgb_tuned"]]},
reader_params = {"cv":5, "random_state":1},
tuning_params = {"max_tuning_iter":20, "max_tuning_time":30},
lgb_params = {"default_params":{"num_threads":4}} )<predict_on_test>
img=image.as_matrix()
img=img.reshape(-1,28,28)
 test_img=test.as_matrix()
test_img=test_img.reshape(-1,28,28)
 img=np.stack(( img,)*3, axis=-1 ).astype('float32')
 test_img=np.stack(( test_img,)*3, axis=-1 ).astype('float32')
oof_pred = autoML.fit_predict(df_train, roles=roles )<load_from_csv>
test_df = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 test_df.head()<predict_on_test>
train_img, val_img, train_lbl, val_lbl=train_test_split(img, lbl, train_size=0.8,random_state=1 )
test_predictions = autoML.predict(test_df )<create_dataframe>
train_lbl=train_lbl.values.flatten()
val_lbl=val_lbl.values.flatten()
output = pd.DataFrame({"Id":test_df["Id"],
"SalePrice":test_predictions.data[:,0]})
 output<save_to_csv>
learn=ConvLearner.pretrained(arch, data, precompute=True )
output.to_csv("submission.csv", index=False )<load_from_csv>
learn.lr_find()
train_all = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv', index_col= 'Id')
 test_all = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv', index_col='Id')
 target = train_all['SalePrice']<count_unique_values>
learn=ConvLearner.pretrained(arch, data, precompute=True )
train_all.drop(['SalePrice'], axis=1, inplace=True)
 train_all.select_dtypes(exclude='number' ).nunique().sort_values(ascending=True )<count_unique_values>
learn.fit(0.01,9 )
train_all.select_dtypes(exclude='object' ).nunique().sort_values(ascending=True )<data_type_conversions>
learn=ConvLearner.pretrained(arch, data, precompute=False )
for col in('BsmtHalfBath', 'HalfBath', 'BsmtFullBath', 'Fireplaces','KitchenAbvGr','FullBath'):
train_all[col] = train_all[col].astype(str)
 test_all[col] = test_all[col].astype(str )<count_unique_values>
learn.fit(0.01,9, cycle_len=1 )
cat_cols = [col for col in train_all.columns if train_all[col].dtype == "object"]
ten_or_over = []
for col in cat_cols:
if train_all[col].nunique() > 10:
ten_or_over.append(col)
 print(ten_or_over )<drop_column>
lr=np.array([1e-4,1e-3,1e-2] )
def drop_columns(data, drop_list):
for col in drop_list:
data = data.drop([col], axis = 1)
 return data
train = drop_columns(train_all, ten_or_over)
 test = drop_columns(test_all, ten_or_over)
<split>
learn.fit(lr, 4, cycle_len=1, cycle_mult=2 )
X_train, X_valid, y_train, y_valid = train_test_split(train, target,
train_size=0.9, test_size=0.1, random_state=42)
 X_train.shape, X_valid.shape, y_train.shape, y_valid.shape<import_modules>
learn.save('4_epochs' )
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
from sklearn.ensemble import GradientBoostingRegressor
from lightgbm import LGBMRegressor
from sklearn.linear_model import Lasso
from catboost import CatBoostRegressor
from xgboost import XGBRegressor<categorify>
%time log_preds_test, y_test=learn.TTA(is_test=True)
 probs_test=np.mean(np.exp(log_preds_test),0)
 probs_test.shape
numerical_cols = [col for col in X_train.columns if X_train[col].dtype == "int64" or X_train[col].dtype == "float"]
categorical_cols = [col for col in X_train.columns if X_train[col].dtype == "object"]
numerical_transformer = SimpleImputer(strategy='constant')
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
] )<train_model>
df=pd.DataFrame(probs_test )
preprocess = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, numerical_cols), 
('cat', categorical_transformer, categorical_cols)
 ])
 def process_and_fit(model, train, target):
pipeline = Pipeline(steps=[('preprocess', preprocess), 
('scale', StandardScaler()), 
('model', model)])
 pipeline.fit(train, target)
 return pipeline
def rmse(validation, target):
return np.sqrt(mean_squared_error(validation, target))<choose_model_class>
df1=df[['ImageId', 'Label']]
randomForest = RandomForestRegressor(n_estimators=800, random_state=20)
 gradientBoost = GradientBoostingRegressor(n_estimators = 600, random_state=20)
 catBoost = CatBoostRegressor(loss_function='RMSE', random_state=20, verbose=False)
 xgBoost = XGBRegressor(colsample_bytree = 0.3, learning_rate = 0.1, max_depth = 7, alpha = 4, n_estimators = 200)
 lassoReg = Lasso(alpha=0.0005, normalize = True)
 lightGBM = LGBMRegressor(objective='regression' )<predict_on_test>
df1.to_csv("submission.csv", index=False )