Kaggle competition: Don't Get Kicked!
output = pd.DataFrame({'ImageId': list(range(1, len(preds)+1)) ,
'Label':preds})
 output.to_csv('output2.csv', index=False )<import_modules>
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPool2D, BatchNormalization
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.losses import categorical_crossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping<define_variables>
test_data=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()
train_path = '.. /input/digit-recognizer/train.csv'
test_path = '.. /input/digit-recognizer/test.csv'<load_from_csv>
train_data.isnull().sum()
train_data = pd.read_csv(train_path)
 test_data = pd.read_csv(test_path )<count_unique_values>
test_data.isnull().sum()
img_rows, img_cols = 28, 28
num_classes = train_data.label.nunique()<categorify>
train_data['IsBadBuy'].value_counts()
def data_prep(raw_data, img_row, img_col, num_classes):
out_y = to_categorical(raw_data.label, num_classes)
 num_images = raw_data.shape[0]
x_as_array = raw_data.values[:, 1:]
x_as_matrix = x_as_array.reshape(num_images, img_row, img_col, 1)
 out_x = x_as_matrix/255.0
return out_x, out_y<prepare_x_and_y>
train_data['Model'].value_counts()
X, y = data_prep(train_data, img_rows, img_cols, num_classes )<choose_model_class>
train_data.drop('Model',axis=1,inplace=True)
 test_data.drop("Model",axis=1,inplace=True )
model = Sequential()<choose_model_class>
train_data['Trim'].value_counts()
model.add(Conv2D(filters=40, kernel_size=(3,3), padding='Same', activation='relu', input_shape=(img_rows, img_cols, 1)))
 model.add(MaxPool2D(2,2))
