You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

860 lines
110 KiB
Plaintext

6 months ago
{
"cells": [
{
"cell_type": "markdown",
"id": "31637f6b",
"metadata": {},
"source": [
"## 1.手写体识别"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "6bab32d4",
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.datasets import load_digits"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "80edf133",
"metadata": {},
"outputs": [],
"source": [
"digits = load_digits()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8ea44c1f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['DESCR', 'data', 'feature_names', 'frame', 'images', 'target', 'target_names']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir(digits)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3d36f2ec",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\".. _digits_dataset:\\n\\nOptical recognition of handwritten digits dataset\\n--------------------------------------------------\\n\\n**Data Set Characteristics:**\\n\\n :Number of Instances: 1797\\n :Number of Attributes: 64\\n :Attribute Information: 8x8 image of integer pixels in the range 0..16.\\n :Missing Attribute Values: None\\n :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr)\\n :Date: July; 1998\\n\\nThis is a copy of the test set of the UCI ML hand-written digits datasets\\nhttps://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits\\n\\nThe data set contains images of hand-written digits: 10 classes where\\neach class refers to a digit.\\n\\nPreprocessing programs made available by NIST were used to extract\\nnormalized bitmaps of handwritten digits from a preprinted form. From a\\ntotal of 43 people, 30 contributed to the training set and different 13\\nto the test set. 32x32 bitmaps are divided into nonoverlapping blocks of\\n4x4 and the number of on pixels are counted in each block. This generates\\nan input matrix of 8x8 where each element is an integer in the range\\n0..16. This reduces dimensionality and gives invariance to small\\ndistortions.\\n\\nFor info on NIST preprocessing routines, see M. D. Garris, J. L. Blue, G.\\nT. Candela, D. L. Dimmick, J. Geist, P. J. Grother, S. A. Janet, and C.\\nL. Wilson, NIST Form-Based Handprint Recognition System, NISTIR 5469,\\n1994.\\n\\n.. topic:: References\\n\\n - C. Kaynak (1995) Methods of Combining Multiple Classifiers and Their\\n Applications to Handwritten Digit Recognition, MSc Thesis, Institute of\\n Graduate Studies in Science and Engineering, Bogazici University.\\n - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika.\\n - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin.\\n Linear dimensionalityreduction using relevance weighted LDA. School of\\n Electrical and Electronic Engineering Nanyang Technological University.\\n 2005.\\n - Claudio Gentile. A New Approximate Maximal Margin Classification\\n Algorithm. NIPS. 2000.\\n\""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"digits.DESCR#discription"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "938ce0d2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 0., 5., 13., 9., 1., 0., 0., 0., 0., 13., 15., 10.,\n",
" 15., 5., 0., 0., 3., 15., 2., 0., 11., 8., 0., 0., 4.,\n",
" 12., 0., 0., 8., 8., 0., 0., 5., 8., 0., 0., 9., 8.,\n",
" 0., 0., 4., 11., 0., 1., 12., 7., 0., 0., 2., 14., 5.,\n",
" 10., 12., 0., 0., 0., 0., 6., 13., 10., 0., 0., 0.])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"digits.data[0]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "14cac6af",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([[ 0., 0., 5., 13., 9., 1., 0., 0.],\n",
" [ 0., 0., 13., 15., 10., 15., 5., 0.],\n",
" [ 0., 3., 15., 2., 0., 11., 8., 0.],\n",
" [ 0., 4., 12., 0., 0., 8., 8., 0.],\n",
" [ 0., 5., 8., 0., 0., 9., 8., 0.],\n",
" [ 0., 4., 11., 0., 1., 12., 7., 0.],\n",
" [ 0., 2., 14., 5., 10., 12., 0., 0.],\n",
" [ 0., 0., 6., 13., 10., 0., 0., 0.]]),\n",
" 0)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"digits.images[0], digits.target[0]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "85db1c61",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1797"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(digits.data)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f3c67f60",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x2da27e566c8>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"<Figure size 640x480 with 0 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAZoAAAGkCAYAAAAIduO+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAYoElEQVR4nO3df2yUhR3H8c/R2oNpexak0I7jpygCtoMWCKvOHyCkQSL7oxKCWYXNRXJMsDFx/WewLOPqH1twGyk/xoqJYyDLis4MusKkZJkdpaQJaIJgmRwidG5wV7rkML3bX7utQ9o+R788PNf3K3midz7HfUIqb+5He75kMpkUAABGhrk9AACQ2QgNAMAUoQEAmCI0AABThAYAYIrQAABMERoAgClCAwAwRWgAAKYIDQDAVMaEZsuWLZo4caKGDx+uefPm6dixY25P6tfRo0e1dOlSFRUVyefzaf/+/W5PGpBwOKw5c+YoNzdXBQUFWrZsmU6fPu32rAGpq6tTcXGx8vLylJeXp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OYP3Xm9/vl9/ut7wYAcIfi+2gAAKYcP6K5du2azp49m7p87tw5tbe3a+TIkRo/fvygjgMAeJ/j0Bw/flxPPPFE6nJ1dbUkqaqqSrt27Rq0YQCAzOA4NI8//riSyaTFFgBABuI1GgCAKUIDADBFaAAApggNAMAUoQEAmCI0AABThAYAYIrQAABMERoAgClCAwAwRWgAAKYIDQDAFKEBAJgiNAAAU4QGAGCK0AA
"text/plain": [
"<Figure size 480x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.gray()#如果不加gray即使是灰度图plt默认三个通道都相同的值所以显示有颜色。\n",
"plt.matshow(digits.images[0])"
]
},
{
"cell_type": "markdown",
"id": "501e6e01",
"metadata": {},
"source": [
"显示多个。"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "5494019a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Figure size 640x480 with 0 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAfEAAAGzCAYAAAA/oi4aAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAtIElEQVR4nO3df1BV953/8RegXn/xwwQiMvKV2NS62giuVmpHG7Nl19U00WRjLHFnlbWxOjHGUXdS224gPxrNdteY3VrU3RjSbaw/kl3t5gezwRlsE1EbI5ltTSMSqCAB1AgXNKLC+f7RCYYgyPlw7zn3I8/HzJ2JF9738845L3l7ufeeT5TjOI4AAIB1ov1uAAAAmGGIAwBgKYY4AACWYogDAGAphjgAAJZiiAMAYCmGOAAAlmKIAwBgKYY4AACWYogDAGAphngvtbS06LHHHlNKSooGDRqkzMxMvfXWW363BXSrublZubm5+uu//mvddNNNioqKUkFBgd9tAdf129/+VsuXL9f48eM1ZMgQ/b//9//0wAMP6Pjx43635guG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Yiu+iOq9fEq6urlZ2drbNnzyopKUnTpk3TwYMHlZSUFK7+gJAgu7AV2UV3XA3xHTt2hKuP6/roo49c15juEDZv3jxPanrj2Wef9XQ92/mZXaA3/M5uQUGB65oZM2YYrZWenu66Zs+ePUZr7d2716juxRdf9GytnuDa6QAAWIohDgCApRjiAABYiiEOAIClGOIAAFiKIQ4AgKUY4gAAWIohDgCApRjiAABYiiEOAIClGOIAAFiKIQ4AgKVcbYDiJ5MNUL7//e8
"text/plain": [
"<Figure size 640x480 with 9 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.gray()\n",
"fig = plt.figure()\n",
"for i, (images, labels) in enumerate(zip(digits.images[:9], digits.target[:9])):\n",
" fig.add_subplot(3,3,i+1)\n",
" plt.title(str(labels))\n",
" plt.imshow(images)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "e00abaa6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"digits.target_names"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "f2e5c6e0",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "e9af25e7",
"metadata": {},
"outputs": [],
"source": [
"x_train, x_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.2, random_state=10)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a2c62100",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1797, 1437, 360)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(digits.data), len(x_train), len(x_test)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "558522cc",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LogisticRegression"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "cc3d8214",
"metadata": {},
"outputs": [],
"source": [
"lr = LogisticRegression()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "8d3dfb7c",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\envs\\stark-lin\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py:818: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
"STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
"\n",
"Increase the number of iterations (max_iter) or scale the data as shown in:\n",
" https://scikit-learn.org/stable/modules/preprocessing.html\n",
"Please also refer to the documentation for alternative solver options:\n",
" https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
" extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG,\n"
]
},
{
"data": {
"text/plain": [
"LogisticRegression()"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.fit(x_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "ffce7229",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.95"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.score(x_test, y_test)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "bb88e9c3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x2da2b608f88>"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAZoAAAGkCAYAAAAIduO+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAYjklEQVR4nO3dcUyUh/3H8c8J43QVrmpFYSLY1taqhVlR47CrVqsh1uj+cMbYDHVbUnNOLWnS8M9wWea5P7bYbYaqc2jSMe2WQbtmytQVzFK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AmCI0AABThAYAYIrQAABMERoAgClCAwAw5To0J0+e1IoVK5Sbm6tAIKC6ujqDWQCAVOE6NF1dXSoqKtLu3bst9gAAUky62weUlpaqtLTUYgsAIAW5Do1b8Xhc8Xi853YsFrN+SgDAEGL+ZoBIJKJQKNRz5OXlWT8lAGAIMQ9NRUWFotFoz9HW1mb9lACAIcT8pbNgMKhgMGj9NACAIYrvowEAmHJ9RdPZ2alLly713L58+bJaW1s1duxYTZ48eUDHAQD8z3Vozpw5o0WLFvXcLi8vlySVlZXpwIEDAzYMAJAaXIdm4cKFchzHYgsAIAXxNRoAgClCAwAwRWgAAKYIDQDAFKEBAJgiNAAAU4QGAGCK0AAATBEaAIApQgMAMEVoAACmCA0AwBShAQCYIjQAAFOEBgBgitAAAEy5/uAz+NPKlSu9npCUyspKryckbdWqVV5PSEpBQYHXE5J25coVryfgLriiAQCYIjQ
"text/plain": [
"<Figure size 480x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.matshow(digits.images[100])"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "829fe818",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"digits.target[100]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "61422431",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([4])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.predict([digits.data[100]])"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "43d3dd43",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.predict(digits.data[:10])"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "38511984",
"metadata": {},
"outputs": [],
"source": [
"y_pred = lr.predict(x_test)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "d4a66067",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import confusion_matrix"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "319a768d",
"metadata": {},
"outputs": [],
"source": [
"cm = confusion_matrix(y_test, y_pred)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "7f878c1c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text(95.72222222222221, 0.5, 'True')"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "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9dRTT7k8bt68uf7880+lp6cX5Evi1tdff62oqCg99NBD+ef8/f01YMAAnTlzRmvXrnV5/QMPPODSqTm/3Ov3338vlHgAAP9DwQDA5505c0YhISFun9+/f7+KFSumKlWquJyvVq3aPxr31Vdf1alTp1S9enXdeOONeu6557Rt27Yrfv+wYcO0YsUKLVy4UN27d1daWpqKFfvfr93jx4/r1KlT+uCDDxQeHu5y9OrVS9Jf6/qlv/Y91KhR45Kbvffv369y5cpd8LU6vwxo//79+f9b0K9XTEyMy+Pzf6z/97//veTX4Ert379f119/vcvXR7owdm/FAwD4H/YwAPBpBw8eVFpa2j/+4/9qtGjRQnv27NFXX32lb775RtOmTdP48eM1depUPf7445d9/4033qi2bdtKku655x6dPXtWffr0UbNmzVSxYkU5HA5J0iOPPKIePXpc9DOcLydrJndXljJMujK3r8UDAEUZHQYAPu2TTz6RJLVv397taypVqiSHw6G9e/e6nE9JSbmiMS511+gyZcqoV69e+uyzz/THH38oLi7O5SpCBTF69GhlZmbq9ddflySFh4crJCREeXl5atu27UWPiIgISVJsbKx27typnJwct59fqVIlHT58+ILlW7/99lv+8+f/9598va5UQe7GXalSJe3evTu/iDrv77EDALyPggGAz1q1apVGjRqlKlWqqFu3bm5fd76YmDx5ssv5d99994rGCQoK0qlTpy44/+eff7o8Dg4OVrVq1ZSVlXVFn/t3sbGx6tKli2bOnKmjR4+qePHi6tKli7744gv9/PPPF7z++PHj+f9/ly5ddOLECb333nsXvO78v6rfcccdysvLu+A148ePl81mU4cOHST986/XlQoKCpKki35t/+6OO+7Q0aNHNW/evPxzubm5evfddxUcHKyWLVsWamwAgCvHkiQAPmHp0qX67bfflJubq2PHjmnVqlVasWKFKlWqpEWLFl3yrs4NGjRQly5dNGHCBP3555/5lwndtWuXpMv/S3eDBg00ZcoUvfbaa6pWrZoiIiLUpk0b1a5dW61atVKDBg1UpkwZbdq0SQsWLFC/fv2uOs/nnntOn3/+uSZMmKDRo0dr9OjRWr16tZo0aaI+ffqodu3aOnnypDZv3qxvv/1WJ0+elCR1795dH3/8sYYMGaIff/xRzZs3V0ZGhr799ls988wz6tSpkzp27KjWrVvrpZde0r59+1S3bl198803+uqrrzRo0CDFxsYWytfrStWrV0/FixfXmDFjlJaWJrvdrjZt2uR3TZw98cQTev/999WzZ08lJyercuXKWrBggf7v//5PEyZMuOQeFgCAh5l5iSYAOH+Z0/NHQECAERUVZdx2223GxIkT8y+n6ezvl1U1DMPIyMgw+vbta5QpU8YIDg427rnnHmPnzp2GJGP06NEXjOd8WdWjR48ad955pxESEmJIyr/c52uvvWY0btzYKF26tBEYGGjUrFnTeP31143s7OxL5nT+sqrz58+/6POtWrUySpUqZZw6dcowDMM4duyY0bdvX6NixYqGv7+/ERUVZdx6663GBx984PK+s2fPGi+99JJRpUqV/Nfdd999xp49e/Jfc/r0aWPw4MFGuXLlDH9/f+P66683xo0bl3/p1YJ+vc5/rY8fP+7y/ot9Hf9
"text/plain": [
"<Figure size 1000x800 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import seaborn as sn\n",
"plt.figure(figsize=(10,8))\n",
"plt.title(\"Digits Recognition\")\n",
"sn.heatmap(cm, annot=True)\n",
"plt.xlabel(\"Predicted\")\n",
"plt.ylabel(\"True\")"
]
},
{
"cell_type": "markdown",
"id": "34c5a18b",
"metadata": {},
"source": [
"## 2.鸢尾花识别"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "07a16459",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.datasets import load_iris"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "0cb6b5e7",
"metadata": {},
"outputs": [],
"source": [
"iris = load_iris()"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "93eaff25",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['DESCR',\n",
" 'data',\n",
" 'data_module',\n",
" 'feature_names',\n",
" 'filename',\n",
" 'frame',\n",
" 'target',\n",
" 'target_names']"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir(iris)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "b8e7e686",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"150"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(iris.data)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "a4c34091",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(150, 4)"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris.data.shape"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "1f7d8b6b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
" 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
" 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris.target"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "4e62908b",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "7cd0d160",
"metadata": {},
"outputs": [],
"source": [
"x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=10)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "5f19db88",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(150, 120, 30)"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(iris.data), len(x_train), len(x_test)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "3e3a9bad",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LogisticRegression"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "6240628c",
"metadata": {},
"outputs": [],
"source": [
"lr = LogisticRegression()"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "a0e21d08",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LogisticRegression()"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.fit(x_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "fa87fe2a",
"metadata": {},
"outputs": [],
"source": [
"y_pred = lr.predict(x_test)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "6ba931a9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([1, 2, 0, 1, 0, 1, 1, 1, 0, 1, 1, 2, 1, 0, 0, 2, 1, 0, 0, 0, 2, 2,\n",
" 2, 0, 1, 0, 1, 1, 1, 2]),\n",
" array([1, 2, 0, 1, 0, 1, 1, 1, 0, 1, 1, 2, 1, 0, 0, 2, 1, 0, 0, 0, 2, 2,\n",
" 2, 0, 1, 0, 1, 1, 1, 2]))"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_pred, y_test"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "60a4514f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 40,
"id": "139465f8",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import confusion_matrix"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "dc96b868",
"metadata": {},
"outputs": [],
"source": [
"cm = confusion_matrix(y_pred, y_test)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "d2f239a8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text(95.72222222222221, 0.5, 'True')"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAwwAAAK9CAYAAACJnusfAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA88klEQVR4nO3de5xVdbk/8GdzG0aEQeQmioJyVAwCFTU1uSiJWipZmh0vgGmZSAKSSqWAlKOleRfMG1pa2knJvOQxFJW8ImFpiuK1VG4hcAAdLnv9/ujHtJawlIGBPYPvd6/16jVrr73Ww+61Gx4+z3etQpIkSQAAAKxDg1IXAAAA1F0aBgAAIJeGAQAAyKVhAAAAcmkYAACAXBoGAAAgl4YBAADIpWEAAAByaRgAAIBcGgZgizV48ODo1KlTqcuot2ry+Y0dOzYKhcKmLQiAktAwAPXGpEmTolAoxPTp0zf7tQuFQmZr0aJF9OnTJ+6///7NXkupLF++PMaOHRtTp04tdSkAbEaFJEmSUhcBsD4mTZoUQ4YMieeeey569er1qcevXLkyisVilJWVbfS1C4VCfOlLX4qTTz45kiSJt99+OyZMmBDvv/9+PPjggzFgwICNvkZd8/HPb8GCBdGmTZsYM2ZMjB07NnPsqlWrYtWqVdG0adMSVArAptSo1AUA1LZly5ZFs2bNonHjxrV63l133TVOPPHE6p+/9rWvxR577BFXXnnlFtkw1OTza9SoUTRq5FcKwJbISBJQrw0ePDi23nrreP311+OII46I5s2bxwknnFD92sdn8H/zm9/E3nvvHc2bN48WLVpE9+7d48orr9yga3ft2jVat24dr7/+emZ/VVVVjBkzJrp06RJlZWXRsWPHOOecc6Kqqmqtc/zqV7+KfffdN7baaqvYZpttonfv3vG///u/mWOuu+66+NznPhdlZWXRoUOHGDp0aCxatGitc1177bWx8847R3l5eey7777xxBNPRN++faNv377Vx0ydOjUKhULcdddd8ZOf/CR22GGHaNq0aRxyyCExe/bszPnSn99bb70Vbdq0iYiIcePGVY9mrUka1rWGYdWqVTF+/PjYZZddoqysLDp16hQ/+MEP1vocOnXqFF/5yldi2rRpse+++0bTpk1j5513jttuuy33swdg89EwAPXeqlWrYsCAAdG2bdu49NJL42tf+9o6j3v44Yfjm9/8ZmyzzTZxySWXxMUXXxx9+/aNP//5zxt03cWLF8cHH3wQ22yzTfW+YrEYRx11VFx66aVx5JFHxtVXXx0DBw6Myy+/PL7xjW9k3j9u3Lg46aSTonHjxnHhhRfGuHHjomPHjvHII49UHzN27NgYOnRodOjQIS677LL42te+Ftdff30ceuihsXLlyurjJkyYEGeeeWbssMMO8dOf/jQOOuigGDhwYPzzn/9cZ+0XX3xx3HPPPTFq1KgYPXp0PP3009WN1rq0adMmJkyYEBERX/3qV+OXv/xl/PKXv4xjjjkm9z2nnnpqXHDBBbHXXnvF5ZdfHn369InKyso4/vjj1zp29uzZ8fWvfz2+9KUvxWWXXRbbbLNNDB48OF566aXc8wOwmSQA9cQtt9ySRETy3HPPVe8bNGhQEhHJeeedt9bxgwYNSnbaaafqn88666ykRYsWyapVq2p87YhIvvWtbyXz589P5s2bl0yfPj057LDDkohIfvazn1Uf98tf/jJp0KBB8sQTT2TeP3HixCQikj//+c9JkiTJa6+9ljRo0CD56le/mqxevTpzbLFYTJIkSebNm5c0adIkOfTQQzPHXHPNNUlEJDfffHOSJElSVVWVbLvttsk+++yTrFy5svq4SZMmJRGR9OnTp3rfo48+mkRE0rVr16Sqqqp6/5VXXplERPK3v/2tet/HP7/58+cnEZGMGTNmrc9nzJgxSfpXysyZM5OISE499dTMcaNGjUoiInnkkUeq9+20005JRCSPP/549b558+YlZWVlydlnn73WtQDYvCQMwBbhu9/97qce07Jly1i2bFk8/PDDG3SNm266Kdq0aRNt27aNXr16xZQpU+Kcc86JkSNHVh/z29/+Nrp27Rq77757LFiwoHo7+OCDIyLi0UcfjYiIyZMnR7FYjAsuuCAaNMj+X/Ga0Z4//elPsWLFihg+fHjmmNNOOy1atGhRfYem6dOnx7/+9a847bTTMusITjjhhEz6kTZkyJBo0qRJ9c8HHXRQRES88cYbG/TZfNwDDzwQEZH5bCIizj777IiIte4utccee1TXEPHvRGO33XartXoA2HAaBqDea9SoUeywww6fetwZZ5wRu+66axx++OGxww47xCmnnBJ//OMf1/s6Rx99dDz88MNx//33V8/sL1++PPOX+ddeey1eeumlaNOmTWbbddddIyJi3rx5ERHx+uuvR4MGDWKPPfbIvd7bb78dERG77bZbZn+TJk1i5513rn59zX936dIlc1yjRo1yn6Ow4447Zn5e01h88MEHn/gZrK+33347GjRosFZN7du3j5YtW1bXnFfPmppqqx4ANpxbWgD1XllZ2Vr/Sr8ubdu2jZkzZ8ZDDz0UDz74YDz44INxyy23xMknnxy33nrrp75/hx12iP79+0dExBFHHBGtW7eOM888M/r161c9y18sFqN79+7x85//fJ3n6NixYw3+ZJtOw4YN17k/qeU7ba/vw9w2Vz0A1JyEAfhMadKkSRx55JFx3XXXxeuvvx7f+c534rbbblvrDkHr4zvf+U7ssssu8aMf/aj6L7a77LJLLFy4MA455JDo37//WtuatGCXXXaJYrEYf//733PPv9NOO0VExKxZszL7V6xYEW+++Wb162v+++N/hlWrVsVbb71V4z9Xnpo8yXmnnXaKYrEYr732Wmb/3LlzY9GiRdU1A1D3aRiAz4x//etfmZ8bNGgQn//85yMi1nnL00/TqFGjOPvss+Pll1+O3//+9xERcdxxx8W7774bN9xww1rHf/jhh7Fs2bKIiBg4cGA0aNAgLrzwwigWi5nj1jQf/fv3jyZNmsRVV12V+Zf2m266KRYvXhxf/vKXIyKiV69ese2228YNN9wQq1atqj7u9ttvr9WRnq222ioiYp23dP24I444IiIirrjiisz+NcnLmtoBqPuMJAGfGaeeemosXLgwDj744Nhhhx3i7bffjquvvjp69uwZXbt23aBzDh48OC644IK45JJLYuDAgXHSSSfFXXfdFaeffno8+uijceCBB8bq1avjlVdeibvuuiseeuih6NWrV3Tp0iV++MMfxvjx4+Oggw6KY445JsrKyuK5556LDh06RGVlZbRp0yZGjx4d48aNi8MOOyyOOuqomDVrVlx33XWxzz77VD9ErkmTJjF27NgYNmxYHHzwwXHcccfFW2+9FZMmTYpddtmlRsnAJykvL4899tgj7rzzzth1112jVatW0a1bt+jWrdtax/bo0SMGDRoUv/jFL2LRokXRp0+fePbZZ+PWW2+NgQMHRr9+/WqlJgA2PQ0D8Jlx4oknxi9+8Yu47rrrYtGiRdG+ffv4xje+EWPHjl2vNRDrUl5eHmeeeWaMHTs2pk6dGn379o3JkyfH5ZdfHrfddlvcc889sdVWW8XOO+8cZ511VvXi54iICy+8MDp37hxXX311/PCHP4ytttoqPv/5z8dJJ51UfczYsWOjTZs2cc0118SIESOiVatW8e1vfzsuuuiizJOYzzzzzEiSJC677LIYNWpU9OjRI+6999743ve+F02bNt3wD+1jbrzxxhg2bFiMGDEiVqxYEWPGjFlnw7Dm2J133jkmTZoU99xzT7Rv3z5Gjx4dY8aMqbV6ANj0CokVZQBbpGKxGG3atIljjjlmnSNSALA+rGEA2AJ89NFHa91R6LbbbouFCxdG3759S1MUAFsECQPAFmDq1KkxYsSIOPbYY2PbbbeNGTNmxE033RRdu3aN559/PvOQNgCoCWsYALYAnTp1io4dO8ZVV10VCxcujFatWsXJJ58cF198sWYBgI0iYQAAAHJZwwAAAOTSMAAAALk0DAAAQK4tctHz8iu+U+oSoF5qcc5
"text/plain": [
"<Figure size 1000x800 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import seaborn as sn\n",
"plt.figure(figsize=(10,8))\n",
"plt.title(\"Iris Recognition\")\n",
"sn.heatmap(cm, annot=True)\n",
"plt.xlabel(\"Predicted\")\n",
"plt.ylabel(\"True\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}