text_html_output_1.png
from scipy.stats.mstats import winsorize

from sklearn.linear_model import TweedieRegressor

from sklearn.metrics import mean_squared_error

from sklearn.model_selection import StratifiedKFold

from sklearn.preprocessing import StandardScaler

from sklearn.preprocessing import StandardScaler, KBinsDiscretizer

import numpy as np

import pandas as pd

X_train = pd.read_csv('../input/30-days-of-ml/train.csv')
X_test = pd.read_csv('../input/30-days-of-ml/test.csv')

y_train = X_train.target
X_train = X_train.set_index('id').drop('target', axis='columns')
X_test = X_test.set_index('id')

y_stratified = pd.cut(y_train.rank(method='first'), bins=10, labels=False)

categoricals = [item for item in X_train.columns if 'cat' in item]

dummies = pd.get_dummies(X_train.append(X_test)[categoricals])
X_train[dummies.columns] = dummies.iloc[:len(X_train), :]
X_test[dummies.columns] = dummies.iloc[len(X_train):, :]
del dummies

important_features = ['cat8_E', 'cont0', 'cont5', 'cont7', 'cont8', 'cat1_A', 'cont2', 'cont13', 'cont3', 'cont10', 'cont1', 'cont9', 'cont11', 'cat1', 'cat8_C', 'cont6', 'cont12', 'cat5', 'cat3_C', 'cont4', 'cat8']
X_train = X_train[important_features]
X_test = X_test[important_features]

folds = 10
skf = StratifiedKFold(n_splits=folds, shuffle=True, random_state=0)
predictions = np.zeros(len(X_test))
score = list()
for k, (train_idx, val_idx) in enumerate(skf.split(X_train, y_stratified)):
    ss = StandardScaler()
    X = ss.fit_transform(X_train.iloc[train_idx, :]).astype(np.float32)
    Xv = ss.transform(X_train.iloc[val_idx, :]).astype(np.float32)
    Xt = ss.transform(X_test).astype(np.float32)
    y_train_w = np.array(winsorize(y_train[train_idx], [0.002, 0.0]))
    glm = TweedieRegressor(power=1, alpha=0.0001, max_iter=10000)
    glm.fit(X, y_train_w)
    val_preds = glm.predict(Xv)
    val_rmse = mean_squared_error(y_true=y_train[val_idx], y_pred=val_preds, squared=False)
    print(f'Fold {k} RMSE: {val_rmse:0.5f}')
    predictions += glm.predict(Xt).ravel()
    score.append(val_rmse)
predictions /= folds
print(f'CV RMSE {np.mean(score):0.5f} ({np.std(score):0.5f})')