Kaggle competition: Digit Recognizer
test['pd_date'] = pd.to_datetime(test['Open Date'], format='%m/%d/%Y')
 test['Open_Year'] = test['pd_date'].dt.strftime('%Y')
 test['Open_Month'] = test['pd_date'].dt.strftime('%m')
 test["Open Date"] = pd.to_datetime(test["Open Date"])
 test["Day"] = test["Open Date"].apply(lambda x:x.day)
 test["kijun"] = "2015-04-27"
test["kijun"] = pd.to_datetime(test["kijun"])
 test["BusinessPeriod"] =(test["kijun"] - test["Open Date"] ).apply(lambda x: x.days)
 test = test.drop('kijun', axis=1)
 test = test.drop('pd_date',axis=1)
 test = test.drop('Open Date',axis=1 )<count_values>
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
train.dtypes.value_counts()<count_unique_values>
from sklearn.model_selection import train_test_split
train.nunique(axis=0 )<define_variables>
from sklearn.model_selection import train_test_split
print(f'categorical variables: {cats}')
 print(f'numerical variables: {nums}' )<define_variables>
X = train.drop('label',axis=1)
 y = train['label']
nominal_list =cats
num_list = nums 
<drop_column>
X = X/255
test = test/255
train = train.drop('Open_Month',axis=1)
 test= test.drop('Open_Month',axis=1)
 nominal_list.remove('Open_Month' )<feature_engineering>
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten
from tensorflow.keras.callbacks import EarlyStopping
mean_revenue_per_city = train[['City', 'revenue']].groupby('City', as_index=False ).mean()
mean_revenue_per_city.head()
mean_revenue_per_city['revenue'] = mean_revenue_per_city['revenue'].apply(lambda x: int(x/1e6))
mean_revenue_per_city
mean_dict = dict(zip(mean_revenue_per_city.City, mean_revenue_per_city.revenue))
mean_dict<count_values>
early_stop = EarlyStopping(monitor='accuracy',mode='max',min_delta=0.005,verbose=7,patience=5 )
l1_l2_and = set(city_train_list)& set(city_test_list)
 print(l1_l2_and)
 print(len(l1_l2_and))<concatenate>
model = Sequential()
model.add(Conv2D(64, 3, activation='relu', input_shape=(28, 28, 1)))
 model.add(MaxPooling2D(( 2, 2)))
 model.add(Dropout(0.5))
model.add(Conv2D(32, 3, activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(128,activation='relu'))
model.add(Dense(10,activation='softmax'))
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'] )
len(set(city_test_list ).difference(city_train_list))<concatenate>
model.fit(X,y, epochs=1000, callbacks=[early_stop] )
len(set(city_train_list ).difference(city_test_list))<predict_on_test>
predictions = model.predict_classes(test )
def adjust_cities(full_full_data, train, k):
relevant_pvars = ["P1", "P2", "P11", "P19", "P20", "P23","P30"]
train = train.loc[:, relevant_pvars]
kmeans = cluster.KMeans(n_clusters=k)
 kmeans.fit(train)
 full_data['City_Cluster'] = kmeans.predict(full_data.loc[:, relevant_pvars])
 return full_data<concatenate>
df = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv' )
num_train = train.shape[0]
num_test = test.shape[0]
print(num_train, num_test)
 full_data = pd.concat([train, test], ignore_index=True )<drop_column>
df['Label'] = predictions
full_data = adjust_cities(full_data, train, 20)
 full_data
full_data = full_data.drop(['City'], axis=1 )<train_model>
df.to_csv('submission.csv',line_terminator='\r
', index=False )