Kaggle competition: Spooky Author Identification
<SOS> metric: MulticlassLoss Kaggle data source: spooky-author-identification<drop_column>
glove_file = '.. /input/glove-global-vectors-for-word-representation/glove.6B.100d.txt'
train_csv = train_csv.drop(["PassengerId", "Name", "Ticket","Cabin"], axis = 1)
 train_csv.head(3 )<drop_column>
df_train = pd.read_csv('.. /input/spooky-author-identification/train.csv')
 df_test = pd.read_csv('.. /input/spooky-author-identification/test.csv')
 df_sample = pd.read_csv('.. /input/spooky-author-identification/sample_submission.csv')
 df_sample.head()
test_csv = test_csv.drop(["Name", "Ticket","Cabin"],axis = 1)
 test_csv.head(3 )<count_missing_values>
stopWords = set(stopwords.words('english'))
train_csv.isnull().sum()<count_values>
def processing(df):
df['processed'] = df['text'].apply(lambda x: re.sub(r'[^\w\s]','', x.lower()))
df['length'] = df['processed'].apply(lambda x: len(x))
df['words'] = df['processed'].apply(lambda x: len(x.split(' ')))
 df['words_not_stopword'] = df['processed'].apply(lambda x: len([t for t in x.split(' ')if t not in stopWords]))
df["num_unique_words"] = df["text"].apply(lambda x: len(set(str(x ).split())))
 df["num_chars"] = df["text"].apply(lambda x: len(str(x)))
 df["num_words_upper"] = df["text"].apply(lambda x: len([w for w in str(x ).split() if w.isupper() ]))
df["num_words_title"] = df["text"].apply(lambda x: len([w for w in str(x ).split() if w.istitle() ]))
df['avg_word_length'] = df['processed'].apply(lambda x: np.mean([len(t)for t in x.split(' ')if t not in stopWords])if len([len(t)for t in x.split(' ')if t not in stopWords])> 0 else 0)
 df['commas'] = df['text'].apply(lambda x: x.count(','))
return(df)
 df_train = processing(df_train)
 df_train.head()
pd.value_counts(train_csv["Embarked"] )<load_from_csv>
features= [c for c in df_train.columns.values if c not in ['id','text','author']]
numeric_features= [c for c in df_train.columns.values if c not in ['id','text','author','processed']]
target = 'author'
X_train, X_test, y_train, y_test = train_test_split(df_train[features], df_train[target], test_size=0.40, random_state=42)
 X_train.head()
_ = train_csv.fillna({"Embarked" : "S"}, inplace = True)
 _ = train_csv.fillna({"Age" : 29.699}, inplace = True )<count_missing_values>
class TextSelector(BaseEstimator, TransformerMixin):

 def __init__(self, key):
self.key = key
def fit(self, X, y=None):
return self
def transform(self, X):
return X[self.key]
class NumberSelector(BaseEstimator, TransformerMixin):

 def __init__(self, key):
self.key = key
def fit(self, X, y=None):
return self
def transform(self, X):
return X[[self.key]]
test_csv.isnull().sum()<load_from_csv>
text = Pipeline([ 
('selector', TextSelector(key='processed')) , 
('tfidf', TfidfVectorizer(stop_words='english'))
])
 text.fit_transform(X_train )
_ = test_csv.fillna({"Fare" : 35.627}, inplace = True)
 _ = test_csv.fillna({"Age" : 30.272}, inplace = True )<categorify>
length = Pipeline([ 
('selector', NumberSelector(key='length')) , 
('standard', StandardScaler())
 ])
 words = Pipeline([ 
('selector', NumberSelector(key='words')) , 
('standard', StandardScaler())
 ])
 words_not_stopword = Pipeline([ 
('selector', NumberSelector(key='words_not_stopword')) , 
('standard', StandardScaler())
 ])
 num_unique_words = Pipeline([ 
('selector', NumberSelector(key='num_unique_words')) , 
('standard', StandardScaler()),
])
 num_chars = Pipeline([ 
('selector', NumberSelector(key='num_chars')) , 
('standard', StandardScaler()),
])
 num_words_upper = Pipeline([ 
('selector', NumberSelector(key='num_words_upper')) , 
('standard', StandardScaler()),
])
 num_words_title = Pipeline([ 
('selector', NumberSelector(key='num_words_title')) , 
('standard', StandardScaler()),
])
 avg_word_length = Pipeline([ 
('selector', NumberSelector(key='avg_word_length')) , 
('standard', StandardScaler())
 ])
 commas = Pipeline([ 
('selector', NumberSelector(key='commas')) , 
('standard', StandardScaler()),
] )
LE = LabelEncoder()
labels = ["Embarked", "Sex"]
for label in labels:
train_csv[label] = LE.fit_transform(train_csv[label])
 test_csv[label] = LE.fit_transform(test_csv[label] )<prepare_x_and_y>
