Kaggle competition: Don't Overfit! II
<SOS> metric: AUC Kaggle data source: dont-overfit!-ii<import_modules>
warnings.filterwarnings('ignore' )
%matplotlib inline
print(check_output(["ls", ".. /input"] ).decode("utf8")) 
<load_from_csv>
df_train = pd.read_csv(".. /input/train.csv")
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
y = df_train["target"]
df_train.pop("target");
df_train.pop("id")
 colnames1 = df_train.columns
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<categorify>
scaler = StandardScaler()
scaler.fit(df_train)
 X = scaler.transform(df_train)
 df_train = pd.DataFrame(data = X, columns=colnames1 )
le = LabelEncoder()
y = le.fit_transform(train.type.values)
 test_ids = test.id
train.drop(['type','id'],axis=1,inplace=True)
 test.drop(['id'],axis=1,inplace=True)
 all_data = pd.concat(( train,test),axis=0 ).reset_index()
all_data['hair_bone'] = all_data['hair_length'] * all_data['bone_length']
all_data['hair_rotting'] = all_data['hair_length'] * all_data['rotting_flesh']
all_data['hair_soul'] = all_data['hair_length'] * all_data['has_soul']
all_data['hair_soul_bone'] = all_data['hair_length'] * all_data['has_soul'] * all_data['bone_length']
all_data['hair_rotting_soul'] = all_data['hair_length'] * all_data['rotting_flesh'] * all_data['has_soul']
all_data['hair_bone_rotting'] = all_data['hair_length'] * all_data['bone_length'] *all_data['rotting_flesh']
all_data['rotting_hair_soul'] = all_data['rotting_flesh'] * all_data['hair_length'] * all_data['has_soul']
all_data['rotting_bone_soul'] = all_data['rotting_flesh'] * all_data['bone_length'] * all_data['has_soul']
all_data['bone_rotting_soul'] = all_data['bone_length'] * all_data['rotting_flesh'] * all_data['has_soul']
all_data['bone_rotting'] = all_data['bone_length'] * all_data['rotting_flesh']
all_data['bone_soul'] = all_data['bone_length'] * all_data['has_soul']
all_data['rotting_soul'] = all_data['rotting_flesh'] * all_data['has_soul']
all_data.drop(['index','color'],axis=1,inplace=True )<categorify>
random_state = 0
clf = LogisticRegression(random_state = random_state)
 param_grid = {'class_weight' : ['balanced'],
'penalty' : ['l1'],
'C' : [0.0001, 0.0005, 0.001,
0.005, 0.01, 0.05, 0.1, 0.5, 1,
10, 100, 1000, 1500, 2000, 2500,
2600, 2700, 2800, 2900, 3000, 3100, 3200
] ,
'max_iter' : [100, 1000, 2000, 5000, 10000] }
scorer = make_scorer(roc_auc_score)
 grid = GridSearchCV(estimator = clf, param_grid = param_grid ,
scoring = scorer, verbose = 1, cv=20,
n_jobs = -1)
 X = df_train.values
grid.fit(X,y)
 print("Best Score:" + str(grid.best_score_))
best_parameters = grid.best_params_
<normalization>
best_clf = grid.best_estimator_
print(best_clf )
columns = all_data.columns
n_trains = train.shape[0]
scaler = StandardScaler().fit(all_data)
 x_train = scaler.transform(all_data.iloc[:n_trains])
 x_test = scaler.transform(all_data.iloc[n_trains:] )<train_model>
model = LogisticRegression(C=0.1, class_weight='balanced', dual=False,
fit_intercept=True, intercept_scaling=1, max_iter=100,
multi_class='warn', n_jobs=None, penalty='l1', random_state=0,
solver='warn', tol=0.0001, verbose=0, warm_start=False);
model.fit(X, y);
clf = RandomForestClassifier(n_estimators=300)
 clf.fit(x_train,y)
 indices = np.argsort(clf.feature_importances_)[::-1]
for idx in range(x_train.shape[1]):
print('%d feature %d %s(%f)' %(idx + 1,
indices[idx],
columns[indices[idx]],
clf.feature_importances_[indices[idx]])) 
<train_on_grid>
df_test = pd.read_csv(".. /input/test.csv")
 df_test.pop("id");
X = df_test
X = scaler.transform(X)
 df_test = pd.DataFrame(data = X, columns=colnames1)
 X = df_test.values
y_pred = model.predict_proba(X)
 y_pred = y_pred[:,1]
clf = RandomForestClassifier()
param = {'n_estimators':[150,250,350],
'criterion' : ['gini', 'entropy'],
'max_features' : ['auto', 'sqrt', 'log2', None]
}
optimzation = GridSearchCV(clf,param_grid = param , cv=5 , scoring='accuracy')
 optimzation.fit(x_train,y)
 print('Best score : ' , optimzation.best_score_)
 print('Best parameter : ' , optimzation.best_params_ )<train_on_grid>
smpsb_df = pd.read_csv(".. /input/sample_submission.csv")
 smpsb_df["target"] = y_pred
smpsb_df.to_csv("submission.csv", index=None)