Kaggle competition: Predicting Molecular Properties
feature_importances['average'] = feature_importances[['fold_{}'.format(fold + 1)for fold in range(folds.n_splits)]].mean(axis=1)
 feature_importances.to_csv('feature_importances.csv')
 plt.figure(figsize=(16, 16))
sns.barplot(data=feature_importances.sort_values(by='average', ascending=False ).head(50), x='average', y='feature');
plt.title('50 TOP feature importance over {} folds average'.format(folds.n_splits)) ;<find_best_params>
sub_path = ".. /input/top-mol"
all_files = os.listdir(sub_path)
 all_files
best_iter = clf.best_iteration<train_model>
outs = [pd.read_csv(os.path.join(sub_path, f), index_col=0)for f in all_files]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "mol" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True)
 concat_sub.head()
ncol = concat_sub.shape[1]
clf = lgb.LGBMClassifier(**params, num_boost_round=best_iter)
 clf.fit(X, y )<predict_on_test>
concat_sub['m_max'] = concat_sub.iloc[:, 1:ncol].max(axis=1)
 concat_sub['m_min'] = concat_sub.iloc[:, 1:ncol].min(axis=1)
 concat_sub['m_mean'] = concat_sub.iloc[:, 1:ncol].mean(axis=1)
 concat_sub['m_median'] = concat_sub.iloc[:, 1:ncol].median(axis=1 )
sub['isFraud'] = clf.predict_proba(test)[:, 1]<save_to_csv>
cutoff_lo = 0.8
cutoff_hi = 0.2
sub.to_csv('ieee_cis_fraud_detection_new.csv', index=False )<import_modules>
concat_sub['scalar_coupling_constant'] = concat_sub['m_mean']
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_mean.csv',
index=False, float_format='%.6f' )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestRegressor
import xgboost as xgb
from sklearn.model_selection import train_test_split,cross_validate,cross_val_score,cross_val_predict
from sklearn.model_selection import GridSearchCV,RandomizedSearchCV
from sklearn.metrics import mean_squared_log_error
from sklearn.preprocessing import MinMaxScaler,StandardScaler<load_from_csv>
concat_sub['scalar_coupling_constant'] = concat_sub['m_median']
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_median.csv',
index=False, float_format='%.6f' )
df1=pd.read_csv("/kaggle/input/bike-sharing-demand/train.csv")
 df2=pd.read_csv("/kaggle/input/bike-sharing-demand/test.csv")
 def preprocess(df):
df["hour"]=pd.to_datetime(df["datetime"] ).dt.hour
df["dayofweek"]=pd.to_datetime(df["datetime"] ).dt.dayofweek
df["dayofmonth"]=pd.to_datetime(df["datetime"] ).dt.day
df["month"]=pd.to_datetime(df["datetime"] ).dt.month
df["year"]=pd.to_datetime(df["datetime"] ).dt.year.map({2011:0, 2012:1})
 df=pd.get_dummies(df,columns=["weather","season"],prefix=["weather","season"],drop_first=True,dtype=int)
 return df.drop(["datetime"],axis=1)
 df1=preprocess(df1)
 df2=preprocess(df2 )<split>
concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1), 1,
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
0, concat_sub['m_median']))
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_pushout_median.csv',
index=False, float_format='%.6f' )
features=[x for x in df1.columns if x not in ["count","casual","registered"]]
label1=["casual"]
label2=["registered"]
label=["count"]
X_train,y1_train,y2_train,y_train=df1[features],df1[label1],df1[label2],df1[label]
X_test=df2[features]
X_train_0,X_train_1,y_train_0,y_train_1=train_test_split(X_train,y_train,test_size=0.25,random_state=42 )<compute_train_metric>
concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['m_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['m_min'],
concat_sub['m_mean']))
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_minmax_mean.csv',
index=False, float_format='%.6f' )
rf=RandomForestRegressor(n_estimators=500,n_jobs=-1)
 rf.fit(X_train_0,y_train_0)
 pred=rf.predict(X_train_1)
 print(mean_squared_log_error(y_train_1,pred)**0.5 )<train_model>
concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['m_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['m_min'],
concat_sub['m_median']))
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_minmax_median.csv',
index=False, float_format='%.6f' )
rf.fit(X_train_0,y_train_0.apply(lambda x:np.log1p(x)))
 pred=np.expm1(rf.predict(X_train_1))
print(mean_squared_log_error(y_train_1,pred)**0.5 )<create_dataframe>
concat_sub['scalar_coupling_constant'] = concat_sub['mol0'].rank(method ='min')+ concat_sub['mol1'].rank(method ='min')+ concat_sub['mol2'].rank(method ='min')
 concat_sub['scalar_coupling_constant'] =(concat_sub['scalar_coupling_constant']-concat_sub['scalar_coupling_constant'].min())/(concat_sub['scalar_coupling_constant'].max() - concat_sub['scalar_coupling_constant'].min())
 concat_sub.describe()
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_rank.csv', index=False, float_format='%.8f' )
print(pd.DataFrame({"Features": features,"Importance" : rf.feature_importances_.round(2)} ).sort_values("Importance",ascending=False))<train_model>
sub1=pd.read_csv('.. /input/another-one/stackers_blend.csv')
 sub2=pd.read_csv('.. /input/lgb-public-kernels-plus-more-features/sub_lgb_model_individual.csv')
 sub3=pd.read_csv('.. /input/yet-another-one/stackers_blend.csv')
 sub4=pd.read_csv('.. /input/giba-r-data-table-simple-features-1-17-lb/submission-giba-1.csv')
 temp=pd.read_csv('.. /input/another-one/stackers_blend.csv' )
DM_0=xgb.DMatrix(X_train_0,y_train_0)
 DM_1=xgb.DMatrix(X_train_1,y_train_1)
 params={"booster":"gbtree"}
xgbr=xgb.train(dtrain=DM_0,num_boost_round=380,params=params)
 pred = xgbr.predict(DM_1)
 pred[pred<0]=0
print(mean_squared_log_error(y_train_1,pred)**0.5 )<train_model>
temp['scalar_coupling_constant'] = 0.6*sub1['scalar_coupling_constant'] + 0.4*sub1['scalar_coupling_constant']
temp.to_csv('submission4.csv', index=False )