feats = FeatureUnion([('text', text), 
('length', length), 
('words', words), 
('num_unique_words', num_unique_words), 
('num_chars', num_chars), 
('num_words_upper', num_words_upper), 
('num_words_title', num_words_title), 
('words_not_stopword', words_not_stopword), 
('avg_word_length', avg_word_length), 
('commas', commas)])
 feature_processing = Pipeline([('feats', feats)])
 feature_processing.fit_transform(X_train )
x = train_csv.drop(["Survived"], axis = 1)
 y = train_csv["Survived"]<import_modules>
import keras
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.layers.advanced_activations import LeakyReLU<choose_model_class>
alpha = 0.01
dropout = 0.3
epochs = 300
batch_size = 32
model = Sequential()
model.add(Dense(7,input_dim = 7))
model.add(LeakyReLU(alpha = alpha))
model.add(Dropout(dropout))
model.add(Dense(100))
model.add(LeakyReLU(alpha = alpha))
model.add(Dropout(dropout))
model.add(Dense(50))
model.add(LeakyReLU(alpha = alpha))
model.add(Dropout(dropout))
model.add(Dense(25))
model.add(LeakyReLU(alpha = alpha))
model.add(Dropout(dropout))
model.add(Dense(10))
model.add(LeakyReLU(alpha = alpha))
model.add(Dropout(dropout))
model.add(Dense(5))
model.add(LeakyReLU(alpha = alpha))
model.add(Dropout(dropout))
model.add(Dense(1, activation = 'sigmoid'))
model.summary()<train_model>
train_corpus = []
for i in range(len(df_train)) :
review = re.sub('[^a-zA-Z0-9]', ' ', df_train['text'][i])
 review = review.lower()
review = review.split()
review = [word for word in review if not word in set(stopwords.words('english')) ]
review = ' '.join(review)
 train_corpus.append(review)
 test_corpus = []
for i in range(len(df_test)) :
review = re.sub('[^a-zA-Z0-9]', ' ', df_test['text'][i])
 review = review.lower()
review = review.split()
review = [word for word in review if not word in set(stopwords.words('english')) ]
review = ' '.join(review)
 test_corpus.append(review)
 X_Train = np.array(train_corpus)
 X_Test = np.array(test_corpus)
 y = df_train.iloc[:, 2].values
model.compile(loss = 'binary_crossentropy', optimizer = 'Nadam', metrics = ['accuracy'])
 result = model.fit(x, y, epochs = epochs, batch_size = batch_size )<predict_on_test>
test_data = test_csv.drop(['PassengerId'], axis = 1)
 predict = np.round(model.predict(test_data))
prediction = pd.DataFrame(predict )<categorify>
classifier = Pipeline([('vect', CountVectorizer()), 
('tfidf', TfidfTransformer()), 
('clf', MultinomialNB()),
])
 classifier.fit(X_Train, y)
submission = pd.concat([test_csv[['PassengerId']],prediction],axis = 1)
 submission = submission.rename(columns = {0 : 'Survived'})
 _ = submission.fillna({'Survived' : 0}, inplace = True)
 submission = submission.astype(np.int64)
 submission.head()<save_to_csv>
parameters = {'vect__ngram_range': [(1, 1),(1, 2),(1, 3)],
'tfidf__use_idf':(True, False),
'clf__alpha':(0.05, 1.0)
 }
gs_clf = GridSearchCV(classifier, parameters)
 gs_clf.fit(X_Train, y)
