Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
warnings.filterwarnings('ignore' )
y_pred_test[y_pred_test>1] = 1
y_pred_test[y_pred_test<0] = 0<save_to_csv>
def open_data(file):
data = pd.read_csv(".. /input/"+file)
 data = data.drop(["Name", "Ticket", "Cabin"], 1)
 le = preprocessing.LabelEncoder()
data["Sex"] = le.fit_transform(list(data["Sex"]))
data["Embarked"] = le.fit_transform(list(data["Embarked"]))
data["Age"] = data["Age"].fillna(value = data.Age.mean())
 data["Fare"] = data["Fare"].fillna(value = data.Fare.mean())
 return data
df_test['winPlacePerc'] = y_pred_test
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission_lr.csv', index=False )<train_model>
def param_label(data):
data = data.drop(["PassengerId"], 1)
 return data.drop(["Survived"], 1), data[["Survived"]]
GBR = GradientBoostingRegressor(loss='ls',learning_rate=0.1,
n_estimators=100,max_depth=3)
 GBR.fit(X_train,y_train )<compute_test_metric>
def subset_data(X, Y, n):
return model_selection.train_test_split(X, Y, test_size = n )
GBR.score(X_train,y_train )<predict_on_test>
data = open_data("train.csv")
 y_true = data[["Survived"]]
y_test = np.array([1 for i in range(len(y_true)) ])
 print("Accuracy for survived = 1: ",metrics.accuracy_score(y_true, y_test))
y_pred_train = GBR.predict(X_train)
 y_pred_test = GBR.predict(X_test )<save_to_csv>
data = open_data("train.csv")
 y_true = data[["Survived"]]
y_test = np.array([random.choice(( 0, 1)) for i in range(len(y_true)) ])
 print("Accuracy for random survival: ",metrics.accuracy_score(y_true, y_test))
df_test['winPlacePerc'] = y_pred_test
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission_gbr.csv', index=False )<load_from_csv>
data = open_data("train.csv")
 y_true = data[["Survived"]]
y_test = np.array([0 for i in range(len(y_true)) ])
 print("Accuracy for survived = 1: ",metrics.accuracy_score(y_true, y_test))
__author__ = 'ZFTurbo: https://kaggle.com/zfturbo'
def run_solution() :
print('Preparing arrays...')
 f = open(".. /input/train.csv", "r")
 f.readline()
best_hotels_od_ulc = defaultdict(lambda: defaultdict(int))
best_hotels_search_dest = defaultdict(lambda: defaultdict(int))
best_hotels_search_dest1 = defaultdict(lambda: defaultdict(int))
best_hotel_country = defaultdict(lambda: defaultdict(int))
popular_hotel_cluster = defaultdict(int)
 total = 0
while 1:
line = f.readline().strip()
total += 1
if total % 10000000 == 0:
print('Read {} lines...'.format(total))
if line == '':
break
arr = line.split(",")
 book_year = int(arr[0][:4])
 user_location_city = arr[5]
orig_destination_distance = arr[6]
srch_destination_id = arr[16]
is_booking = int(arr[18])
 hotel_country = arr[21]
hotel_market = arr[22]
hotel_cluster = arr[23]
append_1 = 3 + 17*is_booking
append_2 = 1 + 5*is_booking
if user_location_city != '' and orig_destination_distance != '':
best_hotels_od_ulc[(user_location_city, orig_destination_distance)][hotel_cluster] += 1
if srch_destination_id != '' and hotel_country != '' and hotel_market != '' and book_year == 2014:
best_hotels_search_dest[(srch_destination_id, hotel_country, hotel_market)][hotel_cluster] += append_1
if srch_destination_id != '':
best_hotels_search_dest1[srch_destination_id][hotel_cluster] += append_1
if hotel_country != '':
best_hotel_country[hotel_country][hotel_cluster] += append_2
popular_hotel_cluster[hotel_cluster] += 1
f.close()
print('Generate submission...')
 now = datetime.datetime.now()
path = 'submission_' + str(now.strftime("%Y-%m-%d-%H-%M")) + '.csv'
out = open(path, "w")
 f = open(".. /input/test.csv", "r")
 f.readline()
