Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
dtcla = DecisionTreeClassifier(random_state=1000)
 dtcla.fit(train[features1],train[target1] )<predict_on_test>
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 train = train_data.copy()
test = test_data.copy()
predictions1 = dtcla.predict(test[features1])
 print(predictions1[0:50] )<prepare_output>
train.drop(['PassengerId'],axis=1,inplace=True)
 test.drop(['PassengerId'],axis=1,inplace=True)
 pred = train_data['Survived']
submission = pd.DataFrame({'ForecastId':test['ForecastId'],'ConfirmedCases':predictions,'Fatalities':predictions1})
 submission.head(10 )<save_to_csv>
train.isnull().sum()
filename = 'submission.csv'
submission.to_csv(filename,index=False)
 print('Saved file: ' + filename )<import_modules>
train.isnull().sum()
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import nltk
from sklearn.preprocessing import LabelBinarizer,LabelEncoder,StandardScaler,MinMaxScaler
from sklearn.linear_model import LogisticRegression,SGDClassifier,LinearRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
from sklearn.model_selection import train_test_split
import keras
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from keras.models import Sequential
from keras.layers import Dense,LSTM
import tensorflow as tf<load_from_csv>
test.isnull().sum()
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )<count_missing_values>
test.isnull().sum()
train_df.isna().sum()<count_missing_values>
sex1 = pd.get_dummies(train['Sex'])
 sex2 = pd.get_dummies(test['Sex'])
 train.drop(['Sex'],axis=1,inplace=True)
 test.drop(['Sex'],axis=1,inplace=True)
 train = pd.concat([train,sex1],axis=1)
 test = pd.concat([test,sex2],axis=1 )
test_df.isna().sum()<drop_column>
train.drop(['female'],axis=1,inplace=True)
 test.drop(['female'],axis=1,inplace=True )
train_df['Country_Region'] = train_df['Country_Region'] + ' ' + train_df['Province_State']
test_df['Country_Region'] = test_df['Country_Region'] + ' ' + test_df['Province_State']
del train_df['Province_State']
del test_df['Province_State']<feature_engineering>
plt.figure(figsize=(8, 5))
sns.set_style('whitegrid')
 sns.countplot(x = 'Pclass',hue='Survived',data=train_data,palette='deep' )
def split_date(date):
date = date.split('-')
 date[0] = int(date[0])
 if(date[1][0] == '0'):
date[1] = int(date[1][1])
 else:
date[1] = int(date[1])
 if(date[2][0] == '0'):
date[2] = int(date[2][1])
 else:
date[2] = int(date[2])
 return date
train_df.Date = train_df.Date.apply(split_date)
 test_df.Date = test_df.Date.apply(split_date )<feature_engineering>
Fare_0 = []
Fare_1 = []
for i in range(0,891):
if train_data["Survived"][i] == 0:
Fare_0.append(train["Fare"][i])
 else:
Fare_1.append(train["Fare"][i] )
year = []
month = []
day = []
for i in train_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )<feature_engineering>
train["Embarked"].fillna("S", inplace = True)
 test["Embarked"].fillna("S", inplace = True )
train_df['Year'] = year
train_df['Month'] = month
train_df['Day'] = day
del train_df['Date']<feature_engineering>
embark1 = pd.get_dummies(train['Embarked'])
 embark2 = pd.get_dummies(test['Embarked'])
 train.drop(['Embarked'],axis=1,inplace=True)
 test.drop(['Embarked'],axis=1,inplace=True)
 train = pd.concat([train,embark1],axis=1)
 test = pd.concat([test,embark2],axis=1 )
year = []
month = []
day = []
for i in test_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )<feature_engineering>
def fam(x):
if(x['SibSp'] + x['Parch'])> 0:
return 1
else:
return 0
train['Family'] = train.apply(fam, axis = 1)
 test['Family'] = test.apply(fam, axis = 1 )
test_df['Year'] = year
test_df['Month'] = month
test_df['Day'] = day
del test_df['Date']
del train_df['Id']
del test_df['ForecastId']<drop_column>
train = train.drop(['SibSp','Parch'],axis=1)
 test = test.drop(['SibSp','Parch'],axis=1 )
del train_df['Year']
del test_df['Year']<data_type_conversions>
train["Cabin"] = pd.Series([i[0] if not pd.isnull(i)else 'X' for i in train['Cabin'] ])
 test["Cabin"] = pd.Series([i[0] if not pd.isnull(i)else 'X' for i in test['Cabin'] ] )
train_df['ConfirmedCases'] = train_df['ConfirmedCases'].apply(int)
 train_df['Fatalities'] = train_df['Fatalities'].apply(int )<drop_column>
