Kaggle competition: ML 4 Money
train_data.loc[:, 'Date'] = train_data.Date.dt.strftime('%y%m%d')
 train_data.loc[:, 'Date'] = train_data['Date'].astype(int)
 test_data.loc[:, 'Date'] = test_data.Date.dt.strftime('%y%m%d')
 test_data.loc[:, 'Date'] = test_data['Date'].astype(int )<feature_engineering>
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression as LR
from sklearn.metrics import mean_squared_error
import lightgbm as lgb
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
train_data['Province_State'] = np.where(train_data['Province_State'] == 'nan',train_data['Country_Region'],train_data['Province_State'])
 test_data['Province_State'] = np.where(test_data['Province_State'] == 'nan',test_data['Country_Region'],test_data['Province_State'] )<data_type_conversions>
warnings.filterwarnings("ignore" )
convert_dict = {'Province_State': str}
train_data = train_data.astype(convert_dict)
 test_data = test_data.astype(convert_dict )<define_variables>
def rsi(values):
up = values[values>0].mean()
down = -1*values[values<0].mean()
return 100 * up /(up + down)
s =(train_data.dtypes == 'object')
 object_cols = list(s[s].index )<import_modules>
def abands(df):
df['AB_Middle_Band'] = df['close'].rolling(window = 20, center=False ).mean()
df['aupband'] = df['high'] *(1 + 4 *(df['high']-df['low'])/(df['high']+df['low']))
df['AB_Upper_Band'] = df['aupband'].rolling(window=20, center=False ).mean()
df['adownband'] = df['low'] *(1 - 4 *(df['high']-df['low'])/(df['high']+df['low']))
df['AB_Lower_Band'] = df['adownband'].rolling(window=20, center=False ).mean()
from sklearn.preprocessing import LabelEncoder<categorify>
def STOK(df, n):
df['STOK'] =(( df['close'] - df['low'].rolling(window=n, center=False ).mean())/(df['high'].rolling(window=n, center=False ).max() - df['low'].rolling(window=n, center=False ).min())) * 100
df['STOD'] = df['STOK'].rolling(window = 3, center=False ).mean()
label_encoder1 = LabelEncoder()
label_encoder2 = LabelEncoder()
train_data['Province_State'] = label_encoder1.fit_transform(train_data['Province_State'])
 test_data['Province_State'] = label_encoder1.transform(test_data['Province_State'])
 train_data['Country_Region'] = label_encoder2.fit_transform(train_data['Country_Region'])
 test_data['Country_Region'] = label_encoder2.transform(test_data['Country_Region'])
<define_variables>
def psar(df, iaf = 0.02, maxaf = 0.2):
length = len(df)
 high =(df['high'])
 low =(df['low'])
 close =(df['close'])
 psar = df['close'][0:len(df['close'])]
psarbull = [None] * length
psarbear = [None] * length
bull = True
af = iaf
ep = df['low'][0]
hp = df['high'][0]
lp = df['low'][0]
for i in range(2,length):
if bull:
psar[i] = psar[i - 1] + af *(hp - psar[i - 1])
 else:
psar[i] = psar[i - 1] + af *(lp - psar[i - 1])
 reverse = False
if bull:
if df['low'][i] < psar[i]:
bull = False
reverse = True
psar[i] = hp
lp = df['low'][i]
af = iaf
else:
if df['high'][i] > psar[i]:
bull = True
reverse = True
psar[i] = lp
hp = df['high'][i]
af = iaf
if not reverse:
if bull:
if df['high'][i] > hp:
hp = df['high'][i]
af = min(af + iaf, maxaf)
 if df['low'][i - 1] < psar[i]:
psar[i] = df['low'][i - 1]
if df['low'][i - 2] < psar[i]:
psar[i] = df['low'][i - 2]
else:
if df['low'][i] < lp:
lp = df['low'][i]
af = min(af + iaf, maxaf)
 if df['high'][i - 1] > psar[i]:
psar[i] = df['high'][i - 1]
if df['high'][i - 2] > psar[i]:
psar[i] = df['high'][i - 2]
if bull:
psarbull[i] = psar[i]
else:
psarbear[i] = psar[i]
df['psar'] = psar
Test_id = test_data.ForecastId<drop_column>
window = 12
def preprocess(data_in):
starting_ids = []
data = data_in.copy()
for k in range(5):
starting_ids.append(data[data.asset == k]['id'].min())
 data['diff'] = data['high'] - data['low']
