Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<data_type_conversions>
print(os.listdir(".. /input"))
size_img = 28
threshold_color = 100 / 255
file = open(".. /input/digit-recognizer/train.csv")
 data_train = pd.read_csv(file)
 y_train = np.array(data_train.iloc[:, 0])
 x_train = np.array(data_train.iloc[:, 1:])
 file = open(".. /input/digit-recognizer/test.csv")
 data_test = pd.read_csv(file)
 x_test = np.array(data_test)
 n_features_train = x_train.shape[1]
n_samples_train = x_train.shape[0]
n_features_test = x_test.shape[1]
n_samples_test = x_test.shape[0]
print(n_features_train, n_samples_train, n_features_test, n_samples_test)
 print(x_train.shape, y_train.shape, x_test.shape )
concat_data['pre_weekend_flg'] =(( concat_data['visit_day_of_week']>3)&(concat_data['visit_day_of_week']<6)).astype(int)
 concat_data['off_day_flg'] =(( concat_data['weekend_flg']==1)|(concat_data['holiday_flg']==1)).astype(int)
 concat_data['next_day_is_off_day'] = concat_data['off_day_flg'].shift(-1,fill_value=0 ).astype(int)
 concat_data["today_off_next_day_working"] =(( concat_data['off_day_flg']==1)|(concat_data['next_day_is_off_day']==0)).astype(int)
<categorify>
def int2float_grey(x):
x = x / 255
return x
concat_data = pd.get_dummies(concat_data, columns = ['air_genre_name'], prefix="", prefix_sep='')
<categorify>
def find_left_edge(x):
edge_left = []
n_samples = x.shape[0]
for k in range(n_samples):
for j in range(size_img):
for i in range(size_img):
if(x[k, size_img*i+j] >= threshold_color):
edge_left.append(j)
 break
if(len(edge_left)> k):
break
return edge_left
concat_data["area_1"] = concat_data["air_area_name"].str.split(" " ).str[0]
concat_data["area_2"] = concat_data["air_area_name"].str.split(" " ).str[1]
concat_data["area_3"] = concat_data["air_area_name"].str.split(" " ).str[2]
concat_data = pd.get_dummies(concat_data, columns = ['area_1'], prefix="", prefix_sep='')
 concat_data = pd.get_dummies(concat_data, columns = ['area_2'], prefix="", prefix_sep='')
 concat_data = pd.get_dummies(concat_data, columns = ['area_3'], prefix="", prefix_sep='')
 concat_data.head()<sort_values>
def find_top_edge(x):
edge_top = []
n_samples = x.shape[0]
for k in range(n_samples):
for i in range(size_img):
for j in range(size_img):
if(x[k, size_img*i+j] >= threshold_color):
edge_top.append(i)
 break
if(len(edge_top)> k):
break
return edge_top
air_visit.sort_values(by="visitors", ascending=False ).head()<define_variables>
def stretch_image(x):
edge_left = find_left_edge(x)
 edge_right = find_right_edge(x)
 edge_top = find_top_edge(x)
 edge_bottom = find_bottom_edge(x)
 n_samples = x.shape[0]
x = x.reshape(n_samples, size_img, size_img)
 for i in range(n_samples):
x[i] = transform.resize(x[i][edge_top[i]:edge_bottom[i]+1, edge_left[i]:edge_right[i]+1],(size_img, size_img))
x = x.reshape(n_samples, size_img ** 2)
 show_img(x )
without_columns = ["air_store_id", "visit_date", "day_of_week", "visit_day_of_week", "air_area_name", "hpg_store_id", "area_1", "area_2", "area_3"]
selected_columns = [x for x in concat_data.columns if x not in without_columns]
train_data = concat_data[selected_columns] 
<prepare_x_and_y>
def get_threshold(x_train, x_test):
selector = VarianceThreshold(threshold=0 ).fit(x_train)
 x_train = selector.transform(x_train)
 x_test = selector.transform(x_test)
 print(x_train.shape)
 return x_train, x_test
x_train = train_data[:air_visit_count]
x_test = train_data[air_visit_count:]
y_train = x_train["visitors"]
x_train = x_train.drop("visitors", axis=1)
 x_test = x_test.drop("visitors", axis=1)
 print(x_train.head(2))
print("==============================")
 print(y_train.head(2))
print("==============================")
 print(x_test.head(2))<train_model>