train["Cabin"] = train["Cabin"].map({"X":0, "A":1, "B" : 2 , "C":3, "D":4, "E":5, "F":6, "G":7,"T":0})
 train["Cabin"] = train["Cabin"].astype(int)
 test["Cabin"] = test["Cabin"].map({"X":0, "A":1, "B" : 2 , "C":3, "D":4, "E":5, "F":6, "G":7,"T":0})
 test["Cabin"] = test["Cabin"].astype(int )
cases = train_df.ConfirmedCases
fatalities = train_df.Fatalities
del train_df['ConfirmedCases']
del train_df['Fatalities']<categorify>
train_title = [i.split(",")[1].split(".")[0].strip() for i in train["Name"]]
train["Title"] = pd.Series(train_title)
 test_title = [i.split(",")[1].split(".")[0].strip() for i in test["Name"]]
test["Title"] = pd.Series(test_title )
lb = LabelEncoder()
train_df['Country_Region'] = lb.fit_transform(train_df['Country_Region'])
 test_df['Country_Region'] = lb.transform(test_df['Country_Region'] )<normalization>
train = train.drop(['Name'], axis = 1)
 test = test.drop(['Name'], axis = 1 )
scaler = MinMaxScaler()
x_train = scaler.fit_transform(train_df.values)
 x_test = scaler.transform(test_df.values )<import_modules>