total = 0
out.write("id,hotel_cluster
")
 topclasters = nlargest(5, sorted(popular_hotel_cluster.items()), key=itemgetter(1))
while 1:
line = f.readline().strip()
total += 1
if total % 1000000 == 0:
print('Write {} lines...'.format(total))
if line == '':
break
arr = line.split(",")
 id = arr[0]
user_location_city = arr[6]
orig_destination_distance = arr[7]
srch_destination_id = arr[17]
hotel_country = arr[20]
hotel_market = arr[21]
out.write(str(id)+ ',')
 filled = []
s1 =(user_location_city, orig_destination_distance)
 if s1 in best_hotels_od_ulc:
d = best_hotels_od_ulc[s1]
topitems = nlargest(5, sorted(d.items()), key=itemgetter(1))
for i in range(len(topitems)) :
if topitems[i][0] in filled:
continue
if len(filled)== 5:
break
out.write(' ' + topitems[i][0])
 filled.append(topitems[i][0])
 s2 =(srch_destination_id, hotel_country, hotel_market)
 if s2 in best_hotels_search_dest:
d = best_hotels_search_dest[s2]
topitems = nlargest(5, d.items() , key=itemgetter(1))
for i in range(len(topitems)) :
if topitems[i][0] in filled:
continue
if len(filled)== 5:
break
out.write(' ' + topitems[i][0])
 filled.append(topitems[i][0])
 elif srch_destination_id in best_hotels_search_dest1:
d = best_hotels_search_dest1[srch_destination_id]
topitems = nlargest(5, d.items() , key=itemgetter(1))
for i in range(len(topitems)) :
if topitems[i][0] in filled:
continue
if len(filled)== 5:
break
out.write(' ' + topitems[i][0])
 filled.append(topitems[i][0])
 if hotel_country in best_hotel_country:
d = best_hotel_country[hotel_country]
topitems = nlargest(5, d.items() , key=itemgetter(1))
for i in range(len(topitems)) :
if topitems[i][0] in filled:
continue
if len(filled)== 5:
break
out.write(' ' + topitems[i][0])
 filled.append(topitems[i][0])
 for i in range(len(topclasters)) :
if topclasters[i][0] in filled:
continue
if len(filled)== 5:
break
out.write(' ' + topclasters[i][0])
 filled.append(topclasters[i][0])
 out.write("
")
 out.close()
print('Completed!')
 run_solution()<set_options>
data_test = open_data("test.csv")
 solution = pd.DataFrame(np.array([[data_test.PassengerId.iloc[i], 0] for i in range(len(data_test)) ]),
columns=['PassengerId', 'Survived'])
 solution.to_csv("solution_naive.csv", index=False )
pd.options.display.max_columns = 50 
<define_variables>
from sklearn.neighbors import KNeighborsClassifier
CAL_DTYPES = { "event_name_1": "category", "event_type_1": "category",
"weekday": "category", 'wm_yr_wk': 'int16', "wday": "int8",
"month": "int8", "year": "int16", "snap_CA": "float32", 'snap_TX': 'float32', 'snap_WI': 'float32' }
STE_DTYPES = { "item_id": "category", "dept_id": "category", "cat_id": "category", "store_id": "category",
"state_id": "category" }
PRICE_DTYPES = { "store_id": "category", "item_id": "category", "wm_yr_wk": "int16","sell_price":"float32" }<load_from_csv>
from sklearn.neighbors import KNeighborsClassifier
cal = pd.read_csv(".. /input/m5-forecasting-accuracy/calendar.csv", dtype = CAL_DTYPES)
 ste = pd.read_csv(".. /input/m5-forecasting-accuracy/sales_train_evaluation.csv", dtype = STE_DTYPES)
 pri = pd.read_csv(".. /input/m5-forecasting-accuracy/sell_prices.csv", dtype = PRICE_DTYPES )<feature_engineering>
from sklearn.neighbors import KNeighborsClassifier
print("canceled" )<data_type_conversions>
data = open_data("train.csv")
 X, Y = param_label(data)
 x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2 )
trans_cols = ["item_id", "dept_id", "cat_id", "store_id", "state_id"]
for col in trans_cols:
ste[col] = ste[col].cat.codes.astype("int16")
 trans_cols = ["item_id", "store_id"]
for col in trans_cols:
pri[col] = pri[col].cat.codes.astype("int16")
 trans_cols = ["event_name_1", "event_type_1"]
for col in trans_cols:
cal[col] = cal[col].cat.codes.astype("int8")+ 1
d_order = { 'Chanukah End': 0, 'Cinco De Mayo': 2, 'Easter': 4, "Father's day": 7, 'OrthodoxEaster': 20, "Purim End": 23 }
cal["event_name_2"] = cal["event_name_2"].map(d_order ).fillna(-1 ).astype("int8")+ 1
d_order = { 'Cultural': 0, 'Religious': 2 }
cal["event_type_2"] = cal["event_type_2"].map(d_order ).fillna(-1 ).astype("int8")+ 1
cal["date"] = pd.to_datetime(cal["date"] )<drop_column>
model = KNeighborsClassifier()
model.fit(x_train, y_train)
 acc = metrics.accuracy_score(model.predict(x_test), y_test)
 print("Accuracy : " + str(acc))
cal.drop(['weekday'], axis=1, inplace=True )<merge>
neighboors = [i for i in range(1, 101)]
averages = []
mins = []
maxs = []
for n in neighboors:
average_acc = 0
min_acc = 1
max_acc = 0
for i in range(100):
x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2)
 model = KNeighborsClassifier(n_neighbors = n)
 model.fit(x_train, y_train)
 acc = metrics.accuracy_score(model.predict(x_test), y_test)
 average_acc = average_acc + acc
