Kaggle competition: Multiple regression for time series data
<SOS> metric: mse Kaggle data source: multiple-regression-for-time-series-data<feature_engineering>
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import numpy as np
TARGET = np.array(list(train_df['target'].values)* 200)
 TRAIN = []
var_mean = {}
var_var = {}
for var in features:
tmp = var_to_feat(train_df[var], var_stats[var], int(var[4:]))
var_mean[var] = np.mean(tmp[:,0])
 var_var[var] = np.var(tmp[:,0])
 tmp[:,0] =(tmp[:,0]-var_mean[var])/var_var[var]
TRAIN.append(tmp)
 TRAIN = np.vstack(TRAIN)
 del train_df
_=gc.collect()
print(TRAIN.shape, len(TARGET))<choose_model_class>
train_df = pd.read_csv('.. /input/train.csv')
 train_df.head()
model = lgb.LGBMClassifier(**{
'learning_rate': 0.04,
'num_leaves': 31,
'max_bin': 1023,
'min_child_samples': 1000,
'reg_alpha': 0.1,
'reg_lambda': 0.2,
'feature_fraction': 1.0,
'bagging_freq': 1,
'bagging_fraction': 0.85,
'objective': 'binary',
'n_jobs': -1,
'n_estimators':200,})
 MODELS = []
skf = StratifiedKFold(n_splits=10, shuffle=True, random_state=11111)
 for fold_,(train_indexes, valid_indexes)in enumerate(skf.split(TRAIN, TARGET)) :
print('Fold:', fold_)
 model = model.fit(TRAIN[train_indexes], TARGET[train_indexes],
eval_set =(TRAIN[valid_indexes], TARGET[valid_indexes]),
verbose = 10,
eval_metric='auc',
early_stopping_rounds=25,
categorical_feature = [2])
 MODELS.append(model)
 del TRAIN, TARGET
_=gc.collect()<save_to_csv>
test_df = pd.read_csv('.. /input/test.csv')
 test_df.head()
ypred = np.zeros(( 200000,200))
for feat,var in enumerate(features):
tmp = var_to_feat(test_df[var], var_stats[var], int(var[4:]))
tmp[:,0] =(tmp[:,0]-var_mean[var])/var_var[var]
for model_id in range(10):
model = MODELS[model_id]
ypred[:,feat] += model.predict_proba(tmp)[:,1] / 10.
 ypred = np.mean(logit(ypred), axis=1)
 sub = test_df[['ID_code']]
sub['target'] = ypred
sub['target'] = sub['target'].rank() / 200000.
 sub.to_csv('sample_submission1.csv', index=False)
<load_from_csv>
columns_in = [f"sensor
columns_out = "oil_extraction"
X = train_df[columns_in].values
train_y = train_df[columns_out].values
test_x = test_df[columns_in].values
def transform(x):
diffs = 25
diff = [np.zeros(x.shape)for i in range(diffs)]
n = len(x)
 diff[0][1:,:] = x[:n-1,:]
for i in range(1, diffs):
diff[i][1:,:] = diff[i-1][:n-1,:]
return np.hstack([x, *diff])
 train_x = np.hstack([transform(X), transform(X**2)] )
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df['target']<categorify>
def predictions_to_submission_file(predictions):
submission_df = pd.DataFrame(columns=['Expected', 'Id'])
 submission_df['Expected'] = predictions
submission_df['Id'] = range(len(predictions))
submission_df.to_csv('submission.csv', index=False)
 test_predictions = lin_reg.predict(np.hstack([transform(test_x), transform(test_x**2)]))
predictions_to_submission_file(test_predictions )