Kaggle competition: ASHRAE - Great Energy Predictor III
<SOS> metric: RMSLE Kaggle data source: ashrae-great-energy-predictor-iii<train_model>
py.init_notebook_mode(connected=True)
model = ModelBuild(X_train, y_train )<predict_on_test>
def reduce_mem_usage(df, use_float16=False):

 start_mem = df.memory_usage().sum() / 1024**2
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
for col in df.columns:
if is_datetime(df[col])or is_categorical_dtype(df[col]):
continue
col_type = df[col].dtype
if col_type != object:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if use_float16 and c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 else:
df[col] = df[col].astype('category')
 end_mem = df.memory_usage().sum() / 1024**2
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 *(start_mem - end_mem)/ start_mem))
return df
for test, sample in tqdm.tqdm(env.iter_test()):
make_pred(test, sample, env, model )<save_to_csv>
%%time
root = Path('.. /input/ashrae-feather-format-for-fast-loading')
 train_df = pd.read_feather(root/'train.feather')
 test_df = pd.read_feather(root/'test.feather')
 building_meta_df = pd.read_feather(root/'building_metadata.feather' )
env.write_submission_file()<set_options>
leak_df = pd.read_feather('.. /input/ashrae-leak-data-station/leak.feather')
 leak_df.fillna(0, inplace=True)
 leak_df = leak_df[(leak_df.timestamp.dt.year > 2016)&(leak_df.timestamp.dt.year < 2019)]
leak_df.loc[leak_df.meter_reading < 0, 'meter_reading'] = 0
leak_df = leak_df[leak_df.building_id!=245]
warnings.filterwarnings('ignore')
 __print__ = print
def print(string):
os.system(f'echo "{string}"')
 __print__(string )<load_from_csv>
leak_df.meter.value_counts()
df = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2020/train.csv', low_memory=False)
 shape = str(df.shape)
 print(df.columns)
 print(df.shape )<feature_engineering>
print(leak_df.duplicated().sum() )
def prepare_data(df):
df.loc[df['StadiumType'].isnull() , 'StadiumType'] = 'Unknown'
df.loc[df['GameWeather'].isnull() , 'GameWeather'] = 'Unknown'
df.loc[df['WindDirection'].isnull() , 'WindDirection'] = 'Unknown'
df.loc[df['WindSpeed'].isnull() , 'WindSpeed'] = '0'
df.loc[df['Orientation'].isnull() , 'Orientation'] = 0
df.loc[df['Dir'].isnull() , 'Dir'] = 0
df.loc[df['GameWeather']== 'T: 51; H: 55; W: NW 10 mph', 'Temperature'] = 51
df.loc[df['GameWeather']== 'T: 51; H: 55; W: NW 10 mph', 'WindDirection'] = 'NW 10 mph'
df.loc[df['GameWeather']== 'T: 51; H: 55; W: NW 10 mph', 'Humidity'] = 55
df.loc[df['Temperature'].isnull() , 'Temperature'] = 70
df.loc[df['Humidity'].isnull() , 'Humidity'] = 0
df.loc[df['OffenseFormation'].isnull() , 'OffenseFormation'] = 'Unknown'
df.loc[df['PossessionTeam'] == df['HomeTeamAbbr'] , 'DefendingTeam'] = df['VisitorTeamAbbr']
df.loc[df['PossessionTeam'] != df['HomeTeamAbbr'] , 'DefendingTeam'] = df['HomeTeamAbbr']
df.loc[(df['FieldPosition'].isnull())&(df['PlayDirection'] == 'right')&(df['X'] >= 50), 'FieldPosition'] = df['DefendingTeam']
df.loc[(df['FieldPosition'].isnull())&(df['PlayDirection'] == 'right')&(df['X'] < 50), 'FieldPosition'] = df['PossessionTeam']
df.loc[(df['FieldPosition'].isnull())&(df['PlayDirection'] == 'left')&(df['X'] >= 50), 'FieldPosition'] = df['PossessionTeam']
df.loc[(df['FieldPosition'].isnull())&(df['PlayDirection'] == 'left')&(df['X'] < 50), 'FieldPosition'] = df['DefendingTeam']
df = df.dropna()
df['WindSpeed'] = df['WindSpeed'].astype('str')
 df['WindSpeed'] = df['WindSpeed'].str.replace('mph', '')
 df['WindSpeed'] = df['WindSpeed'].str.replace('Mph', '')
 df['WindSpeed'] = df['WindSpeed'].str.replace('MPH', '')
 df['WindSpeed'] = df['WindSpeed'].str.replace('MPh', '')
 df['WindSpeed'] = df['WindSpeed'].str.strip()
