Kaggle competition: Spooky Author Identification
<SOS> metric: MulticlassLoss Kaggle data source: spooky-author-identification<choose_model_class>
%matplotlib inline
np.random.seed(25)
 print(check_output(["ls", ".. /input"] ).decode("utf8"))
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
NN = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(1000, 2), random_state=100 )<choose_model_class>
mapping_target = {'EAP':0, 'HPL':1, 'MWS':2}
train = train.replace({'author':mapping_target} )
GBR = GradientBoostingClassifier(n_estimators=2000, max_depth=30, max_leaf_nodes=8, min_samples_split=5, random_state =10 )<train_on_grid>
test_id = test['id']
target = train['author']
param_grid = {'n_estimators': [2000], 'max_depth' : [20], 'random_state' : [0, 5,10]}
grid_search = GridSearchCV(RF, param_grid, cv=5 )<predict_on_test>
def preprocess(text):
text = text.strip()
text = text.replace("' ", " ' ")
 signs = set(',.:;"?!')
 prods = set(text)& signs
if not prods:
return text
for sign in prods:
text = text.replace(sign, ' {} '.format(sign))
return text
def cleanData(text, lowercase = False, remove_stops = False, stemming = False, lemmatization = False):
txt = str(text)
 txt = re.sub(r'[^A-Za-z\s]',r' ',txt)
 if lowercase:
txt = " ".join([w.lower() for w in txt.split() ])
 if remove_stops:
txt = " ".join([w for w in txt.split() if w not in stops])
 if stemming:
st = PorterStemmer()
txt = " ".join([st.stem(w)for w in txt.split() ])
 if lemmatization:
wordnet_lemmatizer = WordNetLemmatizer()
txt = " ".join([wordnet_lemmatizer.lemmatize(w, pos='v')for w in txt.split() ])
 return txt
bagging = BaggingClassifier(n_estimators=2000, max_samples=1.0, max_features=1.0, n_jobs=-1, random_state =10)
<choose_model_class>
train['text'] = train['text'].map(lambda x: preprocess(x))
test['text'] = test['text'].map(lambda x: preprocess(x))
Extraforest = ExtraTreesClassifier(n_estimators=2000, random_state=10 )<define_search_space>
train['text'] = train['text'].map(lambda x: cleanData(x, lowercase=True, remove_stops=False, stemming=False, lemmatization = False))
test['text'] = test['text'].map(lambda x: cleanData(x, lowercase=True, remove_stops=False, stemming=False, lemmatization = False))
votemodel = VotingClassifier(estimators=[('LR', LR),('RF', RF),('GBR', GBR),('ADA', ADA),('Extraforest', Extraforest), 
('bagging_Decision tree', bagging)],voting='hard',weights=[1,1,1,1,1,1])
<create_dataframe>
np.random.seed(25)
submission_bagging=pd.DataFrame({'PassengerId':df_all['PassengerId'][df_all['Survived'].isnull() ], 'Survived':bagging.predict(x_test ).astype(int)})
 submission_votemodel=pd.DataFrame({'PassengerId':df_all['PassengerId'][df_all['Survived'].isnull() ], 'Survived':votemodel.predict(x_test ).astype(int)} )<categorify>
MAX_SEQUENCE_LENGTH = 256
MAX_NB_WORDS = 200000
submission_bagging = submission_bagging.reset_index(drop=True)
 submission_votemodel = submission_votemodel.reset_index(drop=True )<save_to_csv>
def add_ngram(q, n_gram_max):
ngrams = []
for n in range(2, n_gram_max+1):
for w_index in range(len(q)-n+1):
ngrams.append('--'.join(q[w_index:w_index+n]))
return q + ngrams
submission_votemodel.to_csv("submission_v4_vm.csv", index=False)
 submission_bagging.to_csv("submission_v4_bagging.csv", index=False)
 print('Save submission', datetime.now() , )<init_hyperparams>
n_gram_max = 2
print('Processing text dataset')
 texts_1 = []
for text in train['text']:
text = text.split()
texts_1.append(' '.join(add_ngram(text, n_gram_max)))
 labels = train['author']
print('Found %s texts.' % len(texts_1))
test_texts_1 = []
for text in test['text']:
text = text.split()
test_texts_1.append(' '.join(add_ngram(text, n_gram_max)))
 print('Found %s texts.' % len(test_texts_1))
X = x_train
y = y
ada_parameters = {'n_estimators':500, 'learning_rate':0.1}
et_parameters = {'n_jobs': -1, 'n_estimators':500, 'max_depth': 8, 'min_samples_leaf': 2, 'verbose': 0}
gb_parameters = {'n_estimators': 500, 'max_depth': 5, 'min_samples_leaf': 2, 'verbose': 0}
dt_parameters = {'max_depth':8}
knn_parameters = {'n_neighbors':2}
svm_parameters = {'kernel':'linear', 'C':0.025}
gbm_parameters = {'n_estimators': 2000, 'max_depth': 4, 'min_child_weight': 2, 'gamma':0.9, 'subsample':0.8,
'colsample_bytree':0.8, 'objective': 'binary:logistic', 'nthread':-1, 'scale_pos_weight':1}
title = "Learning Curves(RandomForest)"
plot_learning_curve(votemodel, title, X, y, ylim=(0.5, 1.01), cv=stratifiedkfold, n_jobs=4, train_sizes=[200, 300, 400, 500, 600])
 plt.show()<set_options>
min_count = 2
tokenizer = Tokenizer(lower=False, filters='')
 tokenizer.fit_on_texts(texts_1)
 num_words = sum([1 for _, v in tokenizer.word_counts.items() if v >= min_count])
 tokenizer = Tokenizer(num_words=num_words, lower=True, filters='')
 tokenizer.fit_on_texts(texts_1)
 sequences_1 = tokenizer.texts_to_sequences(texts_1)
 test_sequences_1 = tokenizer.texts_to_sequences(test_texts_1)
 data_1 = pad_sequences(sequences_1, maxlen=MAX_SEQUENCE_LENGTH)
 labels = np.array(labels)
 print('Shape of data tensor:', data_1.shape)
 print('Shape of label tensor:', labels.shape)
 test_data_1 = pad_sequences(test_sequences_1, maxlen=MAX_SEQUENCE_LENGTH)
 del test_sequences_1
del sequences_1
gc.collect()
%matplotlib inline
plt.style.use('seaborn')
<load_from_csv>
nb_words = np.max(data_1)+ 1
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<drop_column>
model = Sequential()
model.add(Embedding(nb_words,20,input_length=MAX_SEQUENCE_LENGTH))
model.add(GlobalAveragePooling1D())
 model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics = ['accuracy'] )
train.drop(['Id'], axis=1, inplace=True)
 test.drop(['Id'], axis=1, inplace=True )<concatenate>
model.fit(data_1, to_categorical(labels), validation_split=0.2, nb_epoch=15, batch_size=16 )
df = pd.concat([train.SalePrice, np.log(train.SalePrice + 1 ).rename('LogSalePrice')], axis=1, names=['SalePrice', 'LogSalePrice'])
 df.head()<feature_engineering>
preds = model.predict(test_data_1 )
<prepare_x_and_y><EOS> 
result = pd.DataFrame()
result['id'] = test_id
result['EAP'] = [x[0] for x in preds]
result['HPL'] = [x[1] for x in preds]
result['MWS'] = [x[2] for x in preds]
result.to_csv("result.csv", index=False )