Kaggle competition: Spooky Author Identification
<SOS> metric: MulticlassLoss Kaggle data source: spooky-author-identification<save_to_csv>
np.random.seed(666)
 print(check_output(["ls", ".. /input"] ).decode("utf8"))
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 sample = pd.read_csv(".. /input/sample_submission.csv")
 train["EAP"] =(train.author=="EAP")*1
train["HPL"] =(train.author=="HPL")*1
train["MWS"] =(train.author=="MWS")*1
train.drop("author", 1, inplace=True)
 target_vars = ["EAP", "HPL", "MWS"]
train.head(2 )
def output_prediction(y_pred, model_name):
print(y_pred)
 data_predict = {"ImageId":range(1, n_samples_test+1), "Label":y_pred}
data_predict = pd.DataFrame(data_predict)
 data_predict.to_csv("dr output %s.csv" %model_name, index = False )<import_modules>
eng_stopwords = set(stopwords.words("english"))
train["num_words"] = train["text"].apply(lambda x: len(str(x ).split()))
test["num_words"] = test["text"].apply(lambda x: len(str(x ).split()))
train["num_unique_words"] = train["text"].apply(lambda x: len(set(str(x ).split())))
 test["num_unique_words"] = test["text"].apply(lambda x: len(set(str(x ).split())))
 train["num_chars"] = train["text"].apply(lambda x: len(str(x)))
 test["num_chars"] = test["text"].apply(lambda x: len(str(x)))
 train["num_stopwords"] = train["text"].apply(lambda x: len([w for w in str(x ).lower().split() if w in eng_stopwords]))
test["num_stopwords"] = test["text"].apply(lambda x: len([w for w in str(x ).lower().split() if w in eng_stopwords]))
train["num_punctuations"] =train['text'].apply(lambda x: len([c for c in str(x)if c in string.punctuation]))
test["num_punctuations"] =test['text'].apply(lambda x: len([c for c in str(x)if c in string.punctuation]))
train["num_words_upper"] = train["text"].apply(lambda x: len([w for w in str(x ).split() if w.isupper() ]))
test["num_words_upper"] = test["text"].apply(lambda x: len([w for w in str(x ).split() if w.isupper() ]))
train["num_words_title"] = train["text"].apply(lambda x: len([w for w in str(x ).split() if w.istitle() ]))
test["num_words_title"] = test["text"].apply(lambda x: len([w for w in str(x ).split() if w.istitle() ]))
train["mean_word_len"] = train["text"].apply(lambda x: np.mean([len(w)for w in str(x ).split() ]))
test["mean_word_len"] = test["text"].apply(lambda x: np.mean([len(w)for w in str(x ).split() ]))
num_vars = ["mean_word_len", "num_words_title", "num_punctuations", "num_chars"
, "num_stopwords", "num_chars", "num_unique_words", "num_words"]
mod_name = "sklearn.naive_bayes"
model_name = "GaussianNB" 
<define_variables>
stemmer = stm.SnowballStemmer("english")
 train["stem_text"] = train.text.apply(lambda x:(" " ).join([stemmer.stem(z)for z in re.sub("[^a-zA-Z0-9]"," ", x ).split(" ")]))
test["stem_text"] = test.text.apply(lambda x:(" " ).join([stemmer.stem(z)for z in re.sub("[^a-zA-Z0-9]"," ", x ).split(" ")]))
tok_raw = Tokenizer()
tok_raw.fit_on_texts(train.text.str.lower())
 tok_stem = Tokenizer()
tok_stem.fit_on_texts(train.stem_text)
 train["seq_text_stem"] = tok_stem.texts_to_sequences(train.stem_text)
 test["seq_text_stem"] = tok_stem.texts_to_sequences(test.stem_text)
mod_name = "sklearn.neighbors"
model_name = "KNeighborsClassifier" 
<define_variables>
def get_keras_data(dataset, maxlen=20, scaler=None, tdfidf=None):
if scaler==None:
scaler = StandardScaler()
scaler.fit(dataset[num_vars])
 if tdfidf==None:
tdfidf = Pipeline(steps=[('tdfidf', TfidfVectorizer(analyzer='word', binary=False
, ngram_range=(1, 4), stop_words="english"))
,('svd', TruncatedSVD(algorithm='randomized', n_components=20, n_iter=10,
random_state=None, tol=0.0)
)])
 tdfidf.fit(dataset.text)
 X = {
"stem_input": pad_sequences(dataset.seq_text_stem, maxlen=maxlen),
"num_input": scaler.transform(dataset[num_vars]),
"svd_vect": tdfidf.transform(dataset.text)
 }
return X, scaler, tdfidf
maxlen = 60
dtrain, dvalid = train_test_split(train, random_state=123, train_size=0.9)
 print("processing train...")
 X_train, scaler, tdfidf = get_keras_data(dtrain, maxlen)
 y_train = np.array(dtrain[target_vars])
 print("processing valid...")
 X_valid, _, _ = get_keras_data(dvalid, maxlen, scaler, tdfidf)
 y_valid = np.array(dvalid[target_vars])
 print("processing test...")
 X_test, _, _ = get_keras_data(test, maxlen, scaler, tdfidf)
 n_stem_seq = np.max([np.max(X_valid["stem_input"]), np.max(X_train["stem_input"])])+1
mod_name = "sklearn.cluster"
model_name = "KMeans" 
<define_variables>
def get_callbacks(filepath, patience=2):
es = EarlyStopping('val_loss', patience=patience, mode="min")
 msave = ModelCheckpoint(filepath, save_best_only=True)
 return [es, msave]
def get_model() :
embed_dim = 30
dropout_rate = 0.9
emb_dropout_rate = 0.9
input_text = Input(shape=[maxlen], name="stem_input")
 input_num = Input(shape=[X_train["num_input"].shape[1]], name="num_input")
 input_svd = Input(shape=[X_train["svd_vect"].shape[1]], name="svd_vect")
 emb_lstm = SpatialDropout1D(emb_dropout_rate )(Embedding(n_stem_seq, embed_dim
,input_length = maxlen
)(input_text))
concatenate = Concatenate()([(Flatten()(emb_lstm)) , input_num, input_svd])
 dense = Dropout(dropout_rate )(Dense(256 )(concatenate))
output = Dense(3, activation="softmax" )(dense)
 model = Model([input_text, input_num, input_svd], output)
 optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
 model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"])
 return model
model = get_model()
model.summary()
mod_name = "sklearn.svm"
model_name = "SVC" 
<define_variables>
file_path = ".model_weights.hdf5"
callbacks = get_callbacks(filepath=file_path, patience=5)
 model = get_model()
model.fit(X_train, y_train, epochs=150
, validation_data=[X_valid, y_valid]
, batch_size=512
, callbacks = callbacks )
mod_name = "sklearn.tree"
model_name = "DecisionTreeClassifier" 
<define_variables>
model = get_model()
model.load_weights(file_path)
 preds_train = model.predict(X_train)
 preds_valid = model.predict(X_valid)
 print(log_loss(y_train, preds_train))
print(log_loss(y_valid, preds_valid))
<data_type_conversions><EOS> 
preds = pd.DataFrame(model.predict(X_test), columns=target_vars)
 submission = pd.concat([test["id"],preds], 1)
 submission.to_csv("./submission.csv", index=False)
 submission.head()