if acc > max_acc: max_acc = acc
if acc < min_acc: min_acc = acc
averages = averages + [average_acc/100]
mins = mins + [min_acc]
maxs = maxs + [max_acc]
plt.figure(figsize=(24,8))
plt.plot(averages, color = 'r', linewidth=2)
 plt.plot(mins, color = 'r', linestyle='--')
 plt.plot(maxs, color = 'r', linestyle='--')
 plt.xticks(neighboors )
df = pd.melt(ste,id_vars=ste.columns.values[:6],var_name="d",value_name="sells")
 df["sells"] = df["sells"].astype("float32")
 df = df.merge(cal, on='d', copy = False)
 df = df.merge(pri, on=["store_id", "item_id", "wm_yr_wk"],copy = False)
<drop_column>
model = KNeighborsClassifier(n_neighbors = 12)
 model.fit(X, Y)
 data_test = open_data("test.csv")
 prediction = model.predict(data_test.drop(["PassengerId"], 1))
solution = pd.DataFrame(np.array([[data_test.PassengerId.iloc[i], prediction[i]] for i in range(len(data_test)) ]),
columns=['PassengerId', 'Survived'])
 solution.to_csv("solution_KNN_allfeatures.csv", index=False )
df.drop(df.index[df["wm_yr_wk"]<=11430], inplace=True)
 days_christmas = ["d_331","d_697","d_1062","d_1427","d_1792"]
for day in days_christmas:
df.drop(df.index[df["d"] == day], inplace=True )<drop_column>
data = open_data("train.csv")
 X, Y = param_label(data)
 X = X[["Pclass", "Sex"]]
x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2)
 model = KNeighborsClassifier()
model.fit(x_train, y_train)
 acc = metrics.accuracy_score(model.predict(x_test), y_test)
 print("Accuracy : " + str(acc))
<feature_engineering>
model = KNeighborsClassifier(n_neighbors = 8)
 model.fit(x_train, y_train)
 acc = metrics.accuracy_score(model.predict(x_test), y_test)
 print("Accuracy : " + str(acc))