train["Title"] = train["Title"].replace(['Lady', 'the Countess','Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 train["Title"] = train["Title"].map({"Master":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Mr":2, "Rare":3})
 train["Title"] = train["Title"].astype(int)
 test["Title"] = test["Title"].replace(['Lady', 'the Countess','Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 test["Title"] = test["Title"].map({"Master":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Mr":2, "Rare":3})
 test["Title"] = test["Title"].astype(int )
from xgboost import XGBRegressor<train_model>
Ticket1 = []
for i in list(train.Ticket):
if not i.isdigit() :
Ticket1.append(i.replace(".","" ).replace("/","" ).strip().split(' ')[0])
 else:
Ticket1.append("X")
 train["Ticket"] = Ticket1
Ticket2 = []
for j in list(test.Ticket):
if not j.isdigit() :
Ticket2.append(j.replace(".","" ).replace("/","" ).strip().split(' ')[0])
 else:
Ticket2.append("X")
 test["Ticket"] = Ticket2
rf = XGBRegressor(n_estimators = 2500 , random_state = 0 , max_depth = 27)
 rf.fit(x_train,cases )<predict_on_test>
np.union1d(train["Ticket"], test["Ticket"] )
cases_pred = rf.predict(x_test)
 cases_pred<feature_engineering>
train= pd.get_dummies(train, columns = ["Ticket"], prefix="T")
 test = pd.get_dummies(test, columns = ["Ticket"], prefix="T" )
cases_pred = np.around(cases_pred,decimals = 0)
 cases_pred<concatenate>
train = train.drop(['T_SP','T_SOP','T_Fa','T_LINE','T_SWPP','T_SCOW','T_PPP','T_AS','T_CASOTON'],axis = 1)
 test = test.drop(['T_SCA3','T_STONOQ','T_AQ4','T_A','T_LP','T_AQ3'],axis = 1 )
x_train_cas = []
for i in range(len(x_train)) :
x = list(x_train[i])
 x.append(cases[i])
 x_train_cas.append(x)
 x_train_cas[0]<prepare_x_and_y>
train.drop(['Survived'],axis=1,inplace=True )
x_train_cas = np.array(x_train_cas )<train_model>
print(train.isnull().sum())
 print("Number of columns are :",train.isnull().sum().count() )
rf = XGBRegressor(n_estimators = 2500 , random_state = 0 , max_depth = 27)
 rf.fit(x_train_cas,fatalities )<concatenate>
print(test.isnull().sum())
 print("Number of columns are :",test.isnull().sum().count() )
x_test_cas = []
for i in range(len(x_test)) :
x = list(x_test[i])
 x.append(cases_pred[i])
 x_test_cas.append(x)
 x_test_cas[0]<predict_on_test>
sc = StandardScaler()
train2 = sc.fit_transform(train)
 test2 = sc.transform(test )
fatalities_pred = rf.predict(x_test_cas)
 fatalities_pred<feature_engineering>
KFold_Score = pd.DataFrame()
classifiers = ['Linear SVM', 'Radial SVM', 'LogisticRegression',
'RandomForestClassifier', 'AdaBoostClassifier',
'XGBoostClassifier', 'KNeighborsClassifier','GradientBoostingClassifier']
models = [svm.SVC(kernel='linear'),
svm.SVC(kernel='rbf'),
LogisticRegression(max_iter = 1000),
RandomForestClassifier(n_estimators=200, random_state=0),
AdaBoostClassifier(random_state = 0),
xgb.XGBClassifier(n_estimators=100),
KNeighborsClassifier() ,
GradientBoostingClassifier(random_state=0)
 ]
j = 0
for i in models:
model = i
cv = KFold(n_splits=5, random_state=0, shuffle=True)
 KFold_Score[classifiers[j]] =(cross_val_score(model, train, np.ravel(pred), scoring = 'accuracy', cv=cv))
j = j+1
fatalities_pred = np.around(fatalities_pred,decimals = 0)
 fatalities_pred<prepare_output>
mean = pd.DataFrame(KFold_Score.mean() , index= classifiers)
 KFold_Score = pd.concat([KFold_Score,mean.T])
 KFold_Score.index=['Fold 1','Fold 2','Fold 3','Fold 4','Fold 5','Mean']
KFold_Score.T.sort_values(by=['Mean'], ascending = False )
submission['ConfirmedCases'] = cases_pred
submission['Fatalities'] = fatalities_pred<save_to_csv>
col_name1[0],col_name1[2] = col_name1[2],col_name1[0]
col_name2[0],col_name2[2] = col_name2[2],col_name2[0]
submission.to_csv("submission.csv" , index = False )<load_from_csv>
train_new = train[col_name1]
test_new = test[col_name2]
pd.set_option('mode.chained_assignment', None)
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 train = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 train['Province_State'].fillna('', inplace=True)
 test['Province_State'].fillna('', inplace=True)
 train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'])
 train = train.sort_values(['Country_Region','Province_State','Date'])
 test = test.sort_values(['Country_Region','Province_State','Date'])
 train[['ConfirmedCases', 'Fatalities']] = train.groupby(['Country_Region', 'Province_State'])[['ConfirmedCases', 'Fatalities']].transform('cummax' )<feature_engineering>
train_new = train_new.drop(['Cabin'],axis = 1)
 test_new = test_new.drop(['Cabin'],axis = 1 )
def RMSLE(pred,actual):
return np.sqrt(np.mean(np.power(( np.log(pred+1)-np.log(actual+1)) ,2)))
 feature_day = [1,20,50,100,200,500,1000,5000,10000,15000,20000,50000,100000,200000]
def CreateInput(data):
feature = []
for day in feature_day:
data.loc[:,'Number day from ' + str(day)+ ' case'] = 0
if(train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].count() > 0):
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].max()
else:
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]['Date'].min()
for i in range(0, len(data)) :
if(data['Date'].iloc[i] > fromday):
day_denta = data['Date'].iloc[i] - fromday
data['Number day from ' + str(day)+ ' case'].iloc[i] = day_denta.days
feature = feature + ['Number day from ' + str(day)+ ' case']
return data[feature] 
<install_modules>
sc = StandardScaler()
train3 = sc.fit_transform(train_new)
 test3 = sc.transform(test_new )
!pip install pmdarima<save_to_csv>
rfc = RandomForestClassifier(random_state=0 )
df_val = df_val_2
submission = df_val[['ForecastId','ConfirmedCases_hat','Fatalities_hat']]
submission.columns = ['ForecastId','ConfirmedCases','Fatalities']
submission = submission.round({'ConfirmedCases': 0, 'Fatalities': 0})
 submission.to_csv('submission.csv', index=False)
 submission<import_modules>
param_grid = {
'n_estimators': [ 200,300],
'max_features': ['auto', 'sqrt'],
'max_depth' : [6,7,8],
'criterion' :['gini', 'entropy']
}
tqdm.pandas()<load_from_disk>
CV_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 5)
 CV_rfc.fit(train3,pred)
 CV_rfc.best_params_
train = pd.read_json('.. /input/train.json')
 test = pd.read_json('.. /input/test.json' )<feature_engineering>
rfc1=RandomForestClassifier(random_state=0, n_estimators= 200, criterion = 'gini',max_features = 'auto',max_depth = 8)
 rfc1.fit(train3, pred )
train['num_ingredients'] = train['ingredients'].apply(len)
 train = train[train['num_ingredients'] > 1]<string_transform>
pred3= rfc1.predict(test3)
 print(pred3 )
lemmatizer = WordNetLemmatizer()
def preprocess(ingredients):
ingredients_text = ' '.join(ingredients)
 ingredients_text = ingredients_text.lower()