model.add(BatchNormalization(axis=1))
model.add(Conv2D(filters=60, kernel_size=(3,3), padding='Same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(BatchNormalization(axis=1))
model.add(Dropout(0.25))
model.add(Conv2D(filters=80, kernel_size=(3,3), padding='Same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(BatchNormalization(axis=1))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(BatchNormalization(axis=1))
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization(axis=1))
model.add(Dropout(0.50))
model.add(BatchNormalization(axis=1))
model.add(Dense(10, activation='softmax'))
model.summary()<choose_model_class>
train_data.drop('Trim',inplace=True,axis=1)
 test_data.drop('Trim',inplace=True,axis=1 )
model.compile(loss=categorical_crossentropy,
optimizer=Adam(0.001),
metrics=['accuracy'] )<train_model>
train_data['SubModel'].value_counts()
<train_model>
train_data.drop('SubModel',inplace=True,axis=1)
 test_data.drop('SubModel',inplace=True,axis=1 )
history = model.fit(X, y, epochs=20, validation_split=0.2 )<train_model>
train_data['Color'].value_counts()
test_num_img = test_data.shape[0]
test_data_array = test_data.values
test_data_matrix = test_data_array.reshape(test_num_img, 28,28,1)
 test_data_matrix = test_data_matrix/255<predict_on_test>
test_data['Color'].value_counts()
preds = model.predict_classes(test_data_matrix )<save_to_csv>
train_data['Color'].fillna(value='Color_Unknown',inplace=True)
 test_data['Color'].fillna(value='Color_Unknown',inplace=True )
output = pd.DataFrame({'ImageId': list(range(1, len(preds)+1)) ,
'Label':preds})
 output.to_csv('output2.csv', index=False )<import_modules>
print("Number of null values in Color column "+str(train_data['Color'].isnull().sum()))
print("Number of null values in Color column "+str(test_data['Color'].isnull().sum()))
import plotly.graph_objects as go
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm
import time
from datetime import datetime
from pathlib import Path
from sklearn import preprocessing
import keras.backend as K
from keras.models import Sequential
from keras.layers import Dense, LSTM, RNN, Dropout
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras import optimizers
from sklearn.preprocessing import StandardScaler, MinMaxScaler, LabelEncoder
from sklearn.model_selection import train_test_split<load_from_csv>
train_data['Transmission'].value_counts()
train = pd.read_csv(r'/kaggle/input/covid19-global-forecasting-week-3/train.csv', parse_dates=['Date'])
 test = pd.read_csv(r'/kaggle/input/covid19-global-forecasting-week-3/test.csv',parse_dates=['Date'])
 submission = pd.read_csv(r'/kaggle/input/covid19-global-forecasting-week-3/submission.csv')
 train.tail()<count_values>
test_data['Transmission'].value_counts()
mask = train['Date'].max()
world_cum_confirmed = sum(train[train['Date'] == mask].ConfirmedCases)
 world_cum_fatal = sum(train[train['Date'] == mask].Fatalities)
 print('Number of Countires are: ', len(train['Country_Region'].unique()))
print('Training dataset ends at: ', mask)
 print('Number of cumulative confirmed cases worldwide are: ', world_cum_confirmed)
 print('Number of cumulative fatal cases worldwide are: ', world_cum_fatal )<sort_values>
train_data[train_data['Transmission']=='Manual']
cum_per_country = train[train['Date'] == mask].groupby(['Date','Country_Region'] ).sum().sort_values(['ConfirmedCases'], ascending=False)
 cum_per_country[:10]<groupby>
train_data['Transmission'].replace("Manual","MANUAL",inplace=True )
date = train['Date'].unique()
cc_us = train[train['Country_Region'] == 'US'].groupby(['Date'] ).sum().ConfirmedCases
ft_us = train[train['Country_Region'] == 'US'].groupby(['Date'] ).sum().Fatalities
cc_ity = train[train['Country_Region'] == 'Italy'].groupby(['Date'] ).sum().ConfirmedCases
ft_ity = train[train['Country_Region'] == 'Italy'].groupby(['Date'] ).sum().Fatalities
cc_spn = train[train['Country_Region'] == 'Spain'].groupby(['Date'] ).sum().ConfirmedCases
ft_spn = train[train['Country_Region'] == 'Spain'].groupby(['Date'] ).sum().Fatalities
cc_gmn = train[train['Country_Region'] == 'Germany'].groupby(['Date'] ).sum().ConfirmedCases
ft_gmn = train[train['Country_Region'] == 'Germany'].groupby(['Date'] ).sum().Fatalities
cc_frc = train[train['Country_Region'] == 'France'].groupby(['Date'] ).sum().ConfirmedCases
ft_frc = train[train['Country_Region'] == 'France'].groupby(['Date'] ).sum().Fatalities
fig = go.Figure()
fig.add_trace(go.Scatter(x=date, y=cc_us, name='US'))
fig.add_trace(go.Scatter(x=date, y=cc_ity, name='Italy'))
fig.add_trace(go.Scatter(x=date, y=cc_spn, name='Spain'))
fig.add_trace(go.Scatter(x=date, y=cc_gmn, name='Germany'))
fig.add_trace(go.Scatter(x=date, y=cc_frc, name='France'))
fig.update_layout(title="Plot of Cumulative Cases for Top 5 countires(except China)",
xaxis_title="Date",
yaxis_title="Cases")
 fig.update_xaxes(nticks=30)
 fig.show()<data_type_conversions>
train_data['Transmission'].value_counts()
train.columns = train.columns.str.lower()
test.columns = test.columns.str.lower()<drop_column>
train_data['Transmission'].fillna(value="Transmission_unk",inplace=True)
 test_data['Transmission'].fillna(value="Transmission_unk",inplace=True )
train.fillna(' ',inplace=True)
 test.fillna(' ', inplace=True)
 train_id = train.pop('id')
 test_id = test.pop('forecastid')
 train['cp'] = train['country_region'] + train['province_state']
test['cp'] = test['country_region'] + test['province_state']
train.drop(['province_state','country_region'], axis=1, inplace=True)
 test.drop(['province_state','country_region'], axis =1, inplace=True )<feature_engineering>
train_data['WheelTypeID'].value_counts()
df = pd.DataFrame()
def create_time_feat(data):
df['date']= data['date']
df['hour']=df['date'].dt.hour
df['weekofyear']=df['date'].dt.weekofyear
df['quarter'] =df['date'].dt.quarter
df['month'] = df['date'].dt.month
df['dayofyear']=df['date'].dt.dayofyear
x=df[['hour','weekofyear','quarter','month','dayofyear']]
return x
cr_tr = create_time_feat(train)
 cr_te = create_time_feat(test )<concatenate>
train_data.drop('WheelTypeID',inplace=True,axis=1)
 test_data.drop('WheelTypeID',inplace=True,axis=1 )
train_df = pd.concat([train,cr_tr], axis=1)
 test_df = pd.concat([test, cr_te], axis =1)
 test_df.dropna(inplace=True )<categorify>
train_data['WheelType'].value_counts()
le=LabelEncoder()
train_df['cp_le']=le.fit_transform(train_df['cp'])
 test_df['cp_le']=le.transform(test_df['cp'])
 train_df.drop(['cp'], axis=1, inplace=True)
 test_df.drop(['cp'], axis=1, inplace=True )<count_duplicates>
test_data['WheelType'].value_counts()
def create_date_feat(data, cf, ft):
for d in data['date'].drop_duplicates() :
for i in data['cp_le'].drop_duplicates() :
org_mask =(data['date']==d)&(data['cp_le']==i)
 for lag in range(1,15):
mask_loc =(data['date']==(d-pd.Timedelta(days=lag)))&(data['cp_le']==i)
 try:
data.loc[org_mask, 'cf_' + str(lag)]=data.loc[mask_loc, cf].values
data.loc[org_mask, 'ft_' + str(lag)]=data.loc[mask_loc, ft].values
except:
data.loc[org_mask, 'cf_' + str(lag)]=0.0
data.loc[org_mask, 'ft_' + str(lag)]=0.0
create_date_feat(train_df,'confirmedcases','fatalities' )<compute_test_metric>
train_data['WheelType'].fillna(value='WheelType_unk',inplace=True)
 test_data['WheelType'].fillna(value='WheelType_unk',inplace=True )
def rmsle(pred,true):
assert pred.shape[0]==true.shape[0]
return K.sqrt(K.mean(K.square(K.log(pred+1)- K.log(true+1))))
es = EarlyStopping(monitor='val_loss', min_delta = 0, verbose=0, patience=10, mode='auto')
 mc_cf = ModelCheckpoint('model_cf.h5', monitor='val_loss', verbose=0, save_best_only=True)
 mc_ft = ModelCheckpoint('model_ft.h5', monitor='val_loss', verbose=0, save_best_only=True)
 def lstm_model(hidden_nodes, second_dim, third_dim):
model = Sequential([LSTM(hidden_nodes, input_shape=(second_dim, third_dim), activation='relu'),
Dense(64, activation='relu'),
Dense(32, activation='relu'),
Dense(1, activation='relu')])
 model.compile(loss=rmsle, optimizer = 'adam')
 return model
model_cf = lstm_model(10, tr_x_cf.shape[1], tr_x_cf.shape[2])
 model_ft = lstm_model(10, tr_x_ft.shape[1], tr_x_ft.shape[2])
 history_cf = model_cf.fit(tr_x_cf, tr_y_cf, epochs=200, batch_size=512, validation_data=(val_x_cf,val_y_cf), callbacks=[es,mc_cf])
 history_ft = model_ft.fit(tr_x_ft, tr_y_ft, epochs=200, batch_size=512, validation_data=(val_x_ft,val_y_ft), callbacks=[es,mc_ft] )<define_variables>
train_data['WheelType'].value_counts()
feat = ['confirmedcases','fatalities','cf_1', 'ft_1', 'cf_2', 'ft_2', 'cf_3', 'ft_3',
'cf_4', 'ft_4', 'cf_5', 'ft_5', 'cf_6', 'ft_6', 'cf_7', 'ft_7', 'cf_8', 'ft_8',
'cf_9', 'ft_9', 'cf_10', 'ft_10', 'cf_11', 'ft_11', 'cf_12', 'ft_12', 'cf_13', 'ft_13',
'cf_14', 'ft_14']
c_feat = ['cp_le', 'weekofyear','quarter','month','dayofyear','cf_1', 'cf_2', 'cf_3',
'cf_4', 'cf_5', 'cf_6', 'cf_7', 'cf_8', 'cf_9','cf_10', 'cf_11', 'cf_12',
'cf_13', 'cf_14']
f_feat = ['cp_le', 'weekofyear','quarter','month','dayofyear','ft_1', 'ft_2', 'ft_3',
'ft_4', 'ft_5', 'ft_6', 'ft_7', 'ft_8', 'ft_9','ft_10', 'ft_11', 'ft_12',
'ft_13', 'ft_14']
tot_feat = ['cp_le', 'weekofyear','quarter','month','dayofyear','cf_1', 'ft_1', 'cf_2', 'ft_2', 'cf_3', 'ft_3',
'cf_4', 'ft_4', 'cf_5', 'ft_5', 'cf_6', 'ft_6', 'cf_7', 'ft_7', 'cf_8', 'ft_8',
'cf_9', 'ft_9', 'cf_10', 'ft_10', 'cf_11', 'ft_11', 'cf_12', 'ft_12', 'cf_13', 'ft_13',
'cf_14', 'ft_14']
test_new = test_df.copy().join(pd.DataFrame(columns=feat))
test_mask =(test_df['date'] <= train_df['date'].max())
 train_mask =(train_df['date'] >= test_df['date'].min())
 test_new.loc[test_mask,feat] = train_df.loc[train_mask, feat].values
future_df = pd.date_range(start = train_df['date'].max() +pd.Timedelta(days=1),end=test_df['date'].max() , freq='1D')
 def create_add_trend_pred(data, cf, ft):
for d in future_df:
for i in data['cp_le'].drop_duplicates() :
org_mask =(data['date']==d)&(data['cp_le']==i)
 for lag in range(1,15):
mask_loc =(data['date']==(d-pd.Timedelta(days=lag)))&(data['cp_le']==i)
 try:
data.loc[org_mask, 'cf_' + str(lag)]=data.loc[mask_loc,cf].values
data.loc[org_mask, 'ft_' + str(lag)]=data.loc[mask_loc,ft].values
except:
data.loc[org_mask, 'cf_' + str(lag)]=0.0
data.loc[org_mask, 'ft_' + str(lag)]=0.0
test_x = data.loc[org_mask,tot_feat]
test_x_cf = test_x[c_feat]
test_x_cf = test_x_cf.to_numpy().reshape(1,-1)
 test_x_cf_reshape = test_x_cf.reshape(test_x_cf.shape[0],1,test_x_cf.shape[1])
 test_x_ft = test_x[f_feat]
test_x_ft = test_x_ft.to_numpy().reshape(1,-1)
 test_x_ft_reshape = test_x_ft.reshape(test_x_ft.shape[0],1,test_x_ft.shape[1])
 data.loc[org_mask, cf] = model_cf.predict(test_x_cf_reshape)
 data.loc[org_mask, ft] = model_ft.predict(test_x_ft_reshape)
 create_add_trend_pred(test_new, 'confirmedcases', 'fatalities' )<save_to_csv>
train_data['Nationality'].value_counts()
sub_pred = pd.DataFrame({'ForecastId': test_id, 'ConfirmedCases':test_new['confirmedcases'],'Fatalities':test_new['fatalities']})
 sub_pred.to_csv('submission.csv', index=False )<install_modules>
test_data['Nationality'].value_counts()
!pip install inflect<install_modules>
train_data['Nationality'].fillna(value='Nationality_unk',inplace=True)
 test_data['Nationality'].fillna(value='Nationality_unk',inplace=True )
!pip install contractions<set_options>
train_data['Size'].value_counts()
%matplotlib inline
warnings.filterwarnings('ignore')
 logging.basicConfig(level=logging.INFO )<categorify>
test_data['Size'].value_counts()
def text_preprocessing_platform(df, text_col):
def denoise_text(text):
soup = BeautifulSoup(text, "html.parser")
 text = soup.get_text()
text = contractions.fix(text)
 return text
def remove_non_ascii(words):

 new_words = []
for word in words:
new_word = unicodedata.normalize('NFKD', word ).encode('ascii', 'ignore' ).decode('utf-8', 'ignore')
 new_words.append(new_word)
 return new_words
def to_lowercase(words):

 new_words = []
for word in words:
new_word = word.lower()
new_words.append(new_word)
 return new_words
def remove_punctuation(words):

 new_words = []
for word in words:
new_word = re.sub(r'[^\w\s]', '', word)
 if new_word != '':
new_words.append(new_word)
 return new_words
def replace_numbers(words):

 p = inflect.engine()
new_words = []
for word in words:
if word.isdigit() :
new_word = p.number_to_words(word)
 new_words.append(new_word)
 else:
new_words.append(word)
 return new_words
def remove_stopwords(words):

 new_words = []
for word in words:
if word not in stopwords.words('english'):
new_words.append(word)
 return new_words
def stem_words(words):

 stemmer = LancasterStemmer()
stems = []
for word in words:
stem = stemmer.stem(word)
 stems.append(stem)
 return stems
def lemmatize_verbs(words):

 lemmatizer = WordNetLemmatizer()
lemmas = []
for word in words:
lemma = lemmatizer.lemmatize(word, pos='v')
 lemmas.append(lemma)
 return lemmas
def normalize_text(words):
words = remove_non_ascii(words)
 words = to_lowercase(words)
 words = remove_punctuation(words)
 words = replace_numbers(words)
 words = remove_stopwords(words)
 words = lemmatize_verbs(words)
 return words
def tokenize(text):
return nltk.word_tokenize(text)
 def text_prepare(text):
text = denoise_text(text)
 text = ' '.join([x for x in normalize_text(tokenize(text)) ])
 return text
df[text_col] = [text_prepare(x)for x in df[text_col]]
return df 
<load_from_csv>
train_data['Size'].fillna(value='Size_unk',inplace=True)
 test_data['Size'].fillna(value="Size_unk",inplace=True )
train = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 before = train.head(10)
 print("Before Text Preprocessing")
 display(before[['text']])
 after = text_preprocessing_platform(before, 'text')
 print("After Text Preprocessing")
 display(after[['text']] )<string_transform>
train_data['TopThreeAmericanName'].value_counts()
def loadData_Tokenizer(X_train, X_test,MAX_NB_WORDS=75000,MAX_SEQUENCE_LENGTH=500):
np.random.seed(7)
 text = np.concatenate(( X_train, X_test), axis=0)
 text = np.array(text)
 tokenizer = Tokenizer(num_words=MAX_NB_WORDS)
 tokenizer.fit_on_texts(text)
 sequences = tokenizer.texts_to_sequences(text)
 word_index = tokenizer.word_index
text = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)
 print('Found %s unique tokens.' % len(word_index))
indices = np.arange(text.shape[0])
 text = text[indices]
print(text.shape)
 X_train = text[0:len(X_train), ]
X_test = text[len(X_train):, ]
embeddings_index = {}
f = open("/kaggle/input/glove6b50dtxt/glove.6B.50d.txt", encoding='utf-8')
 for line in f:
try:
values = line.split()
word = values[0]
try:
coefs = np.asarray(values[1:], dtype='float32')
 except:
pass
embeddings_index[word] = coefs
except UnicodeDecodeError:
pass
f.close()
print('Total %s word vectors.' % len(embeddings_index))
return(X_train, X_test, word_index,embeddings_index, tokenizer )<choose_model_class>
test_data['TopThreeAmericanName'].value_counts()
def Build_Model_RNN_Text(word_index, embeddings_index, nclasses, MAX_SEQUENCE_LENGTH=500, EMBEDDING_DIM=50, dropout=0.5):
model = Sequential()
hidden_layer = 2
lstm_node = 32
embedding_matrix = np.random.random(( len(word_index)+ 1, EMBEDDING_DIM))
for word, i in word_index.items() :
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
if len(embedding_matrix[i])!= len(embedding_vector):
print("could not broadcast input array from shape", str(len(embedding_matrix[i])) ,
"into shape", str(len(embedding_vector)) , " Please make sure your"
" EMBEDDING_DIM is equal to embedding_vector file ,GloVe,")
 exit(1)
 embedding_matrix[i] = embedding_vector
model.add(Embedding(len(word_index)+ 1,
EMBEDDING_DIM,
weights=[embedding_matrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=True))
print(lstm_node)
 for i in range(0,hidden_layer):
model.add(Bidirectional(LSTM(lstm_node,return_sequences=True, recurrent_dropout=0.5)))
 model.add(Dropout(dropout))
model.add(Bidirectional(LSTM(lstm_node, recurrent_dropout=0.5)))
 model.add(Dropout(dropout))
model.add(Dense(256, activation='relu'))
model.add(Dense(nclasses, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 return model<compute_train_metric>
train_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True )
def get_eval_report(labels, preds):
mcc = matthews_corrcoef(labels, preds)
 tn, fp, fn, tp = confusion_matrix(labels, preds ).ravel()
precision =(tp)/(tp+fp)
 recall =(tp)/(tp+fn)
 f1 =(2*(precision*recall)) /(precision+recall)
 return {
"mcc": mcc,
"tp": tp,
"tn": tn,
"fp": fp,
"fn": fn,
"pricision" : precision,
"recall" : recall,
"F1" : f1,
"accuracy":(tp+tn)/(tp+tn+fp+fn)
 }
def compute_metrics(labels, preds):
assert len(preds)== len(labels)
 return get_eval_report(labels, preds)
 def plot_graphs(history, string):
plt.plot(history.history[string])
 plt.plot(history.history['val_'+string], '')
 plt.xlabel("Epochs")
 plt.ylabel(string)
 plt.legend([string, 'val_'+string])
 plt.show()
def class_balance(df, target):
cls = df[target].value_counts()
cls.plot(kind='bar')
 plt.show()<define_variables>
train_data['PRIMEUNIT'].value_counts()
preprocess = True
text = 'text'
target = 'target'<load_from_csv>
test_data['PRIMEUNIT'].value_counts()
traindf = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 testdf = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
<prepare_output>
train_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True)
 test_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True )
if preprocess:
traindf = text_preprocessing_platform(traindf, text)
 testdf = text_preprocessing_platform(testdf, text)
 train_final = traindf
test_final = testdf
print("Train DataFrame")
 display(train_final.head(3))
print("Leadeboard DataFrame")
 display(test_final.head(3))<train_model>
train_data['AUCGUART'].replace("AGREEN","GREEN",inplace=True)
 test_data['AUCGUART'].replace("ARED","RED",inplace=True )
df = train_final
test = test_final<split>
train_data['AUCGUART'].fillna(value="AUC_unk",inplace=True)
 test_data['AUCGUART'].fillna(value="AUC_unk",inplace=True )
X = df[text]
y = df[target]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
 print("Generating Glove...")
 X_train_Glove,X_test_Glove, word_index,embeddings_index, tokenizer = loadData_Tokenizer(X_train,X_test )<train_model>
train_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1)
 test_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1 )
