Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<import_modules>
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv' )
from sklearn.preprocessing import LabelEncoder<categorify>
def refineDataSets(data, isTest):
data = data.drop(['PassengerId', 'Ticket', 'Cabin', 'Fare'], axis=1)
 for i in range(0, data.shape[0]):
if('Mr.' in data['Name'][i]):
data['Name'][i] = 0
elif('Misc.' in data['Name'][i]):
data['Name'][i] = 1
elif('Master.' in data['Name'][i]):
data['Name'][i] = 2
elif('Miss.' in data['Name'][i]):
data['Name'][i] = 3
elif('Mrs.' in data['Name'][i]):
data['Name'][i] = 4
else:
data['Name'][i] = 5
if(data['Sex'][i] == 'female'):
data['Sex'][i] = 1
else:
data['Sex'][i] = 0
if(data['Embarked'][i] == 'S'):
data['Embarked'][i] = 0
elif(data['Embarked'][i] == 'C'):
data['Embarked'][i] = 1
else:
data['Embarked'][i] = 2
if(math.isnan(data['Age'][i])) :
data['Age'][i] = data['Age'].mean()
if(math.isnan(data['Pclass'][i])) :
data['Pclass'][i] = data['Pclass'].mean()
if(math.isnan(data['SibSp'][i])) :
data['SibSp'][i] = data['SibSp'].mean()
data['Embarked'][0] = 0
x_data = data.values
x_data = np.float32(np.transpose(x_data))
if isTest:
return x_data
p_data = np.array([data['Survived'].values])
 p_data = np.float32(np.transpose(p_data))
return x_data, p_data
le = LabelEncoder()<categorify>
x_train_data, p_train_data = refineDataSets(train_data, False)
 x_test_data = refineDataSets(test_data, True )
cat_cols = [f for f in train.columns if(train[f].dtype == 'object')]
for col in tqdm(cat_cols):
lbl = LabelEncoder()
lbl.fit(list(train[col].values.astype('str')) + list(test[col].values.astype('str')))
 train[col] = lbl.transform(list(train[col].values.astype('str')))
 test[col] = lbl.transform(list(test[col].values.astype('str')) )<prepare_x_and_y>
x_train_data = np.delete(x_train_data, 0, 0 )
x = train.drop('SALES_PRICE',axis=1)
 y=train.SALES_PRICE<train_model>
x_train_data = np.transpose(x_train_data)
 x_test_data = np.transpose(x_test_data )
reg = LinearRegression()
x.fillna(0,inplace=True)
 reg.fit(x,y)
<save_to_csv>
scaler = MinMaxScaler(feature_range=(-1, 1)).fit(np.concatenate(( x_train_data, x_test_data), axis=0))
x_train_data = scaler.transform(x_train_data)
 x_test_data = scaler.transform(x_test_data )
price = reg.predict(test.fillna(0))
sub = pd.DataFrame(ids)
 sub['SALES_PRICE'] = price
sub.to_csv('sample1.csv',index=None )<save_to_csv>
epoch_train = 1000
mlp = MLP(layer_size=[x_train_data.shape[1], 28, 28, 1], regularization=1, output_shrink=0.1, output_range=[0, 1], loss_type="hardmse")
 mlp.train(x_train_data, p_train_data, iteration_log=20000, rate_init=0.08, rate_decay=0.8, epoch_train=epoch_train, epoch_decay=1 )
def create_download_link(df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv()
b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html)
 df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD'))
create_download_link(sub )<load_from_csv>
predictions = np.round(mlp.predict(x_test_data ).reshape(-1))
data_dir = "/kaggle/input/ykc-2nd/"
train = pd.read_csv(data_dir+"train.csv", index_col=0)
 test = pd.read_csv(data_dir+"test.csv", index_col=0)
 sub = pd.read_csv(data_dir+"sample_submission.csv", index_col=0)
 df = pd.concat([train, test])
 df["product_name"] = df["product_name"].map(lambda w: list(
 filter(None, list(filter(None,
re.sub("'", "",
re.sub(r"-|/|\\|\|"|\:|\;|\@|\^", " ",
re.sub(r"[0-9]+|\+|\&|\?|!|%|\.|,|\)|\(|'s|®|™|
"", w.lower().replace("\xa0", " ")))).split(" "))))))
df_train = df[~df.department_id.isna() ]
df_test = df[df.department_id.isna() ]<define_variables>
predictions = np.round(mlp.predict(x_test_data ).reshape(-1))
ids = list(test_data['PassengerId'].values)
 submission = pd.DataFrame(list(np.transpose([ids, predictions])))
 submission.columns = ['PassengerId', 'Survived']
n_department = 21
n_split = 5<feature_engineering>
submission['Survived'] = np.int32(np.round(submission['Survived']))
submission['PassengerId'] = np.int32(np.round(submission['PassengerId']))
<compute_test_metric><EOS> 
submission.to_csv('titanic-submission-ultimate-nn-1.csv', index=False )