ingredients_text = ingredients_text.replace('-', ' ')
 words = []
for word in ingredients_text.split() :
if re.findall('[0-9]', word): continue
if len(word)<= 2: continue
if '’' in word: continue
word = lemmatizer.lemmatize(word)
 if len(word)> 0: words.append(word)
 return ' '.join(words)
 for ingredient, expected in [ 
('Eggs', 'egg'), 
('all-purpose flour', 'all purpose flour'), 
('purée', 'purée'), 
('1% low-fat milk', 'low fat milk'), 
('half & half', 'half half'), 
('safetida(powder)', 'safetida(powder)')
 ]:
actual = preprocess([ingredient])
 assert actual == expected, f'"{expected}" is excpected but got "{actual}"'<feature_engineering>
pred_test = pred3
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': pred_test})
 output.to_csv('./submission.csv', index=False )
train['x'] = train['ingredients'].progress_apply(preprocess)
 test['x'] = test['ingredients'].progress_apply(preprocess)
 train.head()<feature_engineering>
!pip install fugue-incubator==0.0.6
vectorizer = make_pipeline(
 TfidfVectorizer(sublinear_tf=True),
FunctionTransformer(lambda x: x.astype('float16'), validate=False)
)
 x_train = vectorizer.fit_transform(train['x'].values)
 x_train.sort_indices()
x_test = vectorizer.transform(test['x'].values )<categorify>
train_set = pd.DataFrame(train3, columns=train_new.columns ).assign(label=pred)
 test_set = pd.DataFrame(test3, columns=test_new.columns )
label_encoder = LabelEncoder()
y_train = label_encoder.fit_transform(train['cuisine'].values)
 dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)) )<choose_model_class>
def get_space() :
space = ss(LogisticRegression, max_iter=1000)
 return space
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp", 
)
estimator = SVC(
 C=50,
kernel='rbf',
gamma=1.4,
coef0=1,
cache_size=500, 
)
 classifier = OneVsRestClassifier(estimator, n_jobs=-1 )<compute_train_metric>
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=10, random_state=0)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp", 
)
%%time
scores = cross_validate(classifier, x_train, y_train, cv=3)
 scores['test_score'].mean()<train_model>
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=Grid(10,20), random_state=0)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp", 
)
%%time
classifier.fit(x_train, y_train )<categorify>
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=Grid(10,20), learning_rate=Rand(0.01,0.99), random_state=0)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
objective_runner = HyperoptRunner(max_iter=20, seed=0)
)
y_pred = label_encoder.inverse_transform(classifier.predict(x_train))
y_true = label_encoder.inverse_transform(y_train)
 print(f'accuracy score on train data: {accuracy_score(y_true, y_pred)}')
 def report2dict(cr):
rows = []
for row in cr.split("
"):
parsed_row = [x for x in row.split(" ")if len(x)> 0]
if len(parsed_row)> 0: rows.append(parsed_row)
 measures = rows[0]
classes = defaultdict(dict)
 for row in rows[1:]:
class_label = row[0]
for j, m in enumerate(measures):
classes[class_label][m.strip() ] = float(row[j + 1].strip())
 return classes
report = classification_report(y_true, y_pred)
 pd.DataFrame(report2dict(report)).T<save_to_csv>
%%time
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=Grid(10,20), max_depth=Grid(5,10), learning_rate=Rand(0.01,0.99), random_state=0)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
objective_runner = HyperoptRunner(max_iter=20, seed=0)
)
y_pred = label_encoder.inverse_transform(classifier.predict(x_test))
test['cuisine'] = y_pred
test[['id', 'cuisine']].to_csv('submission.csv', index=False)
 test[['id', 'cuisine']].head()<data_type_conversions>
%%time
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=Grid(10,20), max_depth=Grid(5,10), learning_rate=Rand(0.01,0.99), random_state=0, n_jobs=1)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
objective_runner = HyperoptRunner(max_iter=20, seed=0),
execution_engine = DaskExecutionEngine
)
train_df = pd.read_json(".. /input/train.json" ).set_index("id")
 train_df.cuisine = train_df.cuisine.astype("category")
 train_df.head()<string_transform>