with warnings.catch_warnings() :
print("Building Model...")
 model_RNN = Build_Model_RNN_Text(word_index,embeddings_index, 2)
 model_RNN.summary()
print("
Starting Training...
 ")
 history = model_RNN.fit(X_train_Glove, y_train,
validation_data=(X_test_Glove, y_test),
epochs=5,
batch_size=128,
verbose=1)
 warnings.simplefilter("ignore" )<define_variables>
train_data.drop('PurchDate',axis=1,inplace=True)
 test_data.drop("PurchDate",axis=1,inplace=True )
test.head()
ids = test['id']<string_transform>
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object'
test_input = test[text]
test_input = np.array(test_input)
 test_sequences = tokenizer.texts_to_sequences(test_input)
 test_padded = pad_sequences(test_sequences, maxlen=500)
 indices = np.arange(test_padded.shape[0])
 test_padded = test_padded[indices]
unlabbeled_glove = test_padded<predict_on_test>
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']
test_predictions = model_RNN.predict_classes(unlabbeled_glove )<create_dataframe>
for i in not_categorical:
maximum=np.max(train_data[i])
 train_data[i]=train_data[i]/maximum
maximum_test=np.max(test_data[i])
 test_data[i]=test_data[i]/maximum_test
output = pd.DataFrame({'id':ids,'target':test_predictions} )<prepare_output>
categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']
output.set_index('id' )<save_to_csv>
train_data[categorical[0]]
output.to_csv('/kaggle/working/glove6b50d_bi_lstm_p-2LAYERS.csv', index=False )<import_modules>
pd.get_dummies(train_data[categorical[0]] )
import pandas as pd<load_from_csv>
for i in categorical:
dummies=pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data=pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,inplace=True,axis=1)
 dummies=pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data=pd.concat([test_data,dummies],axis=1)
 test_data.drop(i,inplace=True,axis=1 )
