Kaggle competition: CSM/SEM6420 workshop
kmeans2 = KMeans(n_clusters = 2, random_state = 42, n_init = 500 ).fit_predict(np_pca_significant)
 kmeans3 = KMeans(n_clusters = 3, random_state = 42, n_init = 500 ).fit_predict(np_pca_significant)
 kmeans4 = KMeans(n_clusters = 4, random_state = 42, n_init = 500 ).fit_predict(np_pca_significant)
 kmeans5 = KMeans(n_clusters = 5, random_state = 42, n_init = 500 ).fit_predict(np_pca_significant)
 kmeans6 = KMeans(n_clusters = 6, random_state = 42, n_init = 500 ).fit_predict(np_pca_significant )<predict_on_test>
train = pd.read_csv('.. /input/csm6420-workshop/train.csv')
 test = pd.read_csv('.. /input/csm6420-workshop/test.csv',index_col=0)
 print(train.head())
 print(test.head())
dbscan_model = DBSCAN(eps = 0.7, min_samples = 20 ).fit(np_pca_significant)
 dbscan_predict = dbscan_model.fit_predict(np_pca_significant )<train_model>
X = train.drop('Class', axis=1)
 y = train['Class']
print(y )
class TransformerMissingData(BaseEstimator, TransformerMixin):
def __init__(self, missing_age_masters_strategy = 'mean', missing_age_non_masters_strategy = 'cluster'):
self.missing_age_masters_strategy = missing_age_masters_strategy
self.missing_age_non_masters_strategy = missing_age_non_masters_strategy
def fit(self, X, y = None):
return self
def transform(self, X, y = None):
is_not_master =(X['Title_Master.'] == 0)
 is_master =(X['Title_Master.'] == 1)
 imp_non_master = SimpleImputer(missing_values = np.nan,
strategy = self.missing_age_non_masters_strategy)
 imp_master = SimpleImputer(missing_values = np.nan,
strategy = self.missing_age_masters_strategy)
 imp_fare = SimpleImputer(missing_values = np.nan,
strategy = 'mean')
 X.loc[is_master, 'Age'] = imp_master.fit_transform(X.loc[is_master, ['Age']])
 X.loc[:, 'Fare'] = imp_fare.fit_transform(X.loc[:, ['Fare']])
 if self.missing_age_non_masters_strategy in ['mean', 'median']:
X.loc[is_not_master, 'Age'] = imp_non_master.fit_transform(X.loc[is_not_master, ['Age']])
 elif self.missing_age_non_masters_strategy == 'cluster':
X_without_age = X.drop(['Age', 'Survived'], axis = 1)
 print(X_without_age.isnull().sum())
 pca_fit_vec = PCA(n_components = 3 ).fit_transform(X_without_age)
 X['ClusterLabel'] = DBSCAN(eps = 0.7, min_samples = 20 ).fit_predict(pca_fit_vec)
 dict_means_per_cluster = dict()
for cluster_label in X['ClusterLabel'].tolist() :
dict_means_per_cluster[cluster_label] = X.loc[X['ClusterLabel'] == cluster_label, :]['Age'].mean()
age_list = X['Age'].tolist()
cluster_list = X['ClusterLabel'].tolist()
for i, curr_age in enumerate(age_list):
if np.isnan(curr_age):
age_list[i] = dict_means_per_cluster[cluster_label]
X['Age'] = age_list
return X<load_from_csv>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)
 model = GaussianNB()
y_pred = model.fit(X_train, y_train ).predict(X_test)
 print("Number of mislabeled points out of a total %d points : %d" %(X_test.shape[0],(y_test != y_pred ).sum()))
feature_engineering_pipeline = Pipeline([ 
('prepare', TransformerSignificantData()), 
('dummify', TransformerDummify()), 
('normalize', TransformerNormalize()), 
('missing', TransformerMissingData())
 ])
 df_all_original = pd.concat([pd.read_csv('/kaggle/input/train.csv'),
pd.read_csv('/kaggle/input/test.csv')], ignore_index = True)
 df_all_original.head()<categorify>
sample = pd.read_csv('.. /input/csm6420-workshop/sampleSubmission.csv')
 print(sample.head() )
df_all_processed = feature_engineering_pipeline.transform(df_all_original.copy())
 df_all_processed.head()<count_missing_values>
y_pred = model.predict(test.values)
 results = pd.DataFrame()
results["TestId"] = test.index.values
results["PredictedScore"]= y_pred
results.to_csv("submission.csv", index=False )