def get_pca(x_train, x_test):
pca = PCA(n_components=0.95)
 pca.fit(x_train)
 x_train = pca.transform(x_train)
 x_test = pca.transform(x_test)
 print(x_train.shape, x_test.shape)
 return x_train, x_test
model = XGBRegressor(random_state=110, verbosity=0, nthread=23, n_estimators=980, max_depth=4)
 model.fit(x_train, y_train )<predict_on_test>
x_train = int2float_grey(x_train)
 x_test = int2float_grey(x_test)
pred = model.predict(x_test)
 print(len(pred))
pred = np.expm1(pred )<save_to_csv>
def general_function(mod_name, model_name):
y_pred = model_train_predict(mod_name, model_name)
 output_prediction(y_pred, model_name )
submission['visitors'] = pred
submission.to_csv('sampleSubmission.csv', index=False )<feature_engineering>
def model_train_predict(mod_name, model_name):
import_mod = __import__(mod_name, fromlist = str(True))
if hasattr(import_mod, model_name):
f = getattr(import_mod, model_name)
 else:
print("404")
 return []
clf = f()
clf.fit(x_train, y_train)
 y_pred = clf.predict(x_train)
 get_acc(y_pred, y_train)
 scores = cross_val_score(clf, x_train, y_train, cv=5)
 print("Accuracy: %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
y_pred = clf.predict(x_test)
 return y_pred
first_sub = submission.copy()
first_sub['tmp'] = np.nan
first_sub['air_store_id'] = first_sub.id.map(lambda x: '_'.join(x.split('_')[:-1]))
first_sub['date'] = first_sub.id.map(lambda x: x.split('_')[2])
 first_sub['date'] =first_sub['date'].astype('datetime64[ns]')
 first_sub.loc[first_sub.date=='2017-5-3','tmp'] = np.sqrt(first_sub.loc[first_sub.date=='2017-4-29','visitors'].values*first_sub.loc[first_sub.date=='2017-5-13','visitors'].values)
 first_sub.loc[first_sub.date=='2017-5-4','tmp'] = np.sqrt(first_sub.loc[first_sub.date=='2017-4-29','visitors'].values*first_sub.loc[first_sub.date=='2017-5-13','visitors'].values)
 first_sub.loc[first_sub.date=='2017-5-5','tmp'] = np.sqrt(first_sub.loc[first_sub.date=='2017-4-29','visitors'].values*first_sub.loc[first_sub.date=='2017-5-13','visitors'].values)
 first_sub.loc[first_sub.date=='2017-5-2','tmp'] = np.sqrt(first_sub.loc[first_sub.date=='2017-4-28','visitors'].values*first_sub.loc[first_sub.date=='2017-5-12','visitors'].values)
 first_sub.loc[first_sub.date=='2017-5-3','visitors'] = first_sub.loc[first_sub.date=='2017-5-3','tmp']
first_sub.loc[first_sub.date=='2017-5-4','visitors'] = first_sub.loc[first_sub.date=='2017-5-4','tmp']
first_sub.loc[first_sub.date=='2017-5-5','visitors'] = first_sub.loc[first_sub.date=='2017-5-5','tmp']
first_sub.loc[first_sub.date=='2017-5-2','visitors'] = first_sub.loc[first_sub.date=='2017-5-2','tmp']
first_sub[['id','visitors']] 
<load_from_csv>
def get_acc(y_pred, y_train):
right_num =(y_train == y_pred ).sum()
print("acc: ", right_num/n_samples_train )
sub_file = pd.read_csv(r'.. /input/tpsjan2021/submission_blend_1.csv')
 df_sub = pd.read_csv(r'.. /input/tpsjan2021/best_blend_1.csv')
 df_sub = df_sub.iloc[:, :14]
plt.figure(figsize=(10,10))
sns.heatmap(df_sub.corr() , cmap='Spectral')
 plt.ylabel('file index numbers')
 plt.xlabel('file index numbers')
 plt.show()<prepare_output>
def output_prediction(y_pred, model_name):
print(y_pred)
 data_predict = {"ImageId":range(1, n_samples_test+1), "Label":y_pred}
data_predict = pd.DataFrame(data_predict)
 data_predict.to_csv("dr output %s.csv" %model_name, index = False )
df_sub['weighted_avg'] = abs(1 *(
 -10 *(30 * df_sub['3'] + 30 * df_sub['4'] + 30 * df_sub['6'] +
50 * df_sub['8'] + 1 * df_sub['10'] + 1 * df_sub['11'])/ 142 +
5 *(170 * df_sub['2'] + 20 * df_sub['7'] + 5 * df_sub['12'] + 1 * df_sub['13'])/ 196 +
150 *(150 * df_sub['0'] + 50 * df_sub['1'] -10 * df_sub['5'] - 25 * df_sub['9'])/ 165 
)/ 145)
 submission = pd.DataFrame({'ID': sub_file.ID, 'target': df_sub['weighted_avg'].tolist() })
 submission.to_csv(r'submission.csv', index=False )<import_modules>
mod_name = "sklearn.naive_bayes"
model_name = "GaussianNB"
import numpy as np
import pandas as pd
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
import sklearn.datasets
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import optuna
import lightgbm as lgb
import xgboost as xgb
import seaborn as sns
import matplotlib.pyplot as plt<load_from_csv>
mod_name = "sklearn.neighbors"
model_name = "KNeighborsClassifier"
train = pd.read_csv('.. /input/tabular-playground-series-jan-2021/train.csv')
 test = pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv')
 sub = pd.read_csv('.. /input/tabular-playground-series-jan-2021/sample_submission.csv')
 features = [f'cont{x}'for x in range(1,15)]
data = train[features]
X_test = test[features]
target = train['target']
train.head()<init_hyperparams>
mod_name = "sklearn.cluster"
model_name = "KMeans"
Best_trial = {'lambda': 0.0030282073258141168, 'alpha': 0.01563845128469084, 'colsample_bytree': 0.5,
'subsample': 0.7,'n_estimators': 4000, 'learning_rate': 0.01,'max_depth': 15,
'random_state': 2020, 'min_child_weight': 257,'tree_method':'gpu_hist'
,'predictor': 'gpu_predictor'}
train = train_data
test = test_data
preds = np.zeros(test.shape[0])
 kf = KFold(n_splits=7,random_state=48,shuffle=True)
 rmse=[]
n=0
for trn_idx, test_idx in kf.split(train[features],target):
X_tr,X_val=train[features].iloc[trn_idx],train[features].iloc[test_idx]
y_tr,y_val=target.iloc[trn_idx],target.iloc[test_idx]
model = xgb.XGBRegressor(**Best_trial)
 model.fit(X_tr,y_tr,eval_set=[(X_val,y_val)],early_stopping_rounds=100,verbose=False)
 preds+=model.predict(test[features])/kf.n_splits
rmse.append(mean_squared_error(y_val, model.predict(X_val), squared=False))
print(n+1,rmse[n])
 n+=1
print(f"mean RMSE for all the folds is {np.mean(rmse)}" )<load_from_csv>
mod_name = "sklearn.svm"
model_name = "SVC"
sub1 = pd.read_csv('.. /input/results-driven-tabular-playground-series-201/submission.csv')
 predictions1 = sub1['target'].tolist()
results = [x*0.66 + y*0.34 for x, y in zip(predictions1, preds)] 
<init_hyperparams>
mod_name = "sklearn.tree"
model_name = "DecisionTreeClassifier"
Best_trial ={'random_state': 33,'n_estimators':5000,
'min_data_per_group': 5,
'boosting_type': 'gbdt',
'device_type' : 'gpu',
'num_leaves': 256,
'num_iterations' : 5000,
'max_dept': -1,
'learning_rate': 0.005,
'subsample_for_bin': 200000,
'lambda_l1': 1.074622455507616e-05,
'lambda_l2': 2.0521330798729704e-06,
'n_jobs': -1,
'cat_smooth': 1.0,
'silent': True,
'importance_type': 'split',
'metric': 'rmse',
'feature_pre_filter': False,
'bagging_fraction': 0.8206341150202605,
'min_data_in_leaf': 100,
'min_sum_hessian_in_leaf': 0.001,
'bagging_freq': 6,
'feature_fraction': 0.5,
'min_gain_to_split': 0.0,
'min_child_samples': 20}<train_model>
mod_name = "sklearn.ensemble"
model_name = "RandomForestClassifier"
preds = np.zeros(test.shape[0])
 kf = KFold(n_splits=10,random_state=48,shuffle=True)
 rmse=[]
n=0
for trn_idx, test_idx in kf.split(train[features],target):
X_tr,X_val=train[features].iloc[trn_idx],train[features].iloc[test_idx]
y_tr,y_val=target.iloc[trn_idx],target.iloc[test_idx]
model = lgb.LGBMRegressor(**Best_trial)
 model.fit(X_tr,y_tr,eval_set=[(X_val,y_val)],early_stopping_rounds=50,verbose=False)
 preds+=model.predict(test[features])/kf.n_splits
rmse.append(mean_squared_error(y_val, model.predict(X_val), squared=False))
print(n+1,rmse[n])
 n+=1
print(f"mean RMSE for all the folds is {np.mean(rmse)}" )<save_to_csv>
y_train = keras.utils.to_categorical(y_train, num_classes=10)
 y_all_pred = np.zeros(( 3, n_samples_test)).astype(np.int64)
 print(y_all_pred.dtype )
final_results = [x*0.72 + y*0.28 for x, y in zip(results, preds)]
sub['target']=final_results
sub.to_csv('submission.csv', index=False )<install_modules>
model_name = "LaNet5"
model = Sequential()
model.add(Reshape(target_shape=(1, 28, 28), input_shape=(784,)))
 model.add(Conv2D(kernel_size=(3, 3), filters=6, padding="same", data_format="channels_first", kernel_initializer="uniform", use_bias=False))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Conv2D(kernel_size=(5, 5), filters=16, padding="same", data_format="channels_first", kernel_initializer="uniform", use_bias=False))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Conv2D(kernel_size=(5, 5), filters=120, padding="same", data_format="channels_first", kernel_initializer="uniform", use_bias=False))