def create_features(dt):
dt["lag_7"] = dt[["id","sells"]].groupby("id")["sells"].shift(7)
 dt["lag_28"] = dt[["id","sells"]].groupby("id")["sells"].shift(28)
 dt["win_7"] = dt[["id","sells"]].groupby("id")["sells"].transform(lambda x : x.rolling(7 ).mean() ).shift(1)
 dt["win_28"] = dt[["id","sells"]].groupby("id")["sells"].transform(lambda x : x.rolling(28 ).mean() ).shift(1)
 dt["win_7_lag_7"] = dt[["id","win_7"]].groupby("id")["win_7"].shift(7)
 dt["win_28_lag_28"] = dt[["id","win_28"]].groupby("id")["win_28"].shift(28)
 dt["price_lag_1"] = dt[["id","sell_price"]].groupby("id")["sell_price"].shift(1)
 dt["price_lag_7"] = dt[["id","sell_price"]].groupby("id")["sell_price"].shift(7)
 dt["price_win_7"] = dt[["id","sell_price"]].groupby("id")["sell_price"].transform(lambda x : x.rolling(7 ).mean() ).shift(1)
 dt["price_win_28"] = dt[["id","sell_price"]].groupby("id")["sell_price"].transform(lambda x : x.rolling(28 ).mean() ).shift(1 )<correct_missing_values>
model = KNeighborsClassifier(n_neighbors = 8)
 model.fit(X[["Pclass", "Sex"]], Y)
 data_test = open_data("test.csv")
 prediction = model.predict(data_test[["Pclass", "Sex"]])
 solution = pd.DataFrame(np.array([[data_test.PassengerId.iloc[i], prediction[i]] for i in range(len(data_test)) ]),
columns=['PassengerId', 'Survived'])
 solution.to_csv("solution_KNN_Class_Sex.csv", index=False )
create_features(df)
 df.dropna(inplace = True )<prepare_x_and_y>
data["FamilyMembers"] = data["SibSp"]+data["Parch"]
X, Y = param_label(data)
 X = X[["Pclass", "Sex", "FamilyMembers"]]
x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2)
 model = KNeighborsClassifier(n_neighbors = 8)
 model.fit(x_train, y_train)
 acc = metrics.accuracy_score(model.predict(x_test), y_test)
 print("Accuracy : " + str(acc))
features =['item_id', 'dept_id', 'store_id', 'cat_id', 'state_id',
'wday', 'month', 'year',
'event_name_1', 'event_name_2', 'event_type_1', 'event_type_2',
'snap_CA', 'snap_TX', 'snap_WI',
'sell_price',
'lag_7', 'lag_28', 'win_7', 'win_28', 'win_7_lag_7','win_28_lag_28',
'price_win_7', 'price_win_28']
X_train = df[features]
y_train = df['sells']
cat_features = ['item_id', 'dept_id','store_id', 'cat_id', 'state_id',
'event_name_1', 'event_name_2', 'event_type_1', 'event_type_2']<create_dataframe>
model = KNeighborsClassifier(n_neighbors = 8)
 model.fit(X[["Pclass", "Sex", "FamilyMembers"]], Y)
 data_test = open_data("test.csv")
 data_test["FamilyMembers"] = data_test["SibSp"]+data_test["Parch"]
prediction = model.predict(data_test[["Pclass", "Sex","FamilyMembers"]])
 solution = pd.DataFrame(np.array([[data_test.PassengerId.iloc[i], prediction[i]] for i in range(len(data_test)) ]),
columns=['PassengerId', 'Survived'])
 solution.to_csv("solution_KNN_Class_Sex_Family.csv", index=False )
np.random.seed(1000)
 fake_valid_index = np.random.choice(X_train.index.values, 2000000, replace = False)
 train_index = np.setdiff1d(X_train.index.values, fake_valid_index)
 train_data = lgb.Dataset(X_train.loc[train_index] , label = y_train.loc[train_index],
categorical_feature=cat_features, free_raw_data=False)
 fake_valid_data = lgb.Dataset(X_train.loc[fake_valid_index], label = y_train.loc[fake_valid_index],
categorical_feature=cat_features, free_raw_data=False )<prepare_x_and_y>
data = open_data("train.csv")
 X, Y = param_label(data)
 x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2)
 model = svm.SVC()
model.fit(x_train, y_train)
 y_predict = model.predict(x_test)
 acc = metrics.accuracy_score(y_predict, y_test)
 print("Accuracy:",acc )
del df, X_train, y_train, fake_valid_index, train_index
gc.collect()<init_hyperparams>
kernels = ["rbf", "linear", "sigmoid", "poly"]
for kernel in kernels:
model = svm.SVC(kernel = kernel)
 model.fit(x_train, y_train)
 y_predict = model.predict(x_test)
 acc = metrics.accuracy_score(y_predict, y_test)
 print("Accuracy with kernel =", kernel, ": ",acc )