train_data = pd.read_csv(".. /input/titanic/train.csv")
 test_data = pd.read_csv(".. /input/titanic/test.csv" )<split>
for i in train_data.drop('IsBadBuy',axis=1 ).columns:
if i not in test_data.columns:
test_data[i]=np.zeros(len(test_data))
y = train_data.Survived
X = train_data.drop(['Survived'], axis=1)
 X_test = test_data
X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=0 )<categorify>
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))
num_cols = [cname for cname in X_train if X_train[cname].dtype in ['int64', 'float64']]
cat_cols = [cname for cname in X_train if X_train[cname].dtype == "object"]
numerical_transformer = SimpleImputer(strategy='constant')
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, num_cols), 
('cat', categorical_transformer, cat_cols)
 ])
 model = SVC(kernel='linear', random_state=0)
<predict_on_test>
test_data=test_data[train_data.drop("IsBadBuy",axis=1 ).columns]
my_pipeline = Pipeline(steps=[('preprocessor', preprocessor),('model', model)])
 my_pipeline.fit(X_train, y_train)
 predictions = my_pipeline.predict(X_valid)
 mean_absolute_error(y_valid, predictions)
 final_submission = my_pipeline.predict(X_test )<save_to_csv>
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': final_submission})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<set_options>
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42 )
get_ipython().magic('matplotlib inline')
 plt.rcParams['figure.figsize'] =(8, 6)
 warnings.filterwarnings(action='ignore')
 warnings.filterwarnings(action='ignore', category=DeprecationWarning)
 warnings.filterwarnings(action='ignore', category=FutureWarning)
 start = time.time()<data_type_conversions>