submission.to_csv("submission.csv", index = False )<set_options>
gs_clf.best_params_
%matplotlib inline 
<load_from_csv>
gs_clf.refit
y_pred_proba = gs_clf.predict_proba(X_Test)
 tdf = pd.DataFrame(y_pred_proba)
 tdf.columns = ['EAP', 'HPL', 'MWS']
submission = pd.read_csv('.. /input/spooky-author-identification/sample_submission.csv')
 result = pd.concat([submission[['id']], tdf], axis=1)
 result.head()
result.to_csv('naive_bayes_old.csv', index=False)
training_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 submission_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )<train_model>
stop_words = stopwords.words('english' )
print("Training data(rows, columns):" + str(training_data.shape))
print("Submission data(rows, columns):" + str(submission_df.shape)) 
<define_variables>
def multiclass_logloss(actual, predicted, eps=1e-15):

 if len(actual.shape)== 1:
actual2 = np.zeros(( actual.shape[0], predicted.shape[1]))
for i, val in enumerate(actual):
actual2[i, val] = 1
actual = actual2
clip = np.clip(predicted, eps, 1 - eps)
 rows = actual.shape[0]
vsota = np.sum(actual * np.log(clip))
return -1.0 / rows * vsota
training_df = training_data.sample(frac=0.7, random_state=1)
 testing_df = training_data.drop(training_df.index)
 print("Training data(rows, columns):" + str(training_df.shape))
print("Testing data(rows, columns):" + str(testing_df.shape))
print(str(training_data.shape[0] -(training_df.shape[0] + testing_df.shape[0])) + " rows not accounted for")
<drop_column>
lbl_enc = preprocessing.LabelEncoder()
y = lbl_enc.fit_transform(df_train.author.values)
 y
training_labels = training_df["label"].values
training_df = training_df.drop(columns=['label'] )<train_model>
xtrain, xvalid, ytrain, yvalid = train_test_split(df_train.text.values, y,
stratify = y,
random_state = 42,
test_size = 0.1, shuffle = True )
print("Training data(rows, columns):" + str(training_df.shape))
print("Label Count: " + str(training_labels.size))<drop_column>
print('Train and output {} & {}'.format(xtrain.shape, ytrain.shape))
testing_labels = testing_df['label'].values
testing_df = testing_df.drop(columns=['label'] )<categorify>
tfv = TfidfVectorizer(min_df=3, max_features=None,
strip_accents='unicode', analyzer='word', token_pattern=r'\w{1,}',
ngram_range=(1, 3), use_idf=1, smooth_idf=1, sublinear_tf=1,
stop_words = 'english')
 tfv
testing_df /= 255
training_df /= 255<prepare_x_and_y>
tfv.fit(list(xtrain)+ list(xvalid))
xtrain_tfv = tfv.transform(xtrain)
 xvalid_tfv = tfv.transform(xvalid)
 xvalid_tfv.shape
training_values = training_df.values.reshape(training_df.shape[0], IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS)
 testing_values = testing_df.values.reshape(testing_df.shape[0], IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS )<categorify>
clf = LogisticRegression(C=1.0)
 clf.fit(xtrain_tfv, ytrain)
 predictions = clf.predict_proba(xvalid_tfv)
 print("logloss: %0.3f " % multiclass_logloss(yvalid, predictions))
NUM_CLASSES = 10
training_labels_categorical = tf.keras.utils.to_categorical(training_labels)
 testing_labels_categorical = tf.keras.utils.to_categorical(testing_labels)
 print(training_labels_categorical.shape)
 print(testing_labels_categorical.shape )<define_variables>
test_predictions = clf.predict_proba(xvalid_tfv)
 print("logloss: %0.3f " % multiclass_logloss(yvalid, predictions))
print("Tensorflow Version: " + tf.version.VERSION)
 print("Keras Version: " + tf.keras.__version__)
 EPOCHS=35