data['diff*volume'] = data['diff'] * data['volume']
data ['vol/trades'] = data.volume / data.trades
data.drop('id',axis = 1, inplace = True)
 data.drop('asset',axis = 1, inplace = True)
 data['Momentum_1D'] =(data['close']-data['close'].shift(1)).fillna(0)
 data['RSI_14D'] = data['Momentum_1D'].rolling(center=False, window=14 ).apply(rsi ).fillna(0)
 data['RSI_1D'] = data['Momentum_1D'].rolling(center=False, window=1 ).apply(rsi ).fillna(0)
 data['RSI_2D'] = data['Momentum_1D'].rolling(center=False, window=2 ).apply(rsi ).fillna(0)
 data['RSI_4D'] = data['Momentum_1D'].rolling(center=False, window=4 ).apply(rsi ).fillna(0)
 data['RSI_8D'] = data['Momentum_1D'].rolling(center=False, window=8 ).apply(rsi ).fillna(0)
 data['BB_Middle_Band'], data['BB_Upper_Band'], data['BB_Lower_Band'] = bbands(data['close'], length=20, numsd=1)
 data['BB_Middle_Band'] = data['BB_Middle_Band'].fillna(0)
 data['BB_Upper_Band'] = data['BB_Upper_Band'].fillna(0)
 data['BB_Lower_Band'] = data['BB_Lower_Band'].fillna(0)
 listofzeros = [0] * 25
up, down = aroon(data)
 aroon_list = [x - y for x, y in zip(up,down)]
if len(aroon_list)==0:
aroon_list = [0] * data.shape[0]
data['Aroon_Oscillator'] = aroon_list
else:
data['Aroon_Oscillator'] = listofzeros+aroon_list
data["PVT"] =(data['Momentum_1D']/ data['close'].shift(1)) *data['volume']
data["PVT"] = data["PVT"] - data["PVT"].shift(1)
 data["PVT"] = data["PVT"].fillna(0)
 abands(data)
 data.fillna(0)
 STOK(data, 4)
 data.fillna(0)
 data['ROC'] =(( data['close'] - data['close'].shift(12)) /(data['close'].shift(12)))*100
data.fillna(0)
 data['VWAP'] = np.cumsum(data['volume'] *(data['high'] + data['low'])/2)/ np.cumsum(data['volume'])
 data.fillna(0)
 scaler = StandardScaler(with_std=False)
 data = scaler.fit_transform(data)
 return data
train_data.drop(['Id'], axis=1, inplace=True)
 test_data.drop('ForecastId', axis=1, inplace=True )<count_missing_values>
data = pd.read_csv('.. /input/train.csv')
 target = data.pop('y')
 data_test = pd.read_csv('.. /input/test.csv' )
missing_val_count_by_column =(train_data.isnull().sum())
 print(missing_val_count_by_column[missing_val_count_by_column>0] )<import_modules>