params = {
"objective" : "tweedie",
"metric" : ["rmse"],
"force_row_wise" : True,
"learning_rate" : 0.07,
"bagging_freq" : 3,
"bagging_fraction" : 0.5,
"lambda_l2" : 0.1,
"num_iterations" : 1000,
"num_leaves" : 255,
"min_data_in_leaf": 128,
}<train_model>
model = svm.SVC(kernel = 'linear')
 model.fit(X,Y)
 data_test = open_data("test.csv")
 prediction = model.predict(data_test.drop(["PassengerId"], 1))
solution = pd.DataFrame(np.array([[data_test.PassengerId.iloc[i], prediction[i]] for i in range(len(data_test)) ]),
columns=['PassengerId', 'Survived'])
 solution.to_csv("solution_SVM.csv", index=False )
%%time
m_lgb = lgb.train(params, train_data, valid_sets = [fake_valid_data], verbose_eval=50 )<save_model>
cs = [1, 5, 10, 15, 20]
gammas = [0.005, 0.01, 0.02, 0.05, 0.1]
for gamma in gammas:
for c in cs:
model = svm.SVC(kernel = 'rbf', C = c, gamma = gamma)
 model.fit(x_train, y_train)
 acc = metrics.accuracy_score(model.predict(x_test), y_test)
 print("Accuracy with gamma = ",gamma,"c = ",c,": ",acc)
 print("" )
m_lgb.save_model("model.lgb" )<feature_engineering>
model = svm.SVC(kernel = 'rbf', gamma = 0.01, C = 10)
 model.fit(X,Y)
 data_test = open_data("test.csv")
 prediction = model.predict(data_test.drop(["PassengerId"], 1))
solution = pd.DataFrame(np.array([[data_test.PassengerId.iloc[i], prediction[i]] for i in range(len(data_test)) ]),
columns=['PassengerId', 'Survived'])
 solution.to_csv("solution_SVM_para.csv", index=False )
days = [f"d_{i}" for i in range(1942,1970)]
for day in days:
ste[day] = 0<merge>
from sklearn.ensemble import RandomForestClassifier
df = pd.melt(ste,id_vars=ste.columns.values[:6],var_name="d",value_name="sells")
 df["sells"] = df["sells"].astype("float32")
 df = df.merge(cal, on='d', copy = False)
 df = df.merge(pri, on=["store_id", "item_id", "wm_yr_wk"],copy = False )<drop_column>
data = open_data("train.csv")
 X, Y = param_label(data)
 x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2 )
df.drop(df.index[df["wm_yr_wk"]<=11607], inplace=True )<feature_engineering>
model = RandomForestClassifier()
model.fit(x_train, y_train)
 y_predict = model.predict(x_test)
 y_predict
acc = metrics.accuracy_score(y_predict, y_test)
 print(acc )
create_features(df )<predict_on_test>
trees = [5, 10, 20, 50, 100]
averages = []
mins = []
maxs = []
for tree in trees:
average_acc = 0
min_acc = 1
max_acc = 0
for i in range(100):
x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2)
 model = RandomForestClassifier(n_estimators = tree)
 model.fit(x_train, y_train)
 y_predict = model.predict(x_test)
 y_predict
acc = metrics.accuracy_score(y_predict, y_test)
 average_acc = average_acc + acc
if acc > max_acc: max_acc = acc
if acc < min_acc: min_acc = acc
averages = averages + [average_acc/100]
mins = mins + [min_acc]
maxs = maxs + [max_acc]
plt.figure(figsize=(24,8))
plt.plot(averages, color = 'r', linewidth=2)
 plt.plot(mins, color = 'r', linestyle='--')
 plt.plot(maxs, color = 'r', linestyle='--' )
%%time
for day in days:
X_pred = df[df["d"] == day][features]
y_pred = m_lgb.predict(X_pred)
 print(day)
 df.loc[df["d"] == day, "sells"] = y_pred
create_features(df )<prepare_output>
depths = [1, 2, 5, 10, 15, 20]
averages = []
mins = []
maxs = []
for depth in depths:
average_acc = 0
min_acc = 1
max_acc = 0
for i in range(100):
x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2)
 model = RandomForestClassifier(n_estimators = 20, max_depth = depth)
 model.fit(x_train, y_train)
 y_predict = model.predict(x_test)
 y_predict
acc = metrics.accuracy_score(y_predict, y_test)
 average_acc = average_acc + acc
if acc > max_acc: max_acc = acc
if acc < min_acc: min_acc = acc
averages = averages + [average_acc/100]
mins = mins + [min_acc]
maxs = maxs + [max_acc]
plt.figure(figsize=(24,8))
plt.plot(averages, color = 'r', linewidth=2)
 plt.plot(mins, color = 'r', linestyle='--')
 plt.plot(maxs, color = 'r', linestyle='--' )
sub = df[["id","d","sells"]].pivot(index="id", columns="d", values="sells")
 sub = sub.reset_index()
sub.columns.name = None<concatenate>
model = RandomForestClassifier(n_estimators = 20, max_depth = 10)
 model.fit(X,Y)
 data_test = open_data("test.csv")
 prediction = model.predict(data_test.drop(["PassengerId"], 1))
solution = pd.DataFrame(np.array([[data_test.PassengerId.iloc[i], prediction[i]] for i in range(len(data_test)) ]),
columns=['PassengerId', 'Survived'])
 solution.to_csv("solution_Random_Forest.csv", index=False )
sub1 = pd.concat([sub.T[0:1],sub.T[-56:-28]] ).T
sub2 = pd.concat([sub.T[0:1],sub.T[-28:]] ).T<feature_engineering>
model = RandomForestClassifier(n_estimators = 20, max_depth = 10)
 model.fit(X[["Sex", "Age", "Fare", "Pclass"]],Y)
 data_test = open_data("test.csv")
 prediction = model.predict(data_test[["Sex", "Age", "Fare", "Pclass"]])
 solution = pd.DataFrame(np.array([[data_test.PassengerId.iloc[i], prediction[i]] for i in range(len(data_test)) ]),
columns=['PassengerId', 'Survived'])
 solution.to_csv("solution_Random_Forest_4features.csv", index=False )
sub_columns = ["id"] + [f"F{i}" for i in range(1,29)]
sub1.columns = sub_columns
sub2.columns = sub_columns
sub1["id"] = sub1["id"].str.replace("evaluation", "validation" )<save_to_csv>
import tensorflow as tf
from tensorflow import keras
sub = pd.concat([sub2,sub1])
 sub.to_csv("submission.csv",index=False)
 sub<load_from_csv>
data = open_data("train.csv")
 X, Y = param_label(data)
 x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2 )
cal = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/calendar.csv')
 steval = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_evaluation.csv')
 price = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sell_prices.csv' )<prepare_x_and_y>
def build_model() :
model = keras.Sequential()
model.add(keras.layers.Dense(32, activation='relu', kernel_initializer = 'uniform', input_shape=[len(X.keys())]))
model.add(keras.layers.Dense(12, activation='relu', kernel_initializer = 'uniform'))
model.add(keras.layers.Dense(1, activation='sigmoid', kernel_initializer = 'uniform'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 return model
model = build_model()
model.summary()
id_list = sorted(list(set(steval['id'])))
 d_cols = [col for col in steval.columns if 'd_' in col]
x_1 = steval.loc[steval['id'] == id_list[0]].set_index('id')[d_cols].values[0][:200]
x_2 = steval.loc[steval['id'] == id_list[12]].set_index('id')[d_cols].values[0][300:500]
x_3 = steval.loc[steval['id'] == id_list[36]].set_index('id')[d_cols].values[0][600:800]
x_4 = steval.loc[steval['id'] == id_list[64]].set_index('id')[d_cols].values[0][1000:1200]
x_5 = steval.loc[steval['id'] == id_list[128]].set_index('id')[d_cols].values[0][1300:1500]
x_6 = steval.loc[steval['id'] == id_list[256]].set_index('id')[d_cols].values[0][1600:1800]
fig = make_subplots(rows=6, cols=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_1)) , y=x_1, showlegend=True,
mode='lines', name="1st sample",
marker=dict(color="cadetblue")) , row=1, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_2)) , y=x_2, showlegend=True,
mode='lines', name="2nd sample",
marker=dict(color="firebrick")) , row=2, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_3)) , y=x_3, showlegend=True,
mode='lines', name="3rd sample",
marker=dict(color="yellowgreen")) , row=3, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_4)) , y=x_4, showlegend=True,
mode='lines', name="4th sample",
marker=dict(color="orangered")) , row=4, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_5)) , y=x_5, showlegend=True,
mode='lines', name="5th sample",
marker=dict(color="mediumpurple")) , row=5, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_6)) , y=x_6, showlegend=True,
mode='lines', name="6th sample",
marker=dict(color="navy")) , row=6, col=1)
 fig.update_layout(height=1200, width=900, title_text="Observing some randomly chosen sales trend :")
 fig.show()<categorify>