BATCH_SIZE=64<choose_model_class>
test_tfv = tfv.transform(df_test['text'] )
model = tf.keras.Sequential()
model.add(layers.Conv2D(32, 3, 3, activation='relu', input_shape=(IMG_WIDTH,IMG_HEIGHT,IMG_CHANNELS)))
 model.add(layers.Conv2D(32, 3, 3, activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
 model.add(layers.Dense(128, activation="relu"))
model.add(layers.Dropout(0.50))
model.add(layers.Dense(10, activation="softmax"))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 model.summary()
history = model.fit(training_values, training_labels_categorical, validation_split=0.1, batch_size=BATCH_SIZE, nb_epoch=EPOCHS, verbose=1)
<compute_test_metric>
test_predictions = clf.predict_proba(test_tfv)
 test_predictions.shape
score = model.evaluate(testing_values, testing_labels_categorical, verbose=1)
 print("Model1 Score: " + str(score))<choose_model_class>
tdf = pd.DataFrame(test_predictions)
 tdf.columns = ['EAP', 'HPL', 'MWS']
submission = pd.read_csv('.. /input/spooky-author-identification/sample_submission.csv')
 result = pd.concat([submission[['id']], tdf], axis=1)
 result.head()
result.to_csv('naive_tf_idf.csv', index=False )
model2 = tf.keras.Sequential()
model2.add(layers.Conv2D(32, kernel_size=5, padding='same', activation='relu', input_shape=(IMG_WIDTH,IMG_HEIGHT,IMG_CHANNELS)))
 model2.add(layers.MaxPool2D())
 model2.add(layers.Dropout(0.40))
model2.add(layers.Conv2D(64, kernel_size=5, activation='relu'))
model2.add(layers.MaxPool2D())
 model2.add(layers.Dropout(0.40))
model2.add(layers.Flatten())
 model2.add(layers.Dense(128, activation="relu"))
model2.add(layers.Dropout(0.40))
model2.add(layers.Dense(10, activation="softmax"))
model2.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 model2.summary()
history = model2.fit(training_values, training_labels_categorical, validation_split=0.1, batch_size=BATCH_SIZE, nb_epoch=EPOCHS, verbose=1 )<compute_test_metric>
ctv = CountVectorizer(analyzer='word',token_pattern=r'\w{1,}',
ngram_range=(1, 3), stop_words = 'english')
 ctv.fit(list(xtrain)+ list(xvalid))
xtrain_ctv = ctv.transform(xtrain)
 xvalid_ctv = ctv.transform(xvalid)
score = model2.evaluate(testing_values, testing_labels_categorical, verbose=1)
 print("Model2 Score: " + str(score))<choose_model_class>
clf = LogisticRegression(C=1.0)
 clf.fit(xtrain_ctv, ytrain)
 predictions = clf.predict_proba(xvalid_ctv)
 print("logloss: %0.3f " % multiclass_logloss(yvalid, predictions))
model3 = tf.keras.Sequential()
model3.add(layers.Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model3.add(layers.BatchNormalization())
 model3.add(layers.Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model3.add(layers.BatchNormalization())
 model3.add(layers.MaxPool2D(pool_size=(2,2)))
 model3.add(layers.Dropout(0.25))
model3.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model3.add(layers.BatchNormalization())
 model3.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model3.add(layers.BatchNormalization())
 model3.add(layers.MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model3.add(layers.Dropout(0.25))
model3.add(layers.Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model3.add(layers.BatchNormalization())
 model3.add(layers.Dropout(0.25))
model3.add(layers.Flatten())
 model3.add(layers.Dense(256, activation = "relu"))
model3.add(layers.BatchNormalization())
 model3.add(layers.Dropout(0.25))
model3.add(layers.Dense(10, activation = "softmax"))
model3.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 model3.summary()
history = model3.fit(training_values, training_labels_categorical, validation_split=0.1, batch_size=BATCH_SIZE, nb_epoch=EPOCHS, verbose=1 )<compute_test_metric>
test_predictions = clf.predict_proba(xvalid_ctv)
 print("logloss: %0.3f " % multiclass_logloss(yvalid, predictions))
score = model3.evaluate(testing_values, testing_labels_categorical, verbose=1)
 print("Model3 Score: " + str(score))<choose_model_class>
test_ctv = ctv.transform(df_test['text'] )
modelCheckPoint = ModelCheckpoint(filepath="weights.{epoch:02d}-{val_loss:.2f}.hdf5",
monitor="val_acc",
verbose=1,
save_best_only=True,
mode='max')
 datagen = ImageDataGenerator(
 rotation_range= 8,
zoom_range = 0.13,
width_shift_range=0.13,
height_shift_range=0.13)
 initial_learning_rate = 0.001
optimizer = Adam(lr=initial_learning_rate, decay= initial_learning_rate /(EPOCHS*1.3))
model4 = tf.keras.Sequential()
model4.add(layers.Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model4.add(layers.BatchNormalization())
 model4.add(layers.Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model4.add(layers.BatchNormalization())
 model4.add(layers.MaxPool2D(pool_size=(2,2)))
 model4.add(layers.Dropout(0.25))
model4.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model4.add(layers.BatchNormalization())
 model4.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model4.add(layers.BatchNormalization())
 model4.add(layers.MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model4.add(layers.Dropout(0.25))
model4.add(layers.Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model4.add(layers.BatchNormalization())
 model4.add(layers.Dropout(0.25))
model4.add(layers.Flatten())
 model4.add(layers.Dense(256, activation = "relu"))
model4.add(layers.BatchNormalization())
 model4.add(layers.Dropout(0.25))
model4.add(layers.Dense(10, activation = "softmax"))
model4.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 model4.summary()
history = model4.fit_generator(datagen.flow(training_values, training_labels_categorical, batch_size=BATCH_SIZE), validation_data=(testing_values, testing_labels_categorical), steps_per_epoch=training_values.shape[0] // BATCH_SIZE, epochs=EPOCHS, verbose=1, callbacks=[modelCheckPoint] )<compute_test_metric>
test_predictions = clf.predict_proba(test_ctv)
 test_predictions.shape
score = model4.evaluate(testing_values, testing_labels_categorical, verbose=1)
 print("Score: " + str(score))<save_to_csv>
tdf = pd.DataFrame(test_predictions)
 tdf.columns = ['EAP', 'HPL', 'MWS']
submission = pd.read_csv('.. /input/spooky-author-identification/sample_submission.csv')
 result = pd.concat([submission[['id']], tdf], axis=1)
 result.head()
result.to_csv('naive_ctv.csv', index=False )
submission_df /= 255
submission_values = submission_df.values.reshape(submission_df.shape[0], IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS)
 predictions = model4.predict_classes(submission_values, verbose=0)
 submissions = pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("DR.csv", index=False, header=True )<load_from_csv>
clf = MultinomialNB()
clf.fit(xtrain_tfv, ytrain)
 predictions = clf.predict_proba(xvalid_tfv)
 print("logloss: %0.3f " % multiclass_logloss(yvalid, predictions))
Train_Dir = '.. /input/training/training.csv'
Test_Dir = '.. /input/test/test.csv'
lookid_dir = '.. /input/IdLookupTable.csv'
train_data = pd.read_csv(Train_Dir)
 test_data = pd.read_csv(Test_Dir)
 lookid_data = pd.read_csv(lookid_dir)
 os.listdir('.. /input')
 train_data.fillna(method = 'ffill',inplace = True)
 imag = []
for i in range(0,7049):
img = train_data['Image'][i].split(' ')
 img = ['0' if x == '' else x for x in img]
imag.append(img)
 image_list = np.array(imag,dtype = 'float')
 X_train = image_list.reshape(-1,96,96)
 training = train_data.drop('Image',axis = 1)
 y_train = []
for i in range(0,7049):
y = training.iloc[i,:]
y_train.append(y)
 Y_train = np.array(y_train,dtype = 'float' )<choose_model_class>
clf = MultinomialNB()
clf.fit(xtrain_ctv, ytrain)
 predictions = clf.predict_proba(xvalid_ctv)
 print("logloss: %0.3f " % multiclass_logloss(yvalid, predictions))
<train_model>
classifier = Pipeline([('vect', CountVectorizer()), 
('tfidf', TfidfTransformer()), 
('clf', MultinomialNB()),
])
 classifier.fit(xvalid, yvalid)
def log_test(net,test_iter,ctx):
test_l = 0
for X,y in test_iter:
X, y = X.as_in_context(ctx), y.as_in_context(ctx)
 l = loss(net(X),y)
 test_l = l.mean().asscalar()
return test_l
def train(net, X_train, Y_train,num_epochs, trainer, batch_size, ctx):
train_files,val_files = train_test_split(range(len(X_train)) ,test_size=0.1,shuffle=True)
 train_data,train_label = nd.array(X_train[train_files]),nd.array(Y_train[train_files])
 val_data,val_label = nd.array(X_train[val_files]),nd.array(Y_train[val_files])
 train_iter = gdata.DataLoader(gdata.ArrayDataset(train_data,train_label),batch_size,shuffle=True)
 test_iter = gdata.DataLoader(gdata.ArrayDataset(val_data,val_label),batch_size)
 train_ls,test_ls=[],[]
print('train on:',ctx)
 for epoch in range(num_epochs):
start = time.time()
train_sum_l =0
for X,y in train_iter:
X, y = X.expand_dims(axis=1 ).as_in_context(ctx), y.as_in_context(ctx)
 with autograd.record() :
l = loss(net(X),y)
 l.backward()
trainer.step(batch_size)
 train_loss = loss(net(train_data.expand_dims(axis=1 ).as_in_context(ctx)) ,train_label.as_in_context(ctx)).mean().asscalar()
train_ls.append(train_loss)
 print()
if val_files:
test_ls.append(loss(net(val_data.expand_dims(axis=1 ).as_in_context(ctx)) ,val_label.as_in_context(ctx)).mean().asscalar())
 else:
test_ls.append(train_loss)
 print('epoch %d, train loss %.4f, test loss %.3f, time %.1f sec' %(epoch + 1,train_ls[-1], test_ls[-1], time.time() - start))
return train_ls, test_ls<train_on_grid>
parameters = {'vect__ngram_range': [(1, 1)],
'tfidf__use_idf': [True],
'clf__alpha':(0.05, 0.01)
 }

 gs_clf = GridSearchCV(classifier, parameters)
 gs_clf.fit(xtrain, ytrain )
ctx=mx.gpu(0)
 resnet = vision.resnet34_v1(pretrained=False, ctx=mx.cpu() )<choose_model_class>
gs_clf.best_params_
fine_net = resnet.features
fine_net.add(nn.Dense(30)) 
<choose_model_class>
gs_clf.refit
gs_clf.fit(xtrain, ytrain)
 y_pred_valid = gs_clf.predict_proba(xvalid)
 print("logloss: %0.3f " % multiclass_logloss(y_pred_valid, predictions))
y_pred_valid_f1 = gs_clf.predict(xvalid)
 print(classification_report(yvalid, y_pred_valid_f1))
y_pred_proba = gs_clf.predict_proba(X_Test)
 tdf = pd.DataFrame(y_pred_proba)
 tdf.columns = ['EAP', 'HPL', 'MWS']
submission = pd.read_csv('.. /input/spooky-author-identification/sample_submission.csv')
 result = pd.concat([submission[['id']], tdf], axis=1)
 result.head()
result.to_csv('naive_bayes__2.csv', index=False )
lr, num_epochs = 0.001, 500
batch_size=128
loss = gloss.L2Loss()
net = fine_net
net.initialize(force_reinit=True,ctx=ctx, init=init.Xavier())
 net.collect_params().reset_ctx(ctx)
 trainer = gluon.Trainer(net.collect_params() ,'adam',{'learning_rate':lr} )<train_model>
embeddings_index = {}
f = open(glove_file)
 for line in tqdm(f):
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = coefs
f.close()
print('Found %s word vectors.' % len(embeddings_index))
train_ls,test_ls = train(net, X_train, Y_train,num_epochs, trainer, batch_size, ctx )<prepare_x_and_y>
def sent2vec(s):
words = str(s ).lower()
words = word_tokenize(words)
 words = [w for w in words if not w in stop_words]
words = [w for w in words if w.isalpha() ]
M = []
for w in words:
try:
M.append(embeddings_index[w])
 except:
continue
M = np.array(M)
 v = M.sum(axis=0)
 if type(v)!= np.ndarray:
return np.zeros(300)
 return v / np.sqrt(( v ** 2 ).sum() )
<save_to_csv><EOS> 
clf.fit(xtrain_ctv, ytrain)
 predictions = clf.predict_proba(xvalid_ctv)
 print("logloss: %0.3f " % multiclass_logloss(yvalid, predictions))