b = df['WindSpeed'].loc[df['WindSpeed'].str.contains('-', regex=False)==True].unique()
b1 = df['WindSpeed'].loc[df['WindSpeed'].str.contains('-', regex=False)==True].str[:2]
b2 = df['WindSpeed'].loc[df['WindSpeed'].str.contains('-', regex=False)==True].str[-2:]
b3 =(b1.astype('float64')+ b2.astype('float64')) / 2
for i in b:
df.loc[(df.WindSpeed == i), 'WindSpeed'] = b3
v = df[df['WindSpeed'].str.isnumeric() == False]
av = v['WindSpeed'].unique()
for j in av:
df.loc[(df.WindSpeed == j), 'WindSpeed'] = '0.0'
df['WindSpeed'] = df['WindSpeed'].astype('float64')
 df.X[df['PlayDirection'] == 'left'] = 120 - df['X']
df.YardLine[df['PossessionTeam'] == df['FieldPosition']] = 10 + df['YardLine']
df.YardLine[df['PossessionTeam'] != df['FieldPosition']] = 60.0 +(50 - df['YardLine'])
 df.Orientation[df['PlayDirection'] == 'left'] = 360 - df['Orientation']
df.loc[(df['PlayDirection'] == 'left')&(df['Orientation'] == 0),'Orientation'] = 0
df.Orientation[df['PlayDirection'] == 'left'] = 360 - df['Dir']
df.loc[(df['PlayDirection'] == 'left')&(df['Dir'] == 0),'Dir'] = 0
nfl = {
1:	'ARI',
2:	'ATL',
3:	'BAL',
4:	'BUF',
5:	'CAR',
6:	'CHI',
7:	'CIN',
8:	'CLE',
9:	'DAL',
10:	'DEN',
11:	'DET',
12:	'GB',
13:	'HOU',
14:	'IND',
15:	'JAX',
16:	'KC',
17:	'LAC',
18:	'LAR',
19:	'MIA',
20:	'MIN',
21:	'NE',
22:	'NO',
23:	'NYG',
24:	'NYJ',
25:	'OAK',
26:	'PHI',
27:	'PIT',
28:	'SEA',
29:	'SF',
30:	'TB',
31:	'TEN',
32:	'WAS'
}
nfl_teams = pd.DataFrame.from_dict(nfl, orient='index')
 nfl_teams['team_id'] = nfl_teams.index
nfl_teams = nfl_teams.rename(columns={0: 'team_abb'})
 	
df.loc[df['HomeTeamAbbr'] == 'ARZ', 'HomeTeamAbbr'] = 'ARI'
df.loc[df['HomeTeamAbbr'] == 'BLT', 'HomeTeamAbbr'] = 'BAL'
df.loc[df['HomeTeamAbbr'] == 'CLV', 'HomeTeamAbbr'] = 'CLE'
df.loc[df['HomeTeamAbbr'] == 'HST', 'HomeTeamAbbr'] = 'HOU'
df.loc[df['HomeTeamAbbr'] == 'LA', 'HomeTeamAbbr'] = 'LAR'
df.loc[df['VisitorTeamAbbr'] == 'ARZ', 'VisitorTeamAbbr'] = 'ARI'
df.loc[df['VisitorTeamAbbr'] == 'BLT', 'VisitorTeamAbbr'] = 'BAL'
df.loc[df['VisitorTeamAbbr'] == 'CLV', 'VisitorTeamAbbr'] = 'CLE'
df.loc[df['VisitorTeamAbbr'] == 'HST', 'VisitorTeamAbbr'] = 'HOU'
df.loc[df['VisitorTeamAbbr'] == 'LA', 'VisitorTeamAbbr'] = 'LAR'
df = df.merge(nfl_teams, left_on ='HomeTeamAbbr', right_on ='team_abb', how = 'left')
 df = df.drop(['HomeTeamAbbr','team_abb'], axis=1)
 df = df.rename(columns={'team_id': 'HomeTeamAbbr'})
 	
