Kaggle competition: Spooky Author Identification
<SOS> metric: MulticlassLoss Kaggle data source: spooky-author-identification<import_modules>
np.random.seed(7 )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import time
import datetime
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from scipy.stats import skew<load_from_csv>
df = pd.read_csv('./.. /input/train.csv')
 df_test = pd.read_csv('./.. /input/test.csv')
 df_full = df.append(df_test, sort=False)
 a2c = {'EAP': 0, 'HPL' : 1, 'MWS' : 2}
y = np.array([a2c[a] for a in df.author])
 y = to_categorical(y)
 y
train_data = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv', index_col=0)
 test_data = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv', index_col=0)
 data = train_data.append(test_data )<data_type_conversions>
def preprocess(text):
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
for col in data:
datatype = data[col].dtype
if datatype == np.int64 or datatype == np.float64:
data[col] = data[col].fillna(data[col].mean() )<feature_engineering>
def create_docs(df, n_gram_max=2):
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
docs = []
for doc in df.text:
doc = preprocess(doc ).split()
docs.append(' '.join(add_ngram(doc, n_gram_max)))
 return docs
data['GrLivArea'] = data['GrLivArea'].clip(0, 3500)
 data['TotalBsmtSF'] = data['TotalBsmtSF'].clip(0, 3500)
 data['1stFlrSF'] = data['1stFlrSF'].clip(0, 3500 )<feature_engineering>
min_count = 2
docs = create_docs(df)
 tokenizer = Tokenizer(lower=False, filters='')
 tokenizer.fit_on_texts(docs)
 num_words = sum([1 for _, v in tokenizer.word_counts.items() if v >= min_count])
 num_words
data['GarageArea'] = data['GarageArea'].clip(0, 1200 )<feature_engineering>
tokenizer = Tokenizer(num_words=num_words, lower=False, filters='')
 tokenizer.fit_on_texts(docs_full)
 docs = tokenizer.texts_to_sequences(docs)
 print("Samples Number:", len(docs))
print("Sample 1:
{}
Sample 2:
{}".format(docs[0], docs[1]))
data['YearBuilt'] = data['YearBuilt'].clip(1950, data['YearBuilt'].max())
 data['YearRemodAdd'] = data['YearRemodAdd'].clip(1950, data['YearRemodAdd'].max() )<feature_engineering>
max_size = 0
for ind, text in enumerate(docs):
max_size = len(text)if len(text)> max_size else max_size
print("Max number of words in a sample for full dataset:", max_size )
data['BsmtFinSF1'] = data['BsmtFinSF1'].clip(0, 2500)
 data['OpenPorchSF'] = data['OpenPorchSF'].clip(0, 400)
 data['LotArea'] = data['LotArea'].clip(0, 70000 )<drop_column>
maxlen = 256
docs = pad_sequences(sequences=docs, maxlen=max_size)
 print("Samples Number:", len(docs))
print("Sample 1:
{}
Sample 2:
{}".format(docs[0][-40:], docs[1][-40:]))
data = data.drop(columns=['PoolArea'] )<feature_engineering>
input_dim = np.max(docs)+ 1
embedding_dims = 20
input_dim
data = data.drop(columns=[
'KitchenAbvGr',
'BsmtFullBath',
'BsmtHalfBath'
])
 data['TotRmsAbvGrd'] = data['TotRmsAbvGrd'].clip(0, 11)
 data['HalfBath'] = data['HalfBath'].clip(0, 1)
 data['BedroomAbvGr'] = data['BedroomAbvGr'].clip(0, 4 )<feature_engineering>
x_train, x_test, y_train, y_test = train_test_split(docs, y, test_size=0.20 )
data['GarageYrBlt'] = data['GarageYrBlt'].clip(1950, data['GarageYrBlt'].max() )<data_type_conversions>
def create_model(embedding_dims=20, optimizer='adam'):
model = Sequential()
model.add(Embedding(input_dim=input_dim, output_dim=embedding_dims))
model.add(GlobalAveragePooling1D())
 model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
 return model
train_data['DateSold'] = '01/' + train_data['MoSold'].astype(str)+ '/' + train_data['YrSold'].astype(str)
 train_data['DateSold'] = pd.to_datetime(train_data['DateSold'], format='%d/%m/%Y')
 train_data['DateSold'] =(train_data['DateSold'] - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
 train_data.plot.scatter(x="DateSold", y="SalePrice" )<data_type_conversions>
epochs = 500
hist = model.fit(x_train, y_train,
batch_size=16,
validation_data=(x_test, y_test),
epochs=epochs,
callbacks=[EarlyStopping(patience=2, monitor='val_loss')] )
data['DateSold'] = '01/' + data['MoSold'].astype(str)+ '/' + data['YrSold'].astype(str)
 data['DateSold'] = pd.to_datetime(data['DateSold'], format='%d/%m/%Y')
 data['DateSold'] =(data['DateSold'] - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
 data = data.drop(columns=['MoSold', 'YrSold'] )<sort_values>
test_df = pd.read_csv('.. /input/test.csv')
 docs = create_docs(test_df)
 docs = tokenizer.texts_to_sequences(docs)
 docs = pad_sequences(sequences=docs, maxlen=maxlen)
 y = model.predict_proba(docs)
 result = pd.read_csv('.. /input/sample_submission.csv')
 for a, i in a2c.items() :
result[a] = y[:, i]
<feature_engineering><EOS> 
result.to_csv('fastText_result_01.csv', index=False )