def get_space() :
space = ss(LogisticRegression, max_iter=1000)
 return space
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
save_model=True,
)
cuisines = train_df.cuisine.cat.categories.values.tolist()
texts = []
labels = []
label2index = { cuisine: i for i, cuisine in enumerate(cuisines)}
for i, row in train_df.iterrows() :
texts.append("
".join(row.ingredients))
labels.append(label2index[row.cuisine])
 labels = to_categorical(np.asarray(labels, dtype=np.int32))
word_count = Counter()
def lemmatize(texts):
global word_count
wnl = nltk.WordNetLemmatizer()
for text in texts:
tokens_recipe = []
for sentence in text.split("
"):
tokens_ingredient = [ wnl.lemmatize(w)for w in wordpunct_tokenize(sentence.lower())if w.isalpha() ]
word_count.update(tokens_ingredient)
 tokens_recipe.append(" ".join(tokens_ingredient))
yield " ".join(tokens_recipe)
 def preprocess(texts):
processed_texts = list(lemmatize(texts))
black_list = [ word for word, count in word_count.items() if count < 5 ]
return [[ word for word in sentence.split() if word not in black_list] for sentence in processed_texts ]
tokenizer = Tokenizer(oov_token="<UNK>")
 processed_texts = preprocess(texts)
 tokenizer.fit_on_texts(processed_texts)
 feature_matrix = tokenizer.texts_to_matrix(processed_texts)
 word2index = tokenizer.word_index
print("Unique tokens: {}".format(len(word2index)))
 feature_matrix.shape<choose_model_class>
def get_space() :
space1 = ss(LogisticRegression, max_iter=1000)
 space2 = ss(XGBClassifier, n_estimators=Grid(10,20), max_depth=Grid(5,10), learning_rate=Rand(0.01,0.99), random_state=0, n_jobs=1)
 return space1 + space2
suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
objective_runner = HyperoptRunner(max_iter=20, seed=0),
visualize_top_n = 3
)
def build_model(hidden_units, dropout):
model = Sequential()
model.add(Dense(hidden_units, input_shape=[1722,], activation="relu", name="hidden"))
model.add(Dropout(dropout, name="dropout"))
model.add(Dense(20, name="output"))
model.compile("adam", "categorical_hinge", metrics=["accuracy"])
 return model<split>
def get_space() :
return ss(XGBClassifier, n_estimators=RandInt(5,100), max_depth=RandInt(3,10), learning_rate=Rand(0.01,0.99), random_state=0)
 suggest_sk_model(
 get_space() ,
train_set,
scoring="accuracy",
serialize_path = "/tmp",
partition_keys = ["male"],
objective_runner = HyperoptRunner(20,0)
 )
X_train, X_val, y_train, y_val = train_test_split(feature_matrix, labels, test_size=0.2, random_state=42 )<string_transform>
def get_space() :
return sum([
ss(LogisticRegression, max_iter=1000),
ss(RandomForestClassifier,
n_estimators=RandInt(10,300),
max_depth=Grid(*list(range(3, 20))),
random_state=0,
max_features=Grid("auto", "sqrt", "log2"),
criterion=Grid("gini","entropy")) ,
ss(GradientBoostingClassifier,
n_estimators=RandInt(10,300),
max_depth=Grid(*list(range(3, 20))),
random_state=0,
max_features=Grid("auto", "sqrt", "log2"),
learning_rate=Rand(0.01,0.99)) ,
ss(XGBClassifier,
n_estimators=RandInt(10,300),
max_depth=Grid(*list(range(3, 20))),
random_state=0,
learning_rate=Rand(0.01,0.99),
booster=Grid("gbtree", "gblinear", "dart")) ,
])
 print(len(list(get_space())) )
test_df = pd.read_json(".. /input/test.json")
 test_texts = []
for i, row in test_df.iterrows() :
test_texts.append("
".join(row.ingredients))
processed_texts = preprocess(test_texts)
 test_feature_matrix = tokenizer.texts_to_matrix(processed_texts)
 test_feature_matrix.shape<split>
def get_space() :
tree_space = sum([
ss(RandomForestClassifier,
max_features=Grid("auto", "sqrt", "log2"),
criterion=Grid("gini","entropy")) ,
ss(GradientBoostingClassifier,
max_features=Grid("auto", "sqrt", "log2"),
learning_rate=Rand(0.01,0.99)) ,
ss(XGBClassifier,
learning_rate=Rand(0.01,0.99),
booster=Grid("gbtree", "gblinear", "dart")) ,
])
 common = Space(n_estimators=RandInt(10,300),
max_depth=Grid(*list(range(3, 20))),
random_state=0)
 return tree_space * common + ss(LogisticRegression, max_iter=1000)
 print(len(list(get_space())) )
<create_dataframe><EOS> 
model = RandomForestClassifier(random_state=0, n_estimators= 246, criterion = 'entropy',max_features = 'auto',max_depth = 13)
 model.fit(train_new, pred)
 pred_test = model.predict(test_new)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': pred_test})
 output.to_csv('./submission.csv', index=False )