df = df.merge(nfl_teams, left_on ='VisitorTeamAbbr', right_on ='team_abb', how = 'left')
 df = df.drop(['VisitorTeamAbbr','team_abb'], axis=1)
 df = df.rename(columns={'team_id': 'VisitorTeamAbbr'})
 	
df['HomeTeamAbbr'] = df['HomeTeamAbbr'].astype('float64')
 df['VisitorTeamAbbr'] = df['VisitorTeamAbbr'].astype('float64')
 	
df.loc[df['FieldPosition'] == 'ARZ', 'FieldPosition'] = 'ARI'
df.loc[df['FieldPosition'] == 'BLT', 'FieldPosition'] = 'BAL'
df.loc[df['FieldPosition'] == 'CLV', 'FieldPosition'] = 'CLE'
df.loc[df['FieldPosition'] == 'HST', 'FieldPosition'] = 'HOU'
df.loc[df['FieldPosition'] == 'LA', 'FieldPosition'] = 'LAR'
df.loc[df['PossessionTeam'] == 'ARZ', 'PossessionTeam'] = 'ARI'
df.loc[df['PossessionTeam'] == 'BLT', 'PossessionTeam'] = 'BAL'
df.loc[df['PossessionTeam'] == 'CLV', 'PossessionTeam'] = 'CLE'
df.loc[df['PossessionTeam'] == 'HST', 'PossessionTeam'] = 'HOU'
df.loc[df['PossessionTeam'] == 'LA', 'PossessionTeam'] = 'LAR'
df = df.merge(nfl_teams, left_on ='FieldPosition', right_on ='team_abb', how = 'left')
 df = df.drop(['FieldPosition','team_abb'], axis=1)
 df = df.rename(columns={'team_id': 'FieldPosition'})
 df = df.merge(nfl_teams, left_on ='PossessionTeam', right_on ='team_abb', how = 'left')
 df = df.drop(['PossessionTeam','team_abb'], axis=1)
 df = df.rename(columns={'team_id': 'PossessionTeam'})
 df['FieldPosition'] = df['FieldPosition'].astype('float64')
 df['PossessionTeam'] = df['PossessionTeam'].astype('float64')
 	