model.add(Flatten())
 model.add(Dense(output_dim=120, activation='relu'))
model.add(Dense(output_dim=120, activation='relu'))
model.add(Dense(output_dim=10, activation='softmax'))
adam = keras.optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
 model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
 model.fit(x_train, y_train, epochs=30, batch_size=64)
 y_pred = model.predict_classes(x_test)
 output_prediction(y_pred, model_name)
 y_all_pred[0] = y_pred
!pip install --up.. /input/rfcx-species-audio-detection/he-torch
!ltt install torch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2
!pip install fastaudio==1.0.0
!pip install wandb --upgrade<categorify>
model_name = "CNN"
model = Sequential()
model.add(Reshape(target_shape=(1, 28, 28), input_shape=(784,)))
 model.add(Conv2D(kernel_size=(3, 3), filters=32, padding="same", data_format="channels_first", kernel_initializer="uniform", use_bias=False))
model.add(AveragePooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Conv2D(kernel_size=(3, 3), filters=32, padding="same", data_format="channels_first", kernel_initializer="uniform", use_bias=False))
model.add(AveragePooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Conv2D(kernel_size=(3, 3), filters=64, padding="same", data_format="channels_first", kernel_initializer="uniform", use_bias=False))
model.add(AveragePooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(output_dim=256, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(output_dim=256, activation='relu'))
model.add(Dense(output_dim=10, activation='softmax'))
adam = keras.optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
 model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
 model.fit(x_train, y_train, epochs=50, batch_size=128)
 y_pred = model.predict_classes(x_test)
 output_prediction(y_pred, model_name)
 y_all_pred[1] = y_pred
def placeholder(x):
raise pkg_resources.DistributionNotFound
pkg_resources.get_distribution = placeholder<set_options>
model_name = "ComplexCNN"
model = Sequential()
model.add(Reshape(target_shape=(1, 28, 28), input_shape=(784,)))
 model.add(Conv2D(32,(3, 3), padding='same', activation='relu'))
model.add(Conv2D(32,(3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Conv2D(32,(3, 3), padding='same', activation='relu'))
model.add(Conv2D(32,(3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))
model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(output_dim=256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(output_dim=256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(output_dim=10, activation='softmax'))
adam = keras.optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
 model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
 model.fit(x_train, y_train, epochs=80, batch_size=128)
 y_pred = model.predict_classes(x_test)
 output_prediction(y_pred, model_name)
 y_all_pred[2] = y_pred
<import_modules><EOS> 
model_name = "Ensemble"
print(y_pred.shape)
 y_ensem_pred = np.zeros(( n_samples_test,))
for i,line in enumerate(y_all_pred.T):
y_ensem_pred[i] = np.argmax(np.bincount(line))
print(y_ensem_pred.shape, y_ensem_pred)
 y_ensem_pred = y_ensem_pred.astype("int64")
 output_prediction(y_ensem_pred, model_name )