Kaggle competition: Nomad2018 Predicting Transparent Conductors
<SOS> metric: MCRMSLE Kaggle data source: nomad2018-predicting-transparent-conductors<load_from_csv>
warnings.filterwarnings('ignore')
 print(check_output(["ls", ".. /input"] ).decode("utf8"))
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
df_train = pd.read_csv('.. /input/makejuneextradata-fff8dd/juneExtra.csv', usecols=train_cols )<load_from_csv>
x_columns = [i for i in train.columns if i not in list(['id','formation_energy_ev_natom','bandgap_energy_ev'])]
label1 = 'formation_energy_ev_natom'
label2 = 'bandgap_energy_ev'
X = train[x_columns]
y = train[[label1,label2]]
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state=2017)
 X_train = X_train.as_matrix()
X_valid = X_valid.as_matrix()
y_train_values1 = np.log1p(y_train['formation_energy_ev_natom'].values)
 y_train_values2 = np.log1p(y_train['bandgap_energy_ev'].values)
 y_valid_values1 = np.log1p(y_valid['formation_energy_ev_natom'].values)
 y_valid_values2 = np.log1p(y_valid['bandgap_energy_ev'].values )
df_test = pd.read_csv('.. /input/santander-product-recommendation/test_ver2.csv', usecols = demographic_cols )<filter>
clf1 = KernelRidge(kernel ='linear', alpha=1.0)
 clf2 = KernelRidge(kernel ='linear', alpha=1.0)
 clf1.fit(X_train,y_train_values1)
 clf2.fit(X_train,y_train_values2)
 preds1 = clf1.predict(X_valid)
 preds2 = clf2.predict(X_valid)
 y_pred1 = np.exp(preds1)-1
y_pred2 = np.exp(preds2)-1
rsme_valid1 = np.sqrt(mean_squared_error(y_valid_values1,preds1))
rsme_valid2 = np.sqrt(mean_squared_error(y_valid_values2,preds2))
rsme_total = np.sqrt(rsme_valid1*rsme_valid1+rsme_valid2*rsme_valid2)
 print('RSME for formation energy:')
 print(rsme_valid1)
 print('RSME for band gap:')
 print(rsme_valid2)
 print('RSME for total:')
 print(rsme_total )
df_train = df_train[df_train['ind_nuevo'] == 0]
df_train = df_train[df_train['antiguedad'] != -999999]
df_train = df_train[df_train['indrel'] == 1]
df_train = df_train[df_train['indresi'] == 'S']
df_train = df_train[df_train['indfall'] == 'N']
df_train = df_train[df_train['tipodom'] == 1]
df_train = df_train[df_train['ind_empleado'] == 'N']
df_train = df_train[df_train['pais_residencia'] == 'ES']
df_train = df_train[df_train['indrel_1mes'] == 1]
df_train = df_train[df_train['tiprel_1mes'] ==('A' or 'I')]
df_train = df_train[df_train['indext'] == 'N']<drop_column>
clf3 = KernelRidge(kernel ='polynomial', alpha=1.0)
 clf4 = KernelRidge(kernel ='polynomial', alpha=1.0)
 clf3.fit(X_train,y_train_values1)
 clf4.fit(X_train,y_train_values2)
 preds1 = clf3.predict(X_valid)
 preds2 = clf4.predict(X_valid)
 y_pred1 = np.exp(preds1)-1
y_pred2 = np.exp(preds2)-1
rsme_valid1 = np.sqrt(mean_squared_error(y_valid_values1,preds1))
rsme_valid2 = np.sqrt(mean_squared_error(y_valid_values2,preds2))
rsme_total = np.sqrt(rsme_valid1*rsme_valid1+rsme_valid2*rsme_valid2)
 print('RSME for formation energy:')
 print(rsme_valid1)
 print('RSME for band gap:')
 print(rsme_valid2)
 print('RSME for total:')
 print(rsme_total )
drop_column = ['ind_nuevo','indrel','indresi','indfall','tipodom','ind_empleado','pais_residencia','indrel_1mes','indext','conyuemp','fecha_alta','tiprel_1mes']
df_train.drop(drop_column, axis=1, inplace = True)
 df_test.drop(drop_column, axis=1, inplace = True )<data_type_conversions>
clf5 = KernelRidge(kernel ='rbf', alpha=1.0)
 clf6 = KernelRidge(kernel ='rbf', alpha=1.0)
 clf5.fit(X_train,y_train_values1)
 clf6.fit(X_train,y_train_values2)
 preds1 = clf5.predict(X_valid)
 preds2 = clf6.predict(X_valid)
 y_pred1 = np.exp(preds1)-1
y_pred2 = np.exp(preds2)-1
rsme_valid1 = np.sqrt(mean_squared_error(y_valid_values1,preds1))
rsme_valid2 = np.sqrt(mean_squared_error(y_valid_values2,preds2))
rsme_total = np.sqrt(rsme_valid1*rsme_valid1+rsme_valid2*rsme_valid2)
 print('RSME for formation energy:')
 print(rsme_valid1)
 print('RSME for band gap:')
 print(rsme_valid2)
 print('RSME for total:')
 print(rsme_total )
df_train["renta"] = pd.to_numeric(df_train["renta"], errors="coerce")
 df_test["renta"] = pd.to_numeric(df_test["renta"], errors="coerce" )<feature_engineering>
clf7 = KernelRidge(kernel ='laplacian', alpha=1.0)
 clf8 = KernelRidge(kernel ='laplacian', alpha=1.0)
 clf7.fit(X_train,y_train_values1)
 clf8.fit(X_train,y_train_values2)
 preds1 = clf7.predict(X_valid)
 preds2 = clf8.predict(X_valid)
 y_pred1 = np.exp(preds1)-1
y_pred2 = np.exp(preds2)-1
rsme_valid1 = np.sqrt(mean_squared_error(y_valid_values1,preds1))
rsme_valid2 = np.sqrt(mean_squared_error(y_valid_values2,preds2))
rsme_total = np.sqrt(rsme_valid1*rsme_valid1+rsme_valid2*rsme_valid2)
 print('RSME for formation energy:')
 print(rsme_valid1)
 print('RSME for band gap:')
 print(rsme_valid2)
 print('RSME for total:')
 print(rsme_total )
for cod in unique_prov:
df_test.loc[df_test['cod_prov']==cod,['renta']] = df_test.loc[df_test['cod_prov']==cod,['renta']].fillna({'renta':grouped[cod]} ).values
df_train.loc[df_train['cod_prov']==cod,['renta']] = df_train.loc[df_train['cod_prov']==cod,['renta']].fillna({'renta':grouped[cod]} ).values<correct_missing_values>
X_test = test[x_columns]
X_test = X_test.as_matrix()
preds1 = clf3.predict(X_test)
 preds2 = clf4.predict(X_test)
 y_pred1 = np.exp(preds1)-1
y_pred2 = np.exp(preds2)-1
krr = pd.DataFrame()
krr['id'] = test['id']
krr['formation_energy_ev_natom'] = y_pred1
krr['bandgap_energy_ev'] = y_pred2
krr.to_csv("krr_sub.csv", index=False )