added complementary option to pw3
This commit is contained in:
@@ -61,7 +61,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 9,
|
||||||
"id": "14aba0f7",
|
"id": "14aba0f7",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -148,12 +148,108 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 10,
|
||||||
"id": "4de72736",
|
"id": "4de72736",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Training data shape: (10000, 28, 28)\n",
|
||||||
|
"Training labels shape: (10000,)\n",
|
||||||
|
"Test data shape: (10000, 28, 28)\n",
|
||||||
|
"Test labels shape: (10000,)\n",
|
||||||
|
"(10000, 784) (10000, 784)\n",
|
||||||
|
"Accuracy score : 0.5711\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"pass"
|
"import numpy as np\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"import os\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"# This is a method to read the MNIST dataset from a ROOT directory\n",
|
||||||
|
"def load_MNIST(ROOT):\n",
|
||||||
|
" '''load all of mnist\n",
|
||||||
|
" training set first'''\n",
|
||||||
|
" Xtr = []\n",
|
||||||
|
" train = pd.read_csv(os.path.join(ROOT, 'mnist_train.csv'))\n",
|
||||||
|
" X = np.array(train.drop('label', axis=1))\n",
|
||||||
|
" Ytr = np.array(train['label'])\n",
|
||||||
|
" # With this for-loop we give the data a shape of the acctual image (28x28)\n",
|
||||||
|
" # instead of the shape in file (1x784)\n",
|
||||||
|
" for row in X:\n",
|
||||||
|
" Xtr.append(row.reshape(28,28))\n",
|
||||||
|
" # load test set second\n",
|
||||||
|
" Xte = []\n",
|
||||||
|
" test = pd.read_csv(os.path.join(ROOT, 'mnist_test.csv'))\n",
|
||||||
|
" X = np.array(test.drop('label', axis=1))\n",
|
||||||
|
" Yte = np.array(test['label'])\n",
|
||||||
|
" # same reshaping\n",
|
||||||
|
" for row in X:\n",
|
||||||
|
" Xte.append(row.reshape(28,28))\n",
|
||||||
|
" \n",
|
||||||
|
" return np.array(Xtr), np.array(Ytr), np.array(Xte), np.array(Yte)\n",
|
||||||
|
"\n",
|
||||||
|
"# Load the raw MNIST data.\n",
|
||||||
|
"mnist_dir = '' \n",
|
||||||
|
"X_train, y_train, X_test, y_test = load_MNIST(mnist_dir)\n",
|
||||||
|
"\n",
|
||||||
|
"# As a sanity check, we print out the size of the training and test data.\n",
|
||||||
|
"print('Training data shape: ', X_train.shape)\n",
|
||||||
|
"print('Training labels shape: ', y_train.shape)\n",
|
||||||
|
"print('Test data shape: ', X_test.shape)\n",
|
||||||
|
"print('Test labels shape: ', y_test.shape)\n",
|
||||||
|
"X_train = np.reshape(X_train, (X_train.shape[0], -1)) \n",
|
||||||
|
"X_test = np.reshape(X_test, (X_test.shape[0], -1)) \n",
|
||||||
|
"\n",
|
||||||
|
"print(X_train.shape, X_test.shape)\n",
|
||||||
|
"def predict_gaussian(X_test, mu, sigma2, priors):\n",
|
||||||
|
" n_samples = X_test.shape[0]\n",
|
||||||
|
" y_pred = np.zeros(n_samples)\n",
|
||||||
|
"\n",
|
||||||
|
" K, n_pixels = mu.shape\n",
|
||||||
|
" \n",
|
||||||
|
" for idx in range(n_samples):\n",
|
||||||
|
" x = X_test[idx]\n",
|
||||||
|
" proba_classes = np.zeros(K)\n",
|
||||||
|
"\n",
|
||||||
|
" for c in range(K):\n",
|
||||||
|
" log_likelihood = -0.5 * np.log(2 * np.pi * sigma2[c]) - ((x - mu[c])**2) / (2 * sigma2[c])\n",
|
||||||
|
" proba_classes[c] = np.log(priors[c]) + np.sum(log_likelihood)\n",
|
||||||
|
" \n",
|
||||||
|
" y_pred[idx] = np.argmax(proba_classes)\n",
|
||||||
|
"\n",
|
||||||
|
" return y_pred\n",
|
||||||
|
"classes = np.unique(y_train)\n",
|
||||||
|
"priors = np.array([np.mean(y_train == c) for c in classes])\n",
|
||||||
|
"\n",
|
||||||
|
"n_pixels = X_train.shape[1]\n",
|
||||||
|
"\n",
|
||||||
|
"mu = np.zeros((len(classes), n_pixels))\n",
|
||||||
|
"sigma2 = np.zeros((len(classes), n_pixels))\n",
|
||||||
|
"\n",
|
||||||
|
"for c in classes:\n",
|
||||||
|
" X_c = X_train[y_train == c]\n",
|
||||||
|
" mu[c, :] = X_c.mean(axis=0)\n",
|
||||||
|
" sigma2[c, :] = X_c.var(axis=0) + 1e-5\n",
|
||||||
|
" \n",
|
||||||
|
"y_pred = predict_gaussian(X_test, mu, sigma2, priors)\n",
|
||||||
|
"accuracy = np.mean(y_pred == y_test)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"Accuracy score :\", accuracy)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "07cb7aee",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The .57 accuracy observed here might prove that the method is not the right one for this type of problems, because if each pixel is a feature, the number of dimensions become way to big. This might also be caused by the fact that pixels are not uncorrelated."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -199,7 +295,7 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 3 (ipykernel)",
|
"display_name": ".venv",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
@@ -213,7 +309,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.9.6"
|
"version": "3.12.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
Reference in New Issue
Block a user