From 45195943d71025954cf64ffd0bd4a6145048a809 Mon Sep 17 00:00:00 2001 From: mahehsma Date: Wed, 5 Jun 2024 13:12:53 +0200 Subject: [PATCH] added kmeans clustering --- Experiments.ipynb | 201 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 190 insertions(+), 11 deletions(-) diff --git a/Experiments.ipynb b/Experiments.ipynb index 8f7fe0e..87d8d45 100644 --- a/Experiments.ipynb +++ b/Experiments.ipynb @@ -17,7 +17,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 21, "id": "67503952-9074-4cdb-9d7e-d9142f7c319c", "metadata": {}, "outputs": [ @@ -216,7 +216,7 @@ "[5 rows x 28 columns]" ] }, - "execution_count": 14, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -271,7 +271,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 41, "id": "38eb4f87-ca3c-4ecf-a8ca-29422822d933", "metadata": {}, "outputs": [ @@ -282,7 +282,7 @@ "Training fold 0 for 20 epochs\n", "Train samples:\t267\n", "Test samples:\t30\n", - "Accuracy of fold 0: 0.8666666666666667\n", + "Accuracy of fold 0: 0.9\n", "Training fold 1 for 20 epochs\n", "Train samples:\t267\n", "Test samples:\t30\n", @@ -294,11 +294,11 @@ "Training fold 3 for 20 epochs\n", "Train samples:\t267\n", "Test samples:\t30\n", - "Accuracy of fold 3: 0.9333333333333333\n", + "Accuracy of fold 3: 0.9\n", "Training fold 4 for 20 epochs\n", "Train samples:\t267\n", "Test samples:\t30\n", - "Accuracy of fold 4: 0.8666666666666667\n", + "Accuracy of fold 4: 0.9\n", "Training fold 5 for 20 epochs\n", "Train samples:\t267\n", "Test samples:\t30\n", @@ -306,20 +306,20 @@ "Training fold 6 for 20 epochs\n", "Train samples:\t267\n", "Test samples:\t30\n", - "Accuracy of fold 6: 0.8666666666666667\n", + "Accuracy of fold 6: 0.7666666666666667\n", "Training fold 7 for 20 epochs\n", "Train samples:\t268\n", "Test samples:\t29\n", - "Accuracy of fold 7: 0.896551724137931\n", + "Accuracy of fold 7: 0.8275862068965517\n", "Training fold 8 for 20 epochs\n", "Train samples:\t268\n", "Test samples:\t29\n", - "Accuracy of fold 8: 0.7931034482758621\n", + "Accuracy of fold 8: 0.7586206896551724\n", "Training fold 9 for 20 epochs\n", "Train samples:\t268\n", "Test samples:\t29\n", - "Accuracy of fold 9: 0.7931034482758621\n", - "Avg accuracy 0.8582758620689654\n" + "Accuracy of fold 9: 0.7586206896551724\n", + "Avg accuracy 0.837816091954023\n" ] } ], @@ -371,6 +371,185 @@ "avg_accuracy = sum(accuracies) / len(accuracies)\n", "print(f'Avg accuracy {avg_accuracy}')" ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "95215693-47c9-4202-92f5-efbc65bc32c9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Training fold 0 for 20 epochs\n", + "Train samples:\t237\n", + "Test samples:\t60\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\maxwi\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n", + " warnings.warn(\n", + "C:\\Users\\maxwi\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1382: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n", + " warnings.warn(\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 1 1 0 0 0 0 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 1 0 1 0 1 0 0 1\n", + " 1 1 1 1 1 0 0 0 1 0 1 0 0 0 1 0 0 1 1 1 1 0 1]\n", + "Accuracy of fold 0: 0.5833333333333334\n", + "Training fold 1 for 20 epochs\n", + "Train samples:\t237\n", + "Test samples:\t60\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\maxwi\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n", + " warnings.warn(\n", + "C:\\Users\\maxwi\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1382: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n", + " warnings.warn(\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 0 1 0 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 1 1\n", + " 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1]\n", + "Accuracy of fold 1: 0.5\n", + "Training fold 2 for 20 epochs\n", + "Train samples:\t238\n", + "Test samples:\t59\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\maxwi\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n", + " warnings.warn(\n", + "C:\\Users\\maxwi\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1382: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n", + " warnings.warn(\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 0 0 0 1 0 0 1 1 0 0 1 0 1 1 0 0 0 1 1 0 1 0 0 1 0 1 1 1 0 1 1 0 0 0 0 0\n", + " 0 1 1 0 1 1 1 0 1 0 1 0 0 0 1 0 0 0 0 1 1 0]\n", + "Accuracy of fold 2: 0.559322033898305\n", + "Training fold 3 for 20 epochs\n", + "Train samples:\t238\n", + "Test samples:\t59\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\maxwi\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n", + " warnings.warn(\n", + "C:\\Users\\maxwi\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1382: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n", + " warnings.warn(\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 1 0 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 0 1 0 1\n", + " 1 1 1 1 0 0 1 1 1 0 0 1 0 1 1 1 0 0 0 1 1 1]\n", + "Accuracy of fold 3: 0.576271186440678\n", + "Training fold 4 for 20 epochs\n", + "Train samples:\t238\n", + "Test samples:\t59\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\maxwi\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n", + " warnings.warn(\n", + "C:\\Users\\maxwi\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1382: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n", + " warnings.warn(\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 1 1 1 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 0 1 0 1 1 0 1 1 1\n", + " 1 0 1 0 1 0 0 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1]\n", + "Accuracy of fold 4: 0.5254237288135594\n", + "Avg accuracy 0.5488700564971751\n" + ] + } + ], + "source": [ + "from sklearn.cluster import KMeans\n", + "\n", + "# number of components extracted from the pca\n", + "n_features = 10\n", + "\n", + "k_folds = 5\n", + "\n", + "# used to split the dataset into k folds\n", + "kf = KFold(n_splits=k_folds)\n", + "\n", + "accuracies = []\n", + "for i, (train_idx, test_idx) in enumerate(kf.split(X[numeric_columns])):\n", + " print(f'Training fold {i} for {epochs} epochs')\n", + "\n", + " # extract train and test data from the cleaned dataset\n", + " X_train, X_test = X.iloc[train_idx], X.iloc[test_idx]\n", + " y_train, y_test = y[train_idx], y[test_idx]\n", + "\n", + " print(f'Train samples:\\t{len(X_train)}')\n", + " print(f'Test samples:\\t{len(X_test)}')\n", + "\n", + " # do pca based on the train data of the given fold to extract 'n_features'\n", + " #pca = decomposition.PCA(n_components=n_features)\n", + " #pca.fit(X_train)\n", + " #X_train = pca.transform(X_train)\n", + "\n", + " model = KMeans(n_clusters=2)\n", + " model.fit(X_train)\n", + "\n", + " #X_test = pca.transform(X_test)\n", + " y_pred = model.predict(X_test)\n", + " print(y_pred)\n", + " \n", + "\n", + " # calculate the accuracy of the train data for the current fold\n", + " accuracy1 = sum(y_pred == y_test)[0] / len(y_pred)\n", + " accuracy2 = sum(y_pred != y_test)[0] / len(y_pred)\n", + " accuracy = max(accuracy1, accuracy2)\n", + " accuracies.append(accuracy)\n", + " print(f'Accuracy of fold {i}: {accuracy}')\n", + "\n", + "# calculate the average accuracy over all folds\n", + "avg_accuracy = sum(accuracies) / len(accuracies)\n", + "print(f'Avg accuracy {avg_accuracy}')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "880302e4-82c1-47b9-9fe3-cb3567511639", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {