Kaggle competition: Predict Future Sales
for dataset in combine:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age']=4
train_df.head() 
<drop_column>
pd.options.display.max_rows = 50
plt.style.use("seaborn" )
train_df = train_df.drop(['AgeBand'], axis=1)
 combine = [train_df, test_df]
train_df.head()<sort_values>
def compact_types(df):
for col in df.columns:
if df[col].dtype == np.float64:
df[col] = df[col].astype(np.float32)
 elif df[col].dtype == np.int64:
df[col] = df[col].astype(np.int32 )
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
train_df[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=True)\ 
.mean().sort_values(by='Survived', ascending=False )<feature_engineering>
shops = pd.read_csv(in_path + "shops.csv", header=0, names=["name", "shop"], index_col="shop")
 cats = pd.read_csv(in_path + "item_categories.csv", header=0, names=["name", "cat"], index_col="cat")
 items = pd.read_csv(in_path + "items.csv", header=0, names=["name", "item", "cat"], index_col="item")
 train = pd.read_csv(in_path + "sales_train.csv", header=0,
names=["date", "block", "shop", "item", "price", "count"],
usecols=[1, 2, 3, 4, 5] 
)
 test = pd.read_csv(in_path + "test.csv", header=0, names=["ID", "shop", "item"] )
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
train_df[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=True ).mean()<drop_column>
shops_to_replace = { 0: 57, 1: 58, 11: 10, 23: 24, 40: 39 }
train["shop"].replace(shops_to_replace, inplace=True)
 test ["shop"].replace(shops_to_replace, inplace=True)
 shops.drop(index=shops_to_replace.keys() , inplace=True )
for df in combine:
df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1, inplace=True)
 train_df.head()<feature_engineering>
print("Deleting shops:", shops_to_delete)
 train = train[ ~train["shop"].isin(shops_to_delete)]
shops.drop(index=shops_to_delete, inplace=True )
for dataset in combine:
dataset['Age*Class'] = dataset.Age * dataset.Pclass
train_df.loc[:, ['Age*Class', 'Age', 'Pclass']].head()<groupby>
cities = shops["name"].str.extract("^([^ ]+)")[0]
shops["city"], cities = cities.factorize()
cities = cities.to_frame(index=False, name="name")
 cities.index.name = "city"
print("Extracted", len(cities), "cities" )
train_df[['Age*Class', 'Survived']].groupby(['Age*Class'], as_index=True ).mean()<sort_values>
cats["is_digital"] = cats["name"].str.contains("Цифра")
 names = cats["name"].str.replace("(Цифра)", "", regex=False)
 subcats = names.str.extract("^([^-]+)-? (.*)$")
 majors = subcats[0].str.strip()
minors = subcats[1].str.strip()
del subcats
minors[ minors == "" ] = majors[ minors == "" ]
cats["major"], majors = majors.factorize()
cats["minor"], minors = minors.factorize()
majors = majors.to_frame(index=False, name="name")
 majors.index.name = "major"
minors = minors.to_frame(index=False, name="name")
 minors.index.name = "minor"
print("Extracted", len(majors), "major and", len(minors), "minor categories" )
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)
 train_df[['Embarked', 'Survived']].groupby(['Embarked'], as_index=True)\ 
.mean().sort_values(by='Survived', ascending=False)
<categorify>
outlier_index =(train["price"] <= 0)|(train["price"] > 49000)
 train[outlier_index]
for dataset in combine:
binary_encoded = pd.get_dummies(dataset.Embarked)
 newcols = binary_encoded.columns
dataset[newcols] = binary_encoded
train_df.head()<drop_column>
index_names = ["block", "shop", "item"]
train.set_index(index_names, inplace=True)
 train = train.join(shops[["city"]])
 train = train.join(items[["cat" ]])
 train = train.join(cats [["is_digital", "major", "minor"]], on="cat")
 compact_types(train)
 train.sort_index(inplace=True)
 train
for dataset in combine:
dataset.drop('Embarked', axis=1, inplace=True )<data_type_conversions>
def get_sales(col):
sales = train.groupby(col ).agg(
 count=("count", "sum"),
count_mean=("count", "mean"),
price=("price", "mean"), 
)
 sales = sales.add_prefix(col + "_")
 compact_types(sales)
 return sales
blocks = get_sales("block")
 shops = shops.join(get_sales("shop"))
cities = cities.join(get_sales("city"))
items = items.join(get_sales("item"))
cats = cats.join(get_sales("cat"))
compact_types(train )
test_df['Fare'].fillna(test_df['Fare'].dropna().median() , inplace=True)
 test_df.head()<data_type_conversions>
index_names = ["block", "shop", "item"]
sales_block = train.groupby(index_names ).agg(
 block_shop_item_count=("count", "sum"),
block_shop_item_count_mean=("count", "mean"),
block_shop_item_price=("price", "mean"), 
)
 sales_block["block_shop_item_count"] = sales_block["block_shop_item_count"].clip(0, 20 )
for dataset in combine:
dataset['Fare']=pd.qcut(train_df['Fare'],4,labels=np.arange(4))
dataset['Fare'] = dataset['Fare'].astype(int)
 train_df[['Fare','FareBand']].head()<drop_column>
sales_block =(sales_block.join(blocks)
.join(shops.drop(columns="name")) 
.join(cities.drop(columns="name"), on="city")
.join(items.drop(columns="name")) 
.join(cats.drop(columns="name"), on="cat")
)
 del blocks, shops, cities, items, cats, majors, minors
train_df = train_df.drop(['FareBand'], axis=1)
 combine = [train_df, test_df]<train_model>
feat_names = ["shop", "item", "city", "cat"]
for feat in tqdm(feat_names):
sales_feat = train.groupby(["block", feat] ).agg(
 count=("count", "sum"),
count_mean=("count", "mean"),
price=("price", "mean"), 
)
 sales_feat = sales_feat.add_prefix("block_" + feat + "_")
 sales_block = sales_block.join(sales_feat, on=["block", feat])
 metric = feat + "_price"
block_price = sales_block["block_" + metric]
price = sales_block[metric]
sales_block["block_" + metric + "_change"] =(block_price - price)/ price
del sales_feat, block_price, price
del train
compact_types(sales_block)
 sales_block.info()
train_df.head(7 )<prepare_x_and_y>
sales_block.drop(columns=[
name for name in sales_block.columns
if "block_" in name and "_lag" not in name and name != "block_shop_item_count"
], inplace=True)
 sales_block.reset_index(inplace=True )
X = train_df.drop("Survived", axis=1)
 Y = train_df["Survived"]
X_submission = test_df.drop("PassengerId", axis=1 ).copy()
print(X.shape, Y.shape )<train_model>
sales_block["year"] =(sales_block["block"] // 12 + 2013)
 sales_block["month"] =(sales_block["block"] % 12 + 1)
 dates = sales_block[["year", "month"]].copy()
dates["day"] = 1
sales_block["days"] = pd.to_datetime(dates ).dt.daysinmonth
compact_types(sales_block )
np.random.seed(1337)
 X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.2)
 print(X_train.shape, Y_train.shape)
 print(X_val.shape, Y_val.shape )<choose_model_class>
sales_block.fillna(0, inplace=True)
 for name in tqdm(sales_block.columns):
if "count" in name and "mean" not in name and "change" not in name:
if sales_block[name].dtype != np.int32:
sales_block[name] = sales_block[name].astype(np.int32)
 sales_block.to_pickle("sales.pickle")
 sales_block.info()
logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
 Y_pred = logreg.predict(X_val)
 acc_logreg = sum(Y_pred == Y_val)/len(Y_val)*100
print('Logistic Regression labeling accuracy:', str(round(acc_logreg,2)) ,'%' )<compute_test_metric>
sales_block = pd.read_pickle("sales.pickle")
 test = pd.read_csv(in_path + "test.csv", header=0, names=["ID", "shop", "item"])
 sales_block = sales_block[[
"block",
"shop",
"item",
"block_shop_item_count",
"city",
"cat",
"is_digital",
"major",
"minor",
"month",
"days",
"block_shop_item_count_lag1",
"block_shop_item_price_lag1",
"block_count_mean_lag1",
"block_shop_count_mean_lag1",
"block_shop_price_change_lag1",
"block_item_count_mean_lag1",
"block_item_price_change_lag1",
"block_city_count_mean_lag1",
"block_cat_count_mean_lag1",
"block_shop_item_count_lag2",
"block_shop_count_mean_lag2",
"block_item_count_mean_lag2",
"block_shop_item_count_lag3",
"block_shop_count_mean_lag3",
"block_item_count_mean_lag3",
"block_shop_item_count_lag6",
"block_shop_count_mean_lag6",
"block_item_count_mean_lag6",
"block_shop_item_count_lag12",
"block_shop_count_mean_lag12",
"block_item_count_mean_lag12",
]]
sales_block.info()
acc_log_2 = logreg.score(X_val, Y_val)
 print('Logistic Regression using built-in method:', str(round(acc_log_2*100,2)) ,'%' )<train_model>
test_block = sales_block["block"].max()
valid_block = test_block - 1
X_train = sales_block[ sales_block["block"] < valid_block ].drop(columns="block_shop_item_count")
 y_train = sales_block[ sales_block["block"] < valid_block ]["block_shop_item_count"]
print("X_train:", X_train.shape, "y_train:", y_train.shape)
 X_valid = sales_block[ sales_block["block"] == valid_block ].drop(columns="block_shop_item_count")
 y_valid = sales_block[ sales_block["block"] == valid_block ]["block_shop_item_count"]
print("X_valid:", X_valid.shape, "y_valid:", y_valid.shape)
 X_test = sales_block[ sales_block["block"] == test_block ].drop(columns="block_shop_item_count")
 print("X_test :", X_test.shape)
 del sales_block
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, Y_train)
 acc_knn = knn.score(X_val, Y_val)
 print('K-Nearest Neighbors labeling accuracy:', str(round(acc_knn*100,2)) ,'%' )<train_model>
def print_rmse(which, y_true, y_pred):
print("RMSE", which, np.sqrt(np.square(y_true - y_pred ).mean()))
svc = SVC()
svc.fit(X_train, Y_train)
 acc_svc = svc.score(X_val, Y_val)
 print('Support Vector Machines labeling accuracy:', str(round(acc_svc*100,2)) ,'%' )<train_model>
model_xgb = XGBRegressor(
 max_depth=8,
n_estimators=1000,
min_child_weight=300,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=42,
tree_method=tree_method,
n_jobs=-1, 
)
 model_xgb.fit(
 X_train, y_train,
eval_metric="rmse",
eval_set=[(X_valid, y_valid)],
verbose=True,
early_stopping_rounds=10
)
perceptron = Perceptron()
perceptron.fit(X_train, Y_train)
 acc_perceptron = perceptron.score(X_val, Y_val)
 print('Perceptron labeling accuracy:', str(round(acc_perceptron*100,2)) ,'%' )<train_model>
print_rmse("train", y_train, model_xgb.predict(X_train))
valid_xgb = model_xgb.predict(X_valid)
 print_rmse("valid", y_valid, valid_xgb)
 test_xgb = model_xgb.predict(X_test )
gradboost = xgb.XGBClassifier(n_estimators=1000)
 gradboost.fit(X_train, Y_train)
 acc_xgboost = gradboost.score(X_val, Y_val)
 print('XGBoost labeling accuracy:', str(round(acc_xgboost*100,2)) ,'%' )<train_model>
submission = X_test[["shop", "item"]].copy()
submission["item_cnt_month"] = test_xgb.clip(0, 20)
 submission = submission.merge(test, on=["shop", "item"])
 submission = submission[["ID", "item_cnt_month"]]
submission.sort_values("ID", inplace=True )
random_forest = RandomForestClassifier(n_estimators=500)
 random_forest.fit(X_train, Y_train)
 acc_rf = random_forest.score(X_val, Y_val)
 print('K-Nearest Neighbors labeling accuracy:', str(round(acc_rf*100,2)) ,'%' )<import_modules>
submission.to_csv("submission.csv", index=False )
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense<choose_model_class>
submission.to_csv("submission.csv", index=False )
<train_model><EOS> 
model_xgb.save_model("model_xgb.json")
 X_test.to_pickle("X_test.pickle" )