from sklearn.neighbors import KNeighborsClassifier
print('Version :', platform.python_version())
 print('Compiler :', platform.python_compiler())
 print('Build :', platform.python_build())
 print("
Current date and time using isoformat:")
 print(datetime.datetime.now().isoformat() )<init_hyperparams>
from sklearn.neighbors import KNeighborsClassifier
save = True
n_row = None
debug = False
if debug is True: n_row = 80
n_splits = 2
n_iter = 70
scoring = 'accuracy'
rstate = 27
testset_size = 0.35
num_rounds = 800
n_tree_range = st.randint(100, num_rounds )<load_from_csv>
from sklearn.neighbors import KNeighborsClassifier
train_df = pd.read_csv(".. /input/train.csv", index_col='PassengerId', nrows=n_row)
 test_df = pd.read_csv(".. /input/test.csv", index_col='PassengerId', nrows=n_row)
 Survived = train_df['Survived'].copy()
train_df = train_df.drop('Survived', axis=1 ).copy()
df = pd.concat([test_df, train_df])
 traindex = train_df.index
testdex = test_df.index
del train_df
del test_df
df['FamilySize'] = df['SibSp'] + df['Parch'] + 1
df['Name_length'] = df['Name'].apply(len)
 df['IsAlone'] = 0
df.loc[df['FamilySize'] == 1, 'IsAlone'] = 1
df['Title']=0
df['Title']=df.Name.str.extract('([A-Za-z]+)\.')
 df['Title'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don'],
['Miss','Miss','Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr'],inplace=True)
 df.loc[(df.Age.isnull())&(df.Title=='Mr'),'Age']= df.Age[df.Title=="Mr"].mean()
df.loc[(df.Age.isnull())&(df.Title=='Mrs'),'Age']= df.Age[df.Title=="Mrs"].mean()
df.loc[(df.Age.isnull())&(df.Title=='Master'),'Age']= df.Age[df.Title=="Master"].mean()
df.loc[(df.Age.isnull())&(df.Title=='Miss'),'Age']= df.Age[df.Title=="Miss"].mean()
df.loc[(df.Age.isnull())&(df.Title=='Other'),'Age']= df.Age[df.Title=="Other"].mean()
df = df.drop('Name', axis=1)
 df['Embarked'] = df['Embarked'].fillna(df['Embarked'].mode().iloc[0])
 df['Fare'] = df['Fare'].fillna(df['Fare'].mean())
 df['Sex'] = df['Sex'].map({'female': 1,
'male': 0} ).astype(int)
 df['Title'] = df['Title'].map({'Mr': 0, 'Mrs': 1, 'Miss': 2, 'Master':3, 'Other':4})
 df['Title'] = df['Title'].fillna(df['Title'].mode().iloc[0])
 df['Title'] = df['Title'].astype(int)
 df['Embarked'] = df['Embarked'].map({'Q': 0, 'S': 1, 'C': 2} ).astype(int)
 df= df.drop(['Ticket', 'Cabin'], axis=1)
 categorical_features = ["Pclass","Sex","IsAlone","Title", "Embarked"]<normalization>
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )
continuous_features = ['Fare','Age','Name_length']
for col in continuous_features:
transf = df[col].values.reshape(-1,1)
 scaler = preprocessing.StandardScaler().fit(transf)
 df[col] = scaler.transform(transf)
 train_df = df.loc[traindex, :]
train_df['Survived'] = Survived
test_df = df.loc[testdex, :]
del df<prepare_x_and_y>
KNN.score(X_test,y_test )
X = train_df.drop(["Survived"] , axis=1)
 y = train_df["Survived"]
print("X, Y, Test Shape:",X.shape, y.shape, test_df.shape)
 results = pd.DataFrame(columns=['Model','Para','Test_Score','CV Mean','CV STDEV'])
 ensemble_models= {}<count_values>
predict=KNN.predict(test_data.drop('RefId',axis=1))
print("Depedent Variable Distribution")
 print(y.value_counts(normalize=True)*100)
 print("0 = Died", "
1 = Survived" )<count_values>
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head()
imbalance_weight = y.value_counts(normalize=True)[0]/y.value_counts(normalize=True)[1]
print("Imbalance Weight: ",imbalance_weight )<train_model>
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True )
levels = [2,4,6,8,10,12]
for x in levels:
pca = PCA(n_components=x)
 fit = pca.fit(train_df)
 print(( "{} Components
Explained Variance: {}
" ).format(x, fit.explained_variance_ratio_)) 
<split>
Submission.head()
Submission.to_csv('Submission.csv' )