Kaggle competition: Digit Recognizer
cluster_features = [
"LotArea",
"TotalBsmtSF",
"FirstFlrSF",
"SecondFlrSF",
"GrLivArea",
]<find_best_model_class>
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv');
test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv');
def cluster_labels(df, features, n_clusters=20):
X = df.copy()
X_scaled = X.loc[:, features]
X_scaled =(X_scaled - X_scaled.mean(axis=0)) / X_scaled.std(axis=0)
 kmeans = KMeans(n_clusters=n_clusters, n_init=50, random_state=0)
 X_new = pd.DataFrame()
X_new["Cluster"] = kmeans.fit_predict(X_scaled)
 return X_new<normalization>
rows = 28
cols = 28
tot_rows = train.shape[0]
X_train = train.values[:,1:]
y_train = keras.utils.to_categorical(train.label, 10)
 X_train = X_train.reshape(tot_rows, rows, cols, 1)/255.0
X_test = test.values[:]
test_num_img = test.shape[0]
X_test = X_test.reshape(test_num_img, rows, cols, 1)/255.0
def cluster_distance(df, features, n_clusters=20):
X = df.copy()
X_scaled = X.loc[:, features]
X_scaled =(X_scaled - X_scaled.mean(axis=0)) / X_scaled.std(axis=0)
 kmeans = KMeans(n_clusters=20, n_init=50, random_state=0)
 X_cd = kmeans.fit_transform(X_scaled)
 X_cd = pd.DataFrame(
 X_cd, columns=[f"Centroid_{i}" for i in range(X_cd.shape[1])] 
)
 return X_cd<create_dataframe>
classifier = Sequential()
classifier.add(Conv2D(32,(5,5),input_shape=(28,28,1),activation = 'relu',padding='same'))
classifier.add(BatchNormalization())
 classifier.add(Conv2D(32,(3,3),activation = 'relu',padding='same'))
classifier.add(BatchNormalization())
 classifier.add(MaxPooling2D(pool_size=(2,2), strides=None))
classifier.add(BatchNormalization())
 classifier.add(Dropout(0.25))
classifier.add(Conv2D(64,(5,5),activation = 'relu',padding='same'))
classifier.add(BatchNormalization())
 classifier.add(Conv2D(64,(3,3),activation = 'relu',padding='same'))
classifier.add(BatchNormalization())
 classifier.add(Conv2D(64,(3,3),strides=(2,2),activation = 'relu',padding='same'))
classifier.add(BatchNormalization())
 classifier.add(Dropout(0.25))
classifier.add(Flatten())
 classifier.add(Dense(units=128,activation='relu'))
classifier.add(Dropout(0.4))
classifier.add(Dense(units=10,activation='softmax'))
def apply_pca(X, standardize=True):
if standardize:
X =(X - X.mean(axis=0)) / X.std(axis=0)
 pca = PCA()
X_pca = pca.fit_transform(X)
 component_names = [f"PC{i+1}" for i in range(X_pca.shape[1])]
X_pca = pd.DataFrame(X_pca, columns=component_names)
 loadings = pd.DataFrame(
 pca.components_.T,
columns=component_names,
index=X.columns, 
)
 return pca, X_pca, loadings
def plot_variance(pca, width=8, dpi=100):
fig, axs = plt.subplots(1, 2)
 n = pca.n_components_
grid = np.arange(1, n + 1)
 evr = pca.explained_variance_ratio_
axs[0].bar(grid, evr)
 axs[0].set(
 xlabel="Component", title="% Explained Variance", ylim=(0.0, 1.0)
)
 cv = np.cumsum(evr)
 axs[1].plot(np.r_[0, grid], np.r_[0, cv], "o-")
 axs[1].set(
 xlabel="Component", title="% Cumulative Variance", ylim=(0.0, 1.0)
)
 fig.set(figwidth=8, dpi=100)
 return axs<define_variables>
classifier.compile(optimizer='adam',loss = 'binary_crossentropy',metrics=['accuracy'])
 classifier.fit(X_train,y_train,epochs=100,batch_size=64,validation_split=0.1,shuffle=True )
pca_features = [
"GarageArea",
"YearRemodAdd",
"TotalBsmtSF",
"GrLivArea",
]<load_pretrained>
result = classifier.predict_classes(X_test )
<feature_engineering><EOS> 
out = pd.DataFrame({"ImageId": i+1 , "Label": result[i]} for i in range(0, test_num_img))
out.to_csv('submission.csv', index=False )