Kaggle competition: Don't Overfit! II
<SOS> metric: AUC Kaggle data source: dont-overfit!-ii<categorify>
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
for f in categorical:
	if train_df[f].dtype=='object':
		
		lbl = preprocessing.LabelEncoder()
		lbl.fit(list(train_df[f].values)+ list(test_df[f].values))
		train_df[f] = lbl.transform(list(train_df[f].values))
		test_df[f] = lbl.transform(list(test_df[f].values))
		features_to_use.append(f )<string_transform>
train_df = pd.read_csv(".. /input/train.csv" ).drop('id', axis=1)
 train_df.head()
train_df['features'] = train_df["features"].apply(lambda x: " ".join(["_".join(i.split(" ")) for i in x]))
test_df['features'] = test_df["features"].apply(lambda x: " ".join(["_".join(i.split(" ")) for i in x]))<categorify>
test_df = pd.read_csv('.. /input/test.csv' ).drop('id', axis = 1)
 test_df.head()
def factorize(df1, df2, column):
ps = df1[column].append(df2[column])
 factors = ps.factorize() [0]
df1[column] = factors[:len(df1)]
df2[column] = factors[len(df1):]
return df1, df2<categorify>
y = train_df['target']
X = train_df.drop('target', axis=1 )
for col in('building_id', 'display_address', 'manager_id', 'street_address'):
train_df,test_df = factorize(train_df, test_df,col )<categorify>
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42 )
tfidf = CountVectorizer(stop_words='english', max_features=200)
 tfidf.fit(list(train_df['features'])+ list(test_df['features']))
tr_sparse = tfidf.transform(train_df["features"])
 te_sparse = tfidf.transform(test_df["features"])
 print(te_sparse )<concatenate>
best_score = 0
for penalty in ['l1', 'l2']:
for C in [0.001, 0.01, 0.1, 1, 10, 100]:
logreg = LogisticRegression(class_weight='balanced', penalty=penalty, C=C, solver='liblinear')
 logreg.fit(X_train, y_train)
 score = logreg.score(X_test, y_test)
 if score > best_score:
best_score = score
best_parameters = {'C': C, 'penalty': penalty}
train_X = sparse.hstack([train_df[features_to_use], tr_sparse] ).tocsr()<concatenate>
logreg = LogisticRegression(**best_parameters)
 logreg.fit(X_train, y_train)
 test_score = logreg.score(X_test, y_test )
test_X = sparse.hstack([test_df[features_to_use], te_sparse] ).tocsr()<prepare_x_and_y>
print("Best score: {:.2f}".format(best_score))
print("Best parameters: {}".format(best_parameters))
print("Best score on test data: {:.2f}".format(test_score))
<save_to_csv><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = logreg.predict_proba(test_df)[:,1]
sub.to_csv('submission.csv', index=False )