df['DefensePersonnel_DL'] = df['DefensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(0, 1)
 df['DefensePersonnel_LB'] = df['DefensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(4, 5)
 df['DefensePersonnel_DB'] = df['DefensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(8, 9)
 df['DefensePersonnel_DL'] = df['DefensePersonnel_DL'].astype('int64')
 df['DefensePersonnel_LB'] = df['DefensePersonnel_LB'].astype('int64')
 df['DefensePersonnel_DB'] = df['DefensePersonnel_DB'].astype('int64')
 	
df.loc[df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(1, 3)== 'OL', 'OffensePersonnel_Flag'] = 1
df.loc[df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(1, 3)== 'QB', 'OffensePersonnel_Flag'] = 2
df.loc[df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(1, 3)== 'RB', 'OffensePersonnel_Flag'] = 0
	
df.loc[df['OffensePersonnel_Flag'] == 2, 'OffensePersonnel_QB'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(0, 1)
 df.loc[df['OffensePersonnel_Flag'] == 2, 'OffensePersonnel_RB'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(4, 5)
 df.loc[df['OffensePersonnel_Flag'] == 2, 'OffensePersonnel_TE'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(8, 9)
 df.loc[df['OffensePersonnel_Flag'] == 2, 'OffensePersonnel_WR'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(12, 13)
 df.loc[df['OffensePersonnel_Flag'] != 2, 'OffensePersonnel_QB'] = '1'
	
df.loc[df['OffensePersonnel_Flag'] == 1, 'OffensePersonnel_OL'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(0, 1)
 df.loc[df['OffensePersonnel_Flag'] == 1, 'OffensePersonnel_RB'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(4, 5)
 df.loc[df['OffensePersonnel_Flag'] == 1, 'OffensePersonnel_TE'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(8, 9)
 df.loc[df['OffensePersonnel_Flag'] == 1, 'OffensePersonnel_WR'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(12, 13)
 	
df.loc[df['OffensePersonnel_Flag'] == 0, 'OffensePersonnel_RB'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(0, 1)
 df.loc[df['OffensePersonnel_Flag'] == 0, 'OffensePersonnel_TE'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(4, 5)
 df.loc[df['OffensePersonnel_Flag'] == 0, 'OffensePersonnel_WR'] = df['OffensePersonnel'].str.replace(', ', '_' ).str.replace(' ', '' ).str.slice(8, 9)
 	
df['OffensePersonnel_QB'] = df['OffensePersonnel_QB'].astype('int64')
 df['OffensePersonnel_RB'] = df['OffensePersonnel_RB'].astype('int64')
 df['OffensePersonnel_TE'] = df['OffensePersonnel_TE'].astype('int64')
 df['OffensePersonnel_WR'] = df['OffensePersonnel_WR'].astype('int64')
 df.loc[df['OffensePersonnel_Flag'] != 1, 'OffensePersonnel_OL_tmp'] = 11 -(df['OffensePersonnel_RB'] + df['OffensePersonnel_TE'] + df['OffensePersonnel_WR']+df['OffensePersonnel_QB'])
 df.loc[df['OffensePersonnel_Flag'] != 1, 'OffensePersonnel_OL'] = df['OffensePersonnel_OL_tmp']
df['OffensePersonnel_OL'] = df['OffensePersonnel_OL'].astype('int64')
 df = df.drop(['OffensePersonnel_OL_tmp'], axis=1)
 	
	
	
PlayerHeight_ft=df['PlayerHeight'].str.split('-' ).str[0].astype('float64')
 PlayerHeight_in=df['PlayerHeight'].str.split('-' ).str[1].astype('float64')
 PlayerHeight_m = PlayerHeight_ft*0.3048 + PlayerHeight_in*0.0254
df['PlayerHeight'] = PlayerHeight_m.astype('float64')
 	
time_snap_handoff = pd.to_datetime(df['TimeHandoff'])- pd.to_datetime(df['TimeSnap'])
 time_snap_handoff_sec = pd.to_timedelta(time_snap_handoff)
 df['TimeSnapHandoff'] = time_snap_handoff_sec.dt.seconds.astype('int64')
 df['GameYear'] = pd.DatetimeIndex(df['TimeSnap'] ).year
df['GameMonth'] = pd.DatetimeIndex(df['TimeSnap'] ).month
df['GameDay'] = pd.DatetimeIndex(df['TimeSnap'] ).day
today=dt.datetime.today().strftime('%Y-%m-%d')
 today=pd.to_datetime(today)
 PlayerAgeDays = today - pd.to_datetime(df['PlayerBirthDate'])
 df['PlayerAgeYears'] = PlayerAgeDays/np.timedelta64(1,'Y')
 GameClockMin = pd.DatetimeIndex(df['GameClock'] ).hour
GameClockSec = pd.DatetimeIndex(df['GameClock'] ).minute
df['GameClockSeconds'] = GameClockMin*60 + GameClockSec
columns_to_drop = ['GameClock','TimeSnap','TimeHandoff','PlayerBirthDate', 'JerseyNumber',
'OffensePersonnel', 'DefensePersonnel','PlayerCollegeName']
df = df.drop(columns_to_drop, axis=1)
 df.columns
return df<categorify>
print(len(leak_df)/ len(train_df))
def transform_data(df):
categorical_feature_mask = df.dtypes==object
categorical_cols = df.columns[categorical_feature_mask].tolist()
print(categorical_cols)
 le = LabelEncoder()
df[categorical_cols] = df[categorical_cols].apply(lambda col: le.fit_transform(col))
return df<categorify>
del train_df
gc.collect()
dfa = prepare_data(df )<categorify>
sample_submission1 = pd.read_csv('.. /input/ashrae-kfold-lightgbm-without-leak-1-08/submission.csv', index_col=0)
 sample_submission2 = pd.read_csv('.. /input/ashrae-half-and-half/submission.csv', index_col=0)
 sample_submission3 = pd.read_csv('.. /input/ashrae-highway-kernel-route4/submission.csv', index_col=0 )
dfa_sc = transform_data(dfa )<define_variables>
test_df['pred1'] = sample_submission1.meter_reading
test_df['pred2'] = sample_submission2.meter_reading
test_df['pred3'] = sample_submission3.meter_reading
test_df.loc[test_df.pred3<0, 'pred3'] = 0
del sample_submission1, sample_submission2, sample_submission3
gc.collect()
test_df = reduce_mem_usage(test_df)
 leak_df = reduce_mem_usage(leak_df )
feature_names = ['Team', 'X', 'Y', 'S', 'A', 'Dis', 'Orientation', 'Dir', 'NflId',
'Season', 'YardLine', 'Quarter', 'PossessionTeam', 'Down', 'Distance',
'FieldPosition', 'HomeScoreBeforePlay', 'VisitorScoreBeforePlay',
'NflIdRusher', 'OffenseFormation', 'DefendersInTheBox', 'PlayDirection',
'PlayerHeight', 'PlayerWeight', 'Position', 'HomeTeamAbbr',
'VisitorTeamAbbr', 'Week', 'Stadium', 'Location', 'StadiumType', 'Turf',
'GameWeather', 'Temperature', 'Humidity', 'WindSpeed', 'WindDirection',
'DefensePersonnel_DL', 'DefensePersonnel_LB', 'DefensePersonnel_DB',
'OffensePersonnel_Flag', 'OffensePersonnel_QB', 'OffensePersonnel_RB',
'OffensePersonnel_TE', 'OffensePersonnel_WR', 'OffensePersonnel_OL',
'TimeSnapHandoff', 'GameYear', 'GameMonth', 'GameDay', 'PlayerAgeYears',
'GameClockSeconds']
dfy = dfa_sc[['Yards']]
dfX = dfa_sc[feature_names]
yards = dfy.Yards
y = np.zeros(( yards.shape[0], 199))
for idx, target in enumerate(list(yards)) :
y[idx][99 + target] = 1<compute_test_metric>
leak_df = leak_df.merge(test_df[['building_id', 'meter', 'timestamp', 'pred1', 'pred2', 'pred3', 'row_id']], left_on = ['building_id', 'meter', 'timestamp'], right_on = ['building_id', 'meter', 'timestamp'], how = "left")
 leak_df = leak_df.merge(building_meta_df[['building_id', 'site_id']], on='building_id', how='left' )
def crps(y_true, y_pred):
y_true = np.clip(np.cumsum(y_true, axis=1), 0, 1)
 y_pred = np.clip(np.cumsum(y_pred, axis=1), 0, 1)
 return(( y_true - y_pred)** 2 ).sum(axis=1 ).sum(axis=0)/(199 * y_true.shape[0] )<train_model>
leak_df['pred1_l1p'] = np.log1p(leak_df.pred1)
 leak_df['pred2_l1p'] = np.log1p(leak_df.pred2)
 leak_df['pred3_l1p'] = np.log1p(leak_df.pred3)
 leak_df['meter_reading_l1p'] = np.log1p(leak_df.meter_reading )
models = []
kf = KFold(n_splits=4, random_state=42)
 score = []
for i,(train_index, test_index)in enumerate(kf.split(dfX, y)) :
print(f'Fold : {i}')
 X_train, X_test = dfX.iloc[train_index], dfX.iloc[test_index]
y_train, y_test = y[train_index], y[test_index]
model = RandomForestRegressor(n_estimators=20, random_state=0)
 model.fit(X_train, y_train)
 score_rf_crps = crps(y_test, model.predict(X_test))
print(score_rf_crps)
 score.append(score_rf_crps)
 models.append(model)
 print(np.mean(score_rf_crps))<define_variables>
leak_df[leak_df.pred1_l1p.isnull() ]
env = nflrush.make_env()
for(test_df, sample_prediction_df)in env.iter_test() :
dfb = prepare_data(test_df)
 feature_names = ['Team', 'X', 'Y', 'S', 'A', 'Dis', 'Orientation', 'Dir', 'NflId',
'Season', 'YardLine', 'Quarter', 'PossessionTeam', 'Down', 'Distance',
'FieldPosition', 'HomeScoreBeforePlay', 'VisitorScoreBeforePlay',
'NflIdRusher', 'OffenseFormation', 'DefendersInTheBox', 'PlayDirection',
'PlayerHeight', 'PlayerWeight', 'Position', 'HomeTeamAbbr',
'VisitorTeamAbbr', 'Week', 'Stadium', 'Location', 'StadiumType', 'Turf',
'GameWeather', 'Temperature', 'Humidity', 'WindSpeed', 'WindDirection',
'DefensePersonnel_DL', 'DefensePersonnel_LB', 'DefensePersonnel_DB',
'OffensePersonnel_Flag', 'OffensePersonnel_QB', 'OffensePersonnel_RB',
'OffensePersonnel_TE', 'OffensePersonnel_WR', 'OffensePersonnel_OL',
'TimeSnapHandoff', 'GameYear', 'GameMonth', 'GameDay', 'PlayerAgeYears',
'GameClockSeconds']
dfb_sc = transform_data(dfb)
 dfb_sc_final = dfb[feature_names]
y_pred = np.mean([model.predict(dfb_sc_final)for model in models],axis=0)
 y_pred = np.clip(np.cumsum(y_pred, axis=1), 0, 1 ).tolist() [0]
preds_df = pd.DataFrame(data=[y_pred], columns=sample_prediction_df.columns)
 env.predict(preds_df)
 env.write_submission_file()<load_from_csv>
leak_df['mean_pred'] = np.mean(leak_df[['pred1', 'pred2', 'pred3']].values, axis=1)
 leak_df['mean_pred_l1p'] = np.log1p(leak_df.mean_pred)
 leak_score = np.sqrt(mean_squared_error(leak_df.mean_pred_l1p, leak_df.meter_reading_l1p))
sns.distplot(leak_df.mean_pred_l1p)
 sns.distplot(leak_df.meter_reading_l1p)
 print('mean score=', leak_score )
%matplotlib inline
nlp = spacy.load('en_core_web_lg')
 review_data = pd.read_csv('.. /input/nlp-getting-started/train.csv')
<compute_test_metric>
N = 10
scores = np.zeros(N,)
 for i in range(N):
p = i * 1./N
v = p * leak_df['pred1'].values +(1.-p)* leak_df ['pred3'].values
vl1p = np.log1p(v)
 scores[i] = np.sqrt(mean_squared_error(vl1p, leak_df.meter_reading_l1p))
true_ratio=review_data.target.mean()
print('true_ratio is {0:.2f} %'.format(true_ratio*100))<train_model>
best_weight = np.argmin(scores)* 1./N
print(scores.min() , best_weight )
X_train, X_test, y_train, y_test = train_test_split(vectorsF, review_data.target,
test_size=0.2, random_state=1)
 model = LinearSVC(random_state=1, dual=False)
 model.fit(X_train, y_train)
 print(f'Model test accuracy: {model.score(X_test, y_test)*100:.3f}%' )<train_model>
scores = np.zeros(N,)
 for i in range(N):
p = i * 1./N
v = p *(best_weight * leak_df['pred1'].values +(1.-best_weight)* leak_df ['pred3'].values)+(1.-p)* leak_df ['pred2'].values
vl1p = np.log1p(v)
 scores[i] = np.sqrt(mean_squared_error(vl1p, leak_df.meter_reading_l1p))
clf1 = svm.SVC(kernel='linear')
 clf1.fit(X_train, y_train)
 print(f'Model test accuracy: {clf1.score(X_test, y_test)*100:.3f}%' )<train_model>
best_weight2 = np.argmin(scores)* 1./N
print(scores.min() , best_weight2)
clf2 = svm.SVC(kernel='rbf')
 clf2.fit(X_train, y_train)
 print(f'Model test accuracy: {clf2.score(X_test, y_test)*100:.3f}%' )<choose_model_class>
all_combinations = list(np.linspace(0.2,0.5,31))
all_combinations
model = tf.keras.Sequential([
tf.keras.layers.Dense(48, activation='relu', input_shape=[300]),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
 ])
 model.compile(loss='binary_crossentropy', optimizer='adam',metrics=['accuracy'])
 model.summary()<train_model>
import itertools
num_epochs = 10
model.fit(X_train, y_train, epochs=num_epochs, validation_data=(X_test, y_test))<train_model>
l = [all_combinations, all_combinations, all_combinations]
all_l = list(itertools.product(*l)) + list(itertools.product(*reversed(l)) )
from sklearn.linear_model import Lasso 
<train_model>
filtered_combis = [l for l in all_l if l[0] + l[1] + l[2] > 0.95 and l[0] + l[1] + l[2] < 1.05]
from sklearn.ensemble import RandomForestRegressor 
<compute_test_metric>
best_combi = []
for i, combi in enumerate(filtered_combis):
score1 = combi[0]
score2 = combi[1]
score3 = combi[2]
v = score1 * leak_df['pred1'].values + score2 * leak_df['pred3'].values + score3 * leak_df['pred2'].values
vl1p = np.log1p(v)
 curr_score = np.sqrt(mean_squared_error(vl1p, leak_df.meter_reading_l1p))
if best_combi:
prev_score = best_combi[0][1]
if curr_score < prev_score:
best_combi[:] = []
best_combi += [(i, curr_score)]
else:
best_combi += [(i, curr_score)]
score = best_combi[0][1]
print(score )
from xgboost import XGBRegressor 
<import_modules>
sample_submission = pd.read_feather(os.path.join(root, 'sample_submission.feather'))
final_combi = filtered_combis[best_combi[0][0]]
w1 = final_combi[0]
w2 = final_combi[1]
w3 = final_combi[2]
print("The weights are: w1=" + str(w1)+ ", w2=" + str(w2)+ ", w3=" + str(w3))
sample_submission['meter_reading'] = w1 * test_df.pred1 + w2 * test_df.pred3 + w3 * test_df.pred2
sample_submission.loc[sample_submission.meter_reading < 0, 'meter_reading'] = 0
import tensorflow as tf
from tensorflow import keras<define_variables>
leak_df = leak_df[['meter_reading', 'row_id']].set_index('row_id' ).dropna()
sample_submission.loc[leak_df.index, 'meter_reading'] = leak_df['meter_reading']
train_length=round(review_data.shape[0]*0.9)
 test_length=review_data.shape[0]-train_length
training_sentences = []
training_labels = []
testing_sentences = []
testing_labels = []
for i in range(train_length):
training_sentences.append(review_data.text[i])
 training_labels.append(review_data.target[i])
 for i in range(train_length, review_data.shape[0]):
testing_sentences.append(review_data.text[i])
 testing_labels.append(review_data.target[i])
 training_labels_final = np.array(training_labels)
 testing_labels_final = np.array(testing_labels )<string_transform>
sample_submission.to_csv('submission.csv', index=False, float_format='%.4f' )