target_by_asset = []
test_by_asset = []
train_by_asset = []
PredID = []
preds_df = []
scores = []
best_params = []
grid_scores = []
grid_preds_df = []
for k in range(5):
train_by_asset.append(data[data['asset']==k])
 target_by_asset.append(target[data['asset']==k])
 PredID.append(data_test[data_test['asset']==k]['id'])
 train_by_asset[k] = preprocess(train_by_asset[k])
 test_by_asset.append(data_test[data_test['asset']==k])
 test_by_asset[k] = preprocess(test_by_asset[k])
 X_train, X_test, y_train, y_test = train_test_split(train_by_asset[k], target_by_asset[k], test_size=0.33, random_state=777, shuffle=False)
 lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
 params = {
'boosting_type': 'gbdt',
'colsample_bytree': 0.65,
'learning_rate': 0.005,
'n_estimators': 40,
'num_leaves': 16,
'objective': 'regression',
'random_state': 501,
'reg_alpha': 1,
'reg_lambda': 0,
'subsample': 0.7,
'max_depth' : -1,
'max_bin': 512,
'subsample_for_bin': 200,
'subsample_freq': 1,
'min_split_gain': 0.5,
'min_child_weight': 1,
'min_child_samples': 5,
'scale_pos_weight': 1,
}
gbm = lgb.train(params,
lgb_train,
num_boost_round=20,
valid_sets=lgb_eval,
early_stopping_rounds=5)
 mdl = lgb.LGBMRegressor(boosting_type= 'gbdt',
objective = 'regrssion',
n_jobs = -1,
silent = True,
max_depth = params['max_depth'],
max_bin = params['max_bin'],
subsample_for_bin = params['subsample_for_bin'],
subsample = params['subsample'],
subsample_freq = params['subsample_freq'],
min_split_gain = params['min_split_gain'],
min_child_weight = params['min_child_weight'],
min_child_samples = params['min_child_samples'],
scale_pos_weight = params['scale_pos_weight'])
 gridParams = {
'learning_rate': [0.005],
'n_estimators': [40],
'num_leaves': [6,8,12,16],
'boosting_type' : ['gbdt'],
'objective': ['regression'],
'random_state' : [501],
'colsample_bytree' : [0.65, 0.66],
'subsample' : [0.7,0.75],
'reg_alpha' : [0,1,1.2],
'reg_lambda' : [0,1,1.2,1.4],
}
grid = RandomizedSearchCV(mdl, gridParams,
verbose=0,
cv=4,
n_jobs=-1)
 grid.fit(train_by_asset[k], target_by_asset[k])
 print(grid.best_params_)
 print(grid.best_score_)
 best_params.append(grid.best_params_)
 grid_scores.append(grid.best_score_)
 grid_pred = grid.predict(test_by_asset[k])
 grid_pred[:12] = np.zeros(12)
 out = pd.DataFrame(PredID[k])
 out['expected'] = grid_pred
grid_preds_df.append(out)
 y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration)
 print('The rmse of prediction is:', mean_squared_error(y_test, y_pred)** 0.5)
 scores.append(mean_squared_error(y_test, y_pred)** 0.5)
 preds = gbm.predict(test_by_asset[k], num_iteration=gbm.best_iteration)
 preds[:12] = np.zeros(12)
 out = pd.DataFrame(PredID[k])
 out['expected'] = preds
preds_df.append(out )
from xgboost import XGBRegressor<prepare_x_and_y>
out_df = pd.DataFrame(np.concatenate(preds_df), columns=['id','expected'])
 out_df.id = out_df.id.astype('int')
 out_df.to_csv('by_asset_lgbm.csv',index = False )
X_train = train_data[['Province_State','Country_Region','Date']]
y_train = train_data[['ConfirmedCases', 'Fatalities']]<prepare_x_and_y>
out_df = pd.DataFrame(np.concatenate(grid_preds_df), columns=['id','expected'])
 out_df.id = out_df.id.astype('int')
 out_df.to_csv('grid_by_asset.csv',index = False )
y_train_confirm = y_train.ConfirmedCases
y_train_fatality = y_train.Fatalities<split>
regr = ElasticNetCV(cv=5, random_state=0, l1_ratio = [.1,.5,.7,.9,.95,.99, 1])
 scores = []
target_by_asset = []
test_by_asset = []
train_by_asset = []
PredID = []
preds_df = []
scores = []
best_params = []
grid_scores = []
grid_preds_df = []
for k in range(5):
train_by_asset.append(data[data['asset']==k])
 target_by_asset.append(target[data['asset']==k])
 PredID.append(data_test[data_test['asset']==k]['id'])
 train_by_asset[k] = preprocess(train_by_asset[k])
 test_by_asset.append(data_test[data_test['asset']==k])
 test_by_asset[k] = preprocess(test_by_asset[k])
 X_train, X_test, y_train, y_test = train_test_split(np.nan_to_num(train_by_asset[k],0), target_by_asset[k], test_size=0.33, random_state=777, shuffle=False)
 regr.fit(X_train,y_train)
 y_pred = regr.predict(X_test)
 print('The rmse of prediction is:', mean_squared_error(y_test, y_pred)** 0.5)
 scores.append(mean_squared_error(y_test, y_pred)** 0.5)
 preds = regr.predict(np.nan_to_num(test_by_asset[k],0))
out = pd.DataFrame(PredID[k])
 out['expected'] = preds
preds_df.append(out )
x_train = X_train.iloc[:,:].values
x_test = X_train.iloc[:,:].values<train_model>
out_df = pd.DataFrame(np.concatenate(preds_df), columns=['id','expected'])
 out_df.id = out_df.id.astype('int')
 out_df.to_csv('Elastic_net.csv',index = False )