model = build_model()
class PrintDot(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs):
if epoch % 100 == 0: print('')
 print('.', end='')
 EPOCHS = 1000
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=50)
 history = model.fit(normed_x_train, y_train, epochs=EPOCHS,
validation_split = 0.2, verbose=0, callbacks=[early_stop, PrintDot() ])
 plot_history(history )
for i in range(1942,1970):
col = 'd_' + str(i)
 steval[col] = 0
steval[col] = steval[col].astype(np.int16 )<categorify>
y_pred = model.predict(x_test)
 y_pred =(y_pred > 0.5 ).astype(int ).reshape(x_test.shape[0])
 metrics.accuracy_score(y_pred, y_test )
sales = pd.melt(steval, id_vars=['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'], var_name='d', value_name='sold' ).dropna()<merge>
architectures = [[12, 6],
[32, 16],
[64, 32],
[12, 12, 6],
[32, 16, 8],
[64, 32, 16],
[12, 12, 6, 6],
[32, 32, 16, 8],
[64, 32, 16, 8],
[64, 64, 32, 16, 8]]
sales = pd.merge(sales, cal, on='d', how='left')
 sales = pd.merge(sales, price, on=['store_id','item_id','wm_yr_wk'], how='left' )<define_variables>
def build_model(architecture):
model = keras.Sequential()
model.add(keras.layers.Dense(architecture[0], activation='relu', kernel_initializer = 'uniform', input_shape=[len(X.keys())]))
for i in range(1, len(architecture)) :
n = architecture[i]
model.add(keras.layers.Dense(n, activation='relu', kernel_initializer = 'uniform'))
model.add(keras.layers.Dense(1, activation='sigmoid', kernel_initializer = 'uniform'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 return model
d_id = dict(zip(sales.id.cat.codes, sales.id))
d_item_id = dict(zip(sales.item_id.cat.codes, sales.item_id))
d_dept_id = dict(zip(sales.dept_id.cat.codes, sales.dept_id))
d_cat_id = dict(zip(sales.cat_id.cat.codes, sales.cat_id))
d_store_id = dict(zip(sales.store_id.cat.codes, sales.store_id))
d_state_id = dict(zip(sales.state_id.cat.codes, sales.state_id))<data_type_conversions>
EPOCHS = 1000
averages = []
mins = []
maxs = []
for architecture in architectures:
model = build_model(architecture)
 average_acc = 0
min_acc = 1
max_acc = 0
for i in range(100):
x_train, x_test, y_train, y_test = subset_data(X, Y, 0.2)
 history = model.fit(normed_x_train, y_train, epochs=EPOCHS,
validation_split = 0.2, verbose=0, callbacks=[early_stop])
 y_pred = model.predict(x_test)
 y_pred =(y_pred > 0.5 ).astype(int ).reshape(x_test.shape[0])
 acc = metrics.accuracy_score(y_pred, y_test)
 average_acc = average_acc + acc
if acc > max_acc: max_acc = acc
if acc < min_acc: min_acc = acc
averages = averages + [average_acc/100]
mins = mins + [min_acc]
maxs = maxs + [max_acc]
print("Accuracy with architecture ", architecture, ": ", average_acc/100)
 plt.figure(figsize=(24,8))
plt.plot(averages, color = 'r', linewidth=2)
 plt.plot(mins, color = 'r', linestyle='--')
 plt.plot(maxs, color = 'r', linestyle='--' )