Python-100-Days/Day66-80/code/day03.ipynb

3386 lines
99 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"id": "6fc07f67-318b-4d79-8d4e-4eb8a2c61be2",
"metadata": {},
"source": [
"## NumPy进阶"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "a9d74703-47d5-44f4-8566-eb7d5476c792",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"plt.rcParams['font.sans-serif'].insert(0, 'SimHei')\n",
"plt.rcParams['axes.unicode_minus'] = False"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d139c565-6bf2-4bf6-9d66-d2755b29d1db",
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_format = 'svg'\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"id": "d41a57e5-6009-455b-aff9-4f96682423fc",
"metadata": {},
"source": [
"### NumPy中的函数\n",
"\n",
"#### 通用一元函数"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5f881886-8aca-40cb-a9f3-4514e28b8fe3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 2., 3., inf, nan, -inf, nan, 5.])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# inf - infinity\n",
"# nan - not a number\n",
"array1 = np.array([1, 2, 3, np.inf, np.nan, -np.inf, np.nan, 5])\n",
"array1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b6e891cc-035c-4e98-9406-1e78e3623e76",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('float64')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1.dtype"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "674995a2-e50a-45a6-b1ad-7a9f88331dd0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([False, False, False, False, True, False, True, False])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.isnan(array1)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "358641bc-510c-4f1b-9df7-5dd54de47978",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 2., 3., inf, -inf, 5.])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1[~np.isnan(array1)]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "20030d7d-822e-45c7-b962-3aaf706e133c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, True, True, False, False, False, False, True])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.isfinite(array1)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "357cc22a-7acf-46ee-9523-631134dc8eae",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1., 2., 3., 5.])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1[np.isfinite(array1)]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c38f23a4-7d72-4ce4-9bc4-9f8cd9433fa8",
"metadata": {},
"outputs": [],
"source": [
"x = np.linspace(0.5, 10, 72)\n",
"y1 = np.sin(x)\n",
"y2 = np.log2(x)\n",
"y3 = np.sqrt(x)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "08a272bc-8765-455b-b0d0-700872071cf4",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"477.8pt\" height=\"251.285pt\" viewBox=\"0 0 477.8 251.285\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n",
" <metadata>\n",
" <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
" <cc:Work>\n",
" <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
" <dc:date>2024-09-22T23:37:54.080072</dc:date>\n",
" <dc:format>image/svg+xml</dc:format>\n",
" <dc:creator>\n",
" <cc:Agent>\n",
" <dc:title>Matplotlib v3.9.2, https://matplotlib.org/</dc:title>\n",
" </cc:Agent>\n",
" </dc:creator>\n",
" </cc:Work>\n",
" </rdf:RDF>\n",
" </metadata>\n",
" <defs>\n",
" <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n",
" </defs>\n",
" <g id=\"figure_1\">\n",
" <g id=\"patch_1\">\n",
" <path d=\"M 0 251.285 \n",
"L 477.8 251.285 \n",
"L 477.8 0 \n",
"L 0 0 \n",
"z\n",
"\" style=\"fill: #ffffff\"/>\n",
" </g>\n",
" <g id=\"axes_1\">\n",
" <g id=\"patch_2\">\n",
" <path d=\"M 24.2 228.96 \n",
"L 470.6 228.96 \n",
"L 470.6 7.2 \n",
"L 24.2 7.2 \n",
"z\n",
"\" style=\"fill: #ffffff\"/>\n",
" </g>\n",
" <g id=\"matplotlib.axis_1\">\n",
" <g id=\"xtick_1\">\n",
" <g id=\"line2d_1\">\n",
" <defs>\n",
" <path id=\"mf09f3f5aaa\" d=\"M 0 0 \n",
"L 0 3.5 \n",
"\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </defs>\n",
" <g>\n",
" <use xlink:href=\"#mf09f3f5aaa\" x=\"108.567464\" y=\"228.96\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"text_1\">\n",
" <!-- 2 -->\n",
" <g transform=\"translate(106.067464 242.795937) scale(0.1 -0.1)\">\n",
" <defs>\n",
" <path id=\"SimHei-32\" d=\"M 2850 100 \n",
"L 300 100 \n",
"L 300 500 \n",
"Q 450 900 712 1237 \n",
"Q 975 1575 1475 2000 \n",
"Q 1850 2325 2012 2600 \n",
"Q 2175 2875 2175 3200 \n",
"Q 2175 3525 2037 3737 \n",
"Q 1900 3950 1600 3950 \n",
"Q 1350 3950 1162 3725 \n",
"Q 975 3500 975 2925 \n",
"L 400 2925 \n",
"Q 425 3650 737 4037 \n",
"Q 1050 4425 1625 4425 \n",
"Q 2175 4425 2475 4087 \n",
"Q 2775 3750 2775 3175 \n",
"Q 2775 2700 2500 2350 \n",
"Q 2225 2000 1825 1650 \n",
"Q 1375 1250 1200 1050 \n",
"Q 1025 850 875 575 \n",
"L 2850 575 \n",
"L 2850 100 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" </defs>\n",
" <use xlink:href=\"#SimHei-32\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <g id=\"xtick_2\">\n",
" <g id=\"line2d_2\">\n",
" <g>\n",
" <use xlink:href=\"#mf09f3f5aaa\" x=\"194.002871\" y=\"228.96\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"text_2\">\n",
" <!-- 4 -->\n",
" <g transform=\"translate(191.502871 242.795937) scale(0.1 -0.1)\">\n",
" <defs>\n",
" <path id=\"SimHei-34\" d=\"M 2975 1200 \n",
"L 2450 1200 \n",
"L 2450 100 \n",
"L 1875 100 \n",
"L 1875 1200 \n",
"L 200 1200 \n",
"L 200 1675 \n",
"L 1875 4425 \n",
"L 2450 4425 \n",
"L 2450 1675 \n",
"L 2975 1675 \n",
"L 2975 1200 \n",
"z\n",
"M 1875 1675 \n",
"L 1875 3525 \n",
"L 750 1675 \n",
"L 1875 1675 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" </defs>\n",
" <use xlink:href=\"#SimHei-34\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <g id=\"xtick_3\">\n",
" <g id=\"line2d_3\">\n",
" <g>\n",
" <use xlink:href=\"#mf09f3f5aaa\" x=\"279.438278\" y=\"228.96\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"text_3\">\n",
" <!-- 6 -->\n",
" <g transform=\"translate(276.938278 242.795937) scale(0.1 -0.1)\">\n",
" <defs>\n",
" <path id=\"SimHei-36\" d=\"M 2850 1550 \n",
"Q 2850 850 2550 450 \n",
"Q 2250 50 1650 50 \n",
"Q 1050 50 700 550 \n",
"Q 350 1050 350 2175 \n",
"Q 350 3200 712 3812 \n",
"Q 1075 4425 1750 4425 \n",
"Q 2225 4425 2512 4075 \n",
"Q 2800 3725 2800 3300 \n",
"L 2225 3300 \n",
"Q 2225 3550 2087 3750 \n",
"Q 1950 3950 1725 3950 \n",
"Q 1350 3950 1150 3562 \n",
"Q 950 3175 925 2375 \n",
"Q 1100 2700 1300 2825 \n",
"Q 1500 2950 1775 2950 \n",
"Q 2250 2950 2550 2575 \n",
"Q 2850 2200 2850 1550 \n",
"z\n",
"M 2250 1550 \n",
"Q 2250 2000 2100 2250 \n",
"Q 1950 2500 1675 2500 \n",
"Q 1350 2500 1162 2250 \n",
"Q 975 2000 975 1650 \n",
"Q 975 1100 1162 800 \n",
"Q 1350 500 1675 500 \n",
"Q 1900 500 2075 725 \n",
"Q 2250 950 2250 1550 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" </defs>\n",
" <use xlink:href=\"#SimHei-36\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <g id=\"xtick_4\">\n",
" <g id=\"line2d_4\">\n",
" <g>\n",
" <use xlink:href=\"#mf09f3f5aaa\" x=\"364.873684\" y=\"228.96\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"text_4\">\n",
" <!-- 8 -->\n",
" <g transform=\"translate(362.373684 242.795937) scale(0.1 -0.1)\">\n",
" <defs>\n",
" <path id=\"SimHei-38\" d=\"M 2875 1325 \n",
"Q 2875 700 2525 375 \n",
"Q 2175 50 1575 50 \n",
"Q 975 50 625 375 \n",
"Q 275 700 275 1325 \n",
"Q 275 1650 475 1912 \n",
"Q 675 2175 1025 2300 \n",
"Q 725 2425 562 2650 \n",
"Q 400 2875 400 3225 \n",
"Q 400 3775 750 4100 \n",
"Q 1100 4425 1575 4425 \n",
"Q 2050 4425 2400 4100 \n",
"Q 2750 3775 2750 3225 \n",
"Q 2750 2875 2587 2650 \n",
"Q 2425 2425 2125 2300 \n",
"Q 2475 2175 2675 1912 \n",
"Q 2875 1650 2875 1325 \n",
"z\n",
"M 2200 3225 \n",
"Q 2200 3625 2025 3800 \n",
"Q 1850 3975 1575 3975 \n",
"Q 1300 3975 1125 3800 \n",
"Q 950 3625 950 3225 \n",
"Q 950 2825 1137 2662 \n",
"Q 1325 2500 1575 2500 \n",
"Q 1825 2500 2012 2662 \n",
"Q 2200 2825 2200 3225 \n",
"z\n",
"M 2300 1325 \n",
"Q 2300 1675 2112 1875 \n",
"Q 1925 2075 1575 2075 \n",
"Q 1225 2075 1037 1875 \n",
"Q 850 1675 850 1325 \n",
"Q 850 925 1050 712 \n",
"Q 1250 500 1575 500 \n",
"Q 1900 500 2100 712 \n",
"Q 2300 925 2300 1325 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" </defs>\n",
" <use xlink:href=\"#SimHei-38\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <g id=\"xtick_5\">\n",
" <g id=\"line2d_5\">\n",
" <g>\n",
" <use xlink:href=\"#mf09f3f5aaa\" x=\"450.309091\" y=\"228.96\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"text_5\">\n",
" <!-- 10 -->\n",
" <g transform=\"translate(445.309091 242.795937) scale(0.1 -0.1)\">\n",
" <defs>\n",
" <path id=\"SimHei-31\" d=\"M 1950 100 \n",
"L 1375 100 \n",
"L 1375 3425 \n",
"L 625 3425 \n",
"L 625 3725 \n",
"Q 1075 3725 1325 3900 \n",
"Q 1575 4075 1650 4425 \n",
"L 1950 4425 \n",
"L 1950 100 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" <path id=\"SimHei-30\" d=\"M 2975 2250 \n",
"Q 2975 1350 2650 700 \n",
"Q 2325 50 1600 50 \n",
"Q 875 50 537 700 \n",
"Q 200 1350 200 2250 \n",
"Q 200 3150 537 3787 \n",
"Q 875 4425 1600 4425 \n",
"Q 2325 4425 2650 3787 \n",
"Q 2975 3150 2975 2250 \n",
"z\n",
"M 2375 2250 \n",
"Q 2375 3050 2187 3500 \n",
"Q 2000 3950 1600 3950 \n",
"Q 1200 3950 1000 3500 \n",
"Q 800 3050 800 2250 \n",
"Q 800 1450 1000 987 \n",
"Q 1200 525 1600 525 \n",
"Q 2000 525 2187 987 \n",
"Q 2375 1450 2375 2250 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" </defs>\n",
" <use xlink:href=\"#SimHei-31\"/>\n",
" <use xlink:href=\"#SimHei-30\" x=\"50\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <g id=\"matplotlib.axis_2\">\n",
" <g id=\"ytick_1\">\n",
" <g id=\"line2d_6\">\n",
" <defs>\n",
" <path id=\"mb5a8a9618e\" d=\"M 0 0 \n",
"L -3.5 0 \n",
"\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </defs>\n",
" <g>\n",
" <use xlink:href=\"#mb5a8a9618e\" x=\"24.2\" y=\"218.88\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"text_6\">\n",
" <!-- -1 -->\n",
" <g transform=\"translate(7.2 222.297969) scale(0.1 -0.1)\">\n",
" <defs>\n",
" <path id=\"SimHei-2d\" d=\"M 3000 2100 \n",
"L 150 2100 \n",
"L 150 2500 \n",
"L 3000 2500 \n",
"L 3000 2100 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" </defs>\n",
" <use xlink:href=\"#SimHei-2d\"/>\n",
" <use xlink:href=\"#SimHei-31\" x=\"50\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <g id=\"ytick_2\">\n",
" <g id=\"line2d_7\">\n",
" <g>\n",
" <use xlink:href=\"#mb5a8a9618e\" x=\"24.2\" y=\"172.234152\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"text_7\">\n",
" <!-- 0 -->\n",
" <g transform=\"translate(12.2 175.652121) scale(0.1 -0.1)\">\n",
" <use xlink:href=\"#SimHei-30\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <g id=\"ytick_3\">\n",
" <g id=\"line2d_8\">\n",
" <g>\n",
" <use xlink:href=\"#mb5a8a9618e\" x=\"24.2\" y=\"125.588304\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"text_8\">\n",
" <!-- 1 -->\n",
" <g transform=\"translate(12.2 129.006273) scale(0.1 -0.1)\">\n",
" <use xlink:href=\"#SimHei-31\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <g id=\"ytick_4\">\n",
" <g id=\"line2d_9\">\n",
" <g>\n",
" <use xlink:href=\"#mb5a8a9618e\" x=\"24.2\" y=\"78.942457\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"text_9\">\n",
" <!-- 2 -->\n",
" <g transform=\"translate(12.2 82.360425) scale(0.1 -0.1)\">\n",
" <use xlink:href=\"#SimHei-32\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <g id=\"ytick_5\">\n",
" <g id=\"line2d_10\">\n",
" <g>\n",
" <use xlink:href=\"#mb5a8a9618e\" x=\"24.2\" y=\"32.296609\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"text_10\">\n",
" <!-- 3 -->\n",
" <g transform=\"translate(12.2 35.714578) scale(0.1 -0.1)\">\n",
" <defs>\n",
" <path id=\"SimHei-33\" d=\"M 2825 1300 \n",
"Q 2825 725 2462 387 \n",
"Q 2100 50 1550 50 \n",
"Q 1000 50 637 387 \n",
"Q 275 725 275 1425 \n",
"L 850 1425 \n",
"Q 850 950 1037 737 \n",
"Q 1225 525 1550 525 \n",
"Q 1875 525 2050 725 \n",
"Q 2225 925 2225 1350 \n",
"Q 2225 1700 2037 1900 \n",
"Q 1850 2100 1375 2100 \n",
"L 1375 2525 \n",
"Q 1775 2525 1962 2725 \n",
"Q 2150 2925 2150 3325 \n",
"Q 2150 3625 2012 3800 \n",
"Q 1875 3975 1575 3975 \n",
"Q 1275 3975 1112 3762 \n",
"Q 950 3550 925 3150 \n",
"L 375 3150 \n",
"Q 425 3725 737 4075 \n",
"Q 1050 4425 1575 4425 \n",
"Q 2125 4425 2425 4112 \n",
"Q 2725 3800 2725 3350 \n",
"Q 2725 2925 2575 2687 \n",
"Q 2425 2450 2075 2325 \n",
"Q 2425 2250 2625 1975 \n",
"Q 2825 1700 2825 1300 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" </defs>\n",
" <use xlink:href=\"#SimHei-33\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <g id=\"line2d_11\">\n",
" <path d=\"M 44.490909 149.870942 \n",
"L 50.206658 144.609862 \n",
"L 55.922407 139.842608 \n",
"L 61.638156 135.654402 \n",
"L 67.353905 132.120113 \n",
"L 73.069654 129.302924 \n",
"L 78.785403 127.253194 \n",
"L 84.501152 126.007566 \n",
"L 90.216901 125.588308 \n",
"L 95.93265 126.002914 \n",
"L 101.648399 127.243973 \n",
"L 107.364149 129.289299 \n",
"L 113.079898 132.102328 \n",
"L 118.795647 135.632774 \n",
"L 124.511396 139.817525 \n",
"L 130.227145 144.581771 \n",
"L 135.942894 149.840345 \n",
"L 141.658643 155.499242 \n",
"L 147.374392 161.457301 \n",
"L 153.090141 167.608012 \n",
"L 158.80589 173.841422 \n",
"L 164.521639 180.0461 \n",
"L 170.237388 186.111127 \n",
"L 175.953137 191.928083 \n",
"L 181.668886 197.39298 \n",
"L 187.384635 202.408126 \n",
"L 193.100384 206.883866 \n",
"L 198.816133 210.740191 \n",
"L 204.531882 213.908163 \n",
"L 210.247631 216.331149 \n",
"L 215.96338 217.965836 \n",
"L 221.679129 218.782999 \n",
"L 227.394878 218.768033 \n",
"L 233.110627 217.921204 \n",
"L 238.826376 216.25765 \n",
"L 244.542125 213.80711 \n",
"L 250.257875 210.613391 \n",
"L 255.973624 206.733585 \n",
"L 261.689373 202.237051 \n",
"L 267.405122 197.20417 \n",
"L 273.120871 191.724912 \n",
"L 278.83662 185.897228 \n",
"L 284.552369 179.825295 \n",
"L 290.268118 173.61766 \n",
"L 295.983867 167.385293 \n",
"L 301.699616 161.239606 \n",
"L 307.415365 155.290463 \n",
"L 313.131114 149.644214 \n",
"L 318.846863 144.401793 \n",
"L 324.562612 139.656919 \n",
"L 330.278361 135.49441 \n",
"L 335.99411 131.98868 \n",
"L 341.709859 129.202398 \n",
"L 347.425608 127.185374 \n",
"L 353.141357 125.973663 \n",
"L 358.857106 125.588929 \n",
"L 364.572855 126.038047 \n",
"L 370.288604 127.31299 \n",
"L 376.004353 129.390967 \n",
"L 381.720102 132.234829 \n",
"L 387.435851 135.79374 \n",
"L 393.151601 140.004077 \n",
"L 398.86735 144.790575 \n",
"L 404.583099 150.067669 \n",
"L 410.298848 155.741021 \n",
"L 416.014597 161.709213 \n",
"L 421.730346 167.865554 \n",
"L 427.446095 174.099991 \n",
"L 433.161844 180.301072 \n",
"L 438.877593 186.357946 \n",
"L 444.593342 192.162335 \n",
"L 450.309091 197.610478 \n",
"\" clip-path=\"url(#pcb31c92243)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n",
" <defs>\n",
" <path id=\"m5d2fc65245\" d=\"M 0 1.5 \n",
"C 0.397805 1.5 0.77937 1.341951 1.06066 1.06066 \n",
"C 1.341951 0.77937 1.5 0.397805 1.5 0 \n",
"C 1.5 -0.397805 1.341951 -0.77937 1.06066 -1.06066 \n",
"C 0.77937 -1.341951 0.397805 -1.5 0 -1.5 \n",
"C -0.397805 -1.5 -0.77937 -1.341951 -1.06066 -1.06066 \n",
"C -1.341951 -0.77937 -1.5 -0.397805 -1.5 0 \n",
"C -1.5 0.397805 -1.341951 0.77937 -1.06066 1.06066 \n",
"C -0.77937 1.341951 -0.397805 1.5 0 1.5 \n",
"z\n",
"\" style=\"stroke: #1f77b4\"/>\n",
" </defs>\n",
" <g clip-path=\"url(#pcb31c92243)\">\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"44.490909\" y=\"149.870942\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"50.206658\" y=\"144.609862\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"55.922407\" y=\"139.842608\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"61.638156\" y=\"135.654402\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"67.353905\" y=\"132.120113\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"73.069654\" y=\"129.302924\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"78.785403\" y=\"127.253194\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"84.501152\" y=\"126.007566\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"90.216901\" y=\"125.588308\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"95.93265\" y=\"126.002914\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"101.648399\" y=\"127.243973\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"107.364149\" y=\"129.289299\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"113.079898\" y=\"132.102328\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"118.795647\" y=\"135.632774\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"124.511396\" y=\"139.817525\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"130.227145\" y=\"144.581771\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"135.942894\" y=\"149.840345\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"141.658643\" y=\"155.499242\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"147.374392\" y=\"161.457301\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"153.090141\" y=\"167.608012\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"158.80589\" y=\"173.841422\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"164.521639\" y=\"180.0461\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"170.237388\" y=\"186.111127\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"175.953137\" y=\"191.928083\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"181.668886\" y=\"197.39298\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"187.384635\" y=\"202.408126\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"193.100384\" y=\"206.883866\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"198.816133\" y=\"210.740191\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"204.531882\" y=\"213.908163\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"210.247631\" y=\"216.331149\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"215.96338\" y=\"217.965836\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"221.679129\" y=\"218.782999\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"227.394878\" y=\"218.768033\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"233.110627\" y=\"217.921204\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"238.826376\" y=\"216.25765\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"244.542125\" y=\"213.80711\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"250.257875\" y=\"210.613391\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"255.973624\" y=\"206.733585\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"261.689373\" y=\"202.237051\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"267.405122\" y=\"197.20417\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"273.120871\" y=\"191.724912\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"278.83662\" y=\"185.897228\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"284.552369\" y=\"179.825295\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"290.268118\" y=\"173.61766\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"295.983867\" y=\"167.385293\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"301.699616\" y=\"161.239606\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"307.415365\" y=\"155.290463\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"313.131114\" y=\"149.644214\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"318.846863\" y=\"144.401793\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"324.562612\" y=\"139.656919\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"330.278361\" y=\"135.49441\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"335.99411\" y=\"131.98868\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"341.709859\" y=\"129.202398\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"347.425608\" y=\"127.185374\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"353.141357\" y=\"125.973663\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"358.857106\" y=\"125.588929\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"364.572855\" y=\"126.038047\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"370.288604\" y=\"127.31299\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"376.004353\" y=\"129.390967\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"381.720102\" y=\"132.234829\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"387.435851\" y=\"135.79374\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"393.151601\" y=\"140.004077\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"398.86735\" y=\"144.790575\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"404.583099\" y=\"150.067669\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"410.298848\" y=\"155.741021\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"416.014597\" y=\"161.709213\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"421.730346\" y=\"167.865554\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"427.446095\" y=\"174.099991\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"433.161844\" y=\"180.301072\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"438.877593\" y=\"186.357946\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"444.593342\" y=\"192.162335\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"450.309091\" y=\"197.610478\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"line2d_12\">\n",
" <path d=\"M 44.490909 218.88 \n",
"L 50.206658 202.922177 \n",
"L 55.922407 190.032472 \n",
"L 61.638156 179.219234 \n",
"L 67.353905 169.905348 \n",
"L 73.069654 161.725201 \n",
"L 78.785403 154.432495 \n",
"L 84.501152 147.853383 \n",
"L 90.216901 141.860579 \n",
"L 95.93265 136.358079 \n",
"L 101.648399 131.271687 \n",
"L 107.364149 126.54287 \n",
"L 113.079898 122.124638 \n",
"L 118.795647 117.978695 \n",
"L 124.511396 114.07342 \n",
"L 130.227145 110.382397 \n",
"L 135.942894 106.883335 \n",
"L 141.658643 103.557251 \n",
"L 147.374392 100.387845 \n",
"L 153.090141 97.361018 \n",
"L 158.80589 94.464492 \n",
"L 164.521639 91.68751 \n",
"L 170.237388 89.020594 \n",
"L 175.953137 86.45535 \n",
"L 181.668886 83.984312 \n",
"L 187.384635 81.600803 \n",
"L 193.100384 79.298834 \n",
"L 198.816133 77.073011 \n",
"L 204.531882 74.918456 \n",
"L 210.247631 72.830747 \n",
"L 215.96338 70.805862 \n",
"L 221.679129 68.840128 \n",
"L 227.394878 66.930188 \n",
"L 233.110627 65.072962 \n",
"L 238.826376 63.265618 \n",
"L 244.542125 61.505547 \n",
"L 250.257875 59.790338 \n",
"L 255.973624 58.117761 \n",
"L 261.689373 56.485749 \n",
"L 267.405122 54.89238 \n",
"L 273.120871 53.335866 \n",
"L 278.83662 51.814542 \n",
"L 284.552369 50.32685 \n",
"L 290.268118 48.871336 \n",
"L 295.983867 47.446638 \n",
"L 301.699616 46.051478 \n",
"L 307.415365 44.684655 \n",
"L 313.131114 43.345041 \n",
"L 318.846863 42.031575 \n",
"L 324.562612 40.743254 \n",
"L 330.278361 39.479135 \n",
"L 335.99411 38.238325 \n",
"L 341.709859 37.019979 \n",
"L 347.425608 35.823299 \n",
"L 353.141357 34.647527 \n",
"L 358.857106 33.491946 \n",
"L 364.572855 32.355874 \n",
"L 370.288604 31.238663 \n",
"L 376.004353 30.139697 \n",
"L 381.720102 29.058389 \n",
"L 387.435851 27.994181 \n",
"L 393.151601 26.946541 \n",
"L 398.86735 25.914961 \n",
"L 404.583099 24.898955 \n",
"L 410.298848 23.89806 \n",
"L 416.014597 22.911835 \n",
"L 421.730346 21.939853 \n",
"L 427.446095 20.981711 \n",
"L 433.161844 20.03702 \n",
"L 438.877593 19.105407 \n",
"L 444.593342 18.186514 \n",
"L 450.309091 17.28 \n",
"\" clip-path=\"url(#pcb31c92243)\" style=\"fill: none; stroke: #9c9c9c; stroke-width: 3; stroke-linecap: square\"/>\n",
" </g>\n",
" <g id=\"line2d_13\">\n",
" <path d=\"M 44.490909 139.250557 \n",
"L 50.206658 135.098584 \n",
"L 55.922407 131.366276 \n",
"L 61.638156 127.947403 \n",
"L 67.353905 124.774179 \n",
"L 73.069654 121.800216 \n",
"L 78.785403 118.992113 \n",
"L 84.501152 116.324872 \n",
"L 90.216901 113.779208 \n",
"L 95.93265 111.339872 \n",
"L 101.648399 108.994558 \n",
"L 107.364149 106.733167 \n",
"L 113.079898 104.547285 \n",
"L 118.795647 102.42982 \n",
"L 124.511396 100.374722 \n",
"L 130.227145 98.376786 \n",
"L 135.942894 96.431491 \n",
"L 141.658643 94.534884 \n",
"L 147.374392 92.683482 \n",
"L 153.090141 90.874199 \n",
"L 158.80589 89.104285 \n",
"L 164.521639 87.371276 \n",
"L 170.237388 85.672956 \n",
"L 175.953137 84.007322 \n",
"L 181.668886 82.372557 \n",
"L 187.384635 80.767004 \n",
"L 193.100384 79.189152 \n",
"L 198.816133 77.637614 \n",
"L 204.531882 76.111117 \n",
"L 210.247631 74.608486 \n",
"L 215.96338 73.128635 \n",
"L 221.679129 71.670558 \n",
"L 227.394878 70.233322 \n",
"L 233.110627 68.816058 \n",
"L 238.826376 67.417956 \n",
"L 244.542125 66.038258 \n",
"L 250.257875 64.676257 \n",
"L 255.973624 63.331289 \n",
"L 261.689373 62.00273 \n",
"L 267.405122 60.689993 \n",
"L 273.120871 59.392528 \n",
"L 278.83662 58.109812 \n",
"L 284.552369 56.841354 \n",
"L 290.268118 55.586688 \n",
"L 295.983867 54.345375 \n",
"L 301.699616 53.116997 \n",
"L 307.415365 51.901158 \n",
"L 313.131114 50.697482 \n",
"L 318.846863 49.50561 \n",
"L 324.562612 48.325202 \n",
"L 330.278361 47.155933 \n",
"L 335.99411 45.997494 \n",
"L 341.709859 44.84959 \n",
"L 347.425608 43.711938 \n",
"L 353.141357 42.584268 \n",
"L 358.857106 41.466323 \n",
"L 364.572855 40.357854 \n",
"L 370.288604 39.258624 \n",
"L 376.004353 38.168408 \n",
"L 381.720102 37.086985 \n",
"L 387.435851 36.014148 \n",
"L 393.151601 34.949694 \n",
"L 398.86735 33.89343 \n",
"L 404.583099 32.845171 \n",
"L 410.298848 31.804736 \n",
"L 416.014597 30.771953 \n",
"L 421.730346 29.746655 \n",
"L 427.446095 28.728683 \n",
"L 433.161844 27.717881 \n",
"L 438.877593 26.714101 \n",
"L 444.593342 25.717197 \n",
"L 450.309091 24.72703 \n",
"\" clip-path=\"url(#pcb31c92243)\" style=\"fill: none; stroke-dasharray: 3.2,0.8,0.5,0.8; stroke-dashoffset: 0; stroke: #ff7f0e; stroke-width: 0.5\"/>\n",
" </g>\n",
" <g id=\"patch_3\">\n",
" <path d=\"M 24.2 228.96 \n",
"L 24.2 7.2 \n",
"\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
" </g>\n",
" <g id=\"patch_4\">\n",
" <path d=\"M 470.6 228.96 \n",
"L 470.6 7.2 \n",
"\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
" </g>\n",
" <g id=\"patch_5\">\n",
" <path d=\"M 24.2 228.96 \n",
"L 470.6 228.96 \n",
"\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
" </g>\n",
" <g id=\"patch_6\">\n",
" <path d=\"M 24.2 7.2 \n",
"L 470.6 7.2 \n",
"\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
" </g>\n",
" <g id=\"legend_1\">\n",
" <g id=\"patch_7\">\n",
" <path d=\"M 385.3 142.23 \n",
"L 463.6 142.23 \n",
"Q 465.6 142.23 465.6 140.23 \n",
"L 465.6 95.93 \n",
"Q 465.6 93.93 463.6 93.93 \n",
"L 385.3 93.93 \n",
"Q 383.3 93.93 383.3 95.93 \n",
"L 383.3 140.23 \n",
"Q 383.3 142.23 385.3 142.23 \n",
"z\n",
"\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n",
" </g>\n",
" <g id=\"line2d_14\">\n",
" <path d=\"M 387.3 102.03 \n",
"L 397.3 102.03 \n",
"L 407.3 102.03 \n",
"\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n",
" <g>\n",
" <use xlink:href=\"#m5d2fc65245\" x=\"397.3\" y=\"102.03\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"text_11\">\n",
" <!-- $y=sin(x)$ -->\n",
" <g transform=\"translate(415.3 105.53) scale(0.1 -0.1)\">\n",
" <defs>\n",
" <path id=\"DejaVuSans-Oblique-79\" d=\"M 1588 -325 \n",
"Q 1188 -997 936 -1164 \n",
"Q 684 -1331 294 -1331 \n",
"L -159 -1331 \n",
"L -63 -850 \n",
"L 269 -850 \n",
"Q 509 -850 678 -719 \n",
"Q 847 -588 1056 -206 \n",
"L 1234 128 \n",
"L 459 3500 \n",
"L 1069 3500 \n",
"L 1650 819 \n",
"L 3256 3500 \n",
"L 3859 3500 \n",
"L 1588 -325 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" <path id=\"DejaVuSans-3d\" d=\"M 678 2906 \n",
"L 4684 2906 \n",
"L 4684 2381 \n",
"L 678 2381 \n",
"L 678 2906 \n",
"z\n",
"M 678 1631 \n",
"L 4684 1631 \n",
"L 4684 1100 \n",
"L 678 1100 \n",
"L 678 1631 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" <path id=\"DejaVuSans-Oblique-73\" d=\"M 3200 3397 \n",
"L 3091 2853 \n",
"Q 2863 2978 2609 3040 \n",
"Q 2356 3103 2088 3103 \n",
"Q 1634 3103 1373 2948 \n",
"Q 1113 2794 1113 2528 \n",
"Q 1113 2219 1719 2053 \n",
"Q 1766 2041 1788 2034 \n",
"L 1972 1978 \n",
"Q 2547 1819 2739 1644 \n",
"Q 2931 1469 2931 1166 \n",
"Q 2931 609 2489 259 \n",
"Q 2047 -91 1331 -91 \n",
"Q 1053 -91 747 -37 \n",
"Q 441 16 72 128 \n",
"L 184 722 \n",
"Q 500 559 806 475 \n",
"Q 1113 391 1394 391 \n",
"Q 1816 391 2080 572 \n",
"Q 2344 753 2344 1031 \n",
"Q 2344 1331 1650 1516 \n",
"L 1591 1531 \n",
"L 1394 1581 \n",
"Q 956 1697 753 1886 \n",
"Q 550 2075 550 2369 \n",
"Q 550 2928 970 3256 \n",
"Q 1391 3584 2113 3584 \n",
"Q 2397 3584 2667 3537 \n",
"Q 2938 3491 3200 3397 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" <path id=\"DejaVuSans-Oblique-69\" d=\"M 1172 4863 \n",
"L 1747 4863 \n",
"L 1606 4134 \n",
"L 1031 4134 \n",
"L 1172 4863 \n",
"z\n",
"M 909 3500 \n",
"L 1484 3500 \n",
"L 800 0 \n",
"L 225 0 \n",
"L 909 3500 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" <path id=\"DejaVuSans-Oblique-6e\" d=\"M 3566 2113 \n",
"L 3156 0 \n",
"L 2578 0 \n",
"L 2988 2091 \n",
"Q 3016 2238 3031 2350 \n",
"Q 3047 2463 3047 2528 \n",
"Q 3047 2791 2881 2937 \n",
"Q 2716 3084 2419 3084 \n",
"Q 1956 3084 1622 2776 \n",
"Q 1288 2469 1184 1941 \n",
"L 800 0 \n",
"L 225 0 \n",
"L 903 3500 \n",
"L 1478 3500 \n",
"L 1363 2950 \n",
"Q 1603 3253 1940 3418 \n",
"Q 2278 3584 2650 3584 \n",
"Q 3113 3584 3367 3334 \n",
"Q 3622 3084 3622 2631 \n",
"Q 3622 2519 3608 2391 \n",
"Q 3594 2263 3566 2113 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" <path id=\"DejaVuSans-28\" d=\"M 1984 4856 \n",
"Q 1566 4138 1362 3434 \n",
"Q 1159 2731 1159 2009 \n",
"Q 1159 1288 1364 580 \n",
"Q 1569 -128 1984 -844 \n",
"L 1484 -844 \n",
"Q 1016 -109 783 600 \n",
"Q 550 1309 550 2009 \n",
"Q 550 2706 781 3412 \n",
"Q 1013 4119 1484 4856 \n",
"L 1984 4856 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" <path id=\"DejaVuSans-Oblique-78\" d=\"M 3841 3500 \n",
"L 2234 1784 \n",
"L 3219 0 \n",
"L 2559 0 \n",
"L 1819 1388 \n",
"L 531 0 \n",
"L -166 0 \n",
"L 1556 1844 \n",
"L 641 3500 \n",
"L 1300 3500 \n",
"L 1972 2234 \n",
"L 3144 3500 \n",
"L 3841 3500 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" <path id=\"DejaVuSans-29\" d=\"M 513 4856 \n",
"L 1013 4856 \n",
"Q 1481 4119 1714 3412 \n",
"Q 1947 2706 1947 2009 \n",
"Q 1947 1309 1714 600 \n",
"Q 1481 -109 1013 -844 \n",
"L 513 -844 \n",
"Q 928 -128 1133 580 \n",
"Q 1338 1288 1338 2009 \n",
"Q 1338 2731 1133 3434 \n",
"Q 928 4138 513 4856 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" </defs>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-79\" transform=\"translate(0 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-3d\" transform=\"translate(78.662109 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-73\" transform=\"translate(181.933594 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-69\" transform=\"translate(234.033203 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-6e\" transform=\"translate(261.816406 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-28\" transform=\"translate(325.195312 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-78\" transform=\"translate(364.208984 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-29\" transform=\"translate(423.388672 0.015625)\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"line2d_15\">\n",
" <path d=\"M 387.3 116.73 \n",
"L 397.3 116.73 \n",
"L 407.3 116.73 \n",
"\" style=\"fill: none; stroke: #9c9c9c; stroke-width: 3; stroke-linecap: square\"/>\n",
" </g>\n",
" <g id=\"text_12\">\n",
" <!-- $y=log_{2}x$ -->\n",
" <g transform=\"translate(415.3 120.23) scale(0.1 -0.1)\">\n",
" <defs>\n",
" <path id=\"DejaVuSans-Oblique-6c\" d=\"M 1172 4863 \n",
"L 1747 4863 \n",
"L 800 0 \n",
"L 225 0 \n",
"L 1172 4863 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" <path id=\"DejaVuSans-Oblique-6f\" d=\"M 1625 -91 \n",
"Q 1009 -91 651 289 \n",
"Q 294 669 294 1325 \n",
"Q 294 1706 417 2101 \n",
"Q 541 2497 738 2766 \n",
"Q 1047 3184 1428 3384 \n",
"Q 1809 3584 2291 3584 \n",
"Q 2888 3584 3255 3212 \n",
"Q 3622 2841 3622 2241 \n",
"Q 3622 1825 3500 1412 \n",
"Q 3378 1000 3181 728 \n",
"Q 2875 309 2494 109 \n",
"Q 2113 -91 1625 -91 \n",
"z\n",
"M 891 1344 \n",
"Q 891 869 1089 633 \n",
"Q 1288 397 1691 397 \n",
"Q 2269 397 2648 901 \n",
"Q 3028 1406 3028 2181 \n",
"Q 3028 2634 2825 2865 \n",
"Q 2622 3097 2228 3097 \n",
"Q 1903 3097 1650 2945 \n",
"Q 1397 2794 1197 2484 \n",
"Q 1050 2253 970 1956 \n",
"Q 891 1659 891 1344 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" <path id=\"DejaVuSans-Oblique-67\" d=\"M 3816 3500 \n",
"L 3219 434 \n",
"Q 3047 -456 2561 -893 \n",
"Q 2075 -1331 1253 -1331 \n",
"Q 950 -1331 690 -1286 \n",
"Q 431 -1241 206 -1147 \n",
"L 313 -588 \n",
"Q 525 -725 762 -790 \n",
"Q 1000 -856 1269 -856 \n",
"Q 1816 -856 2167 -557 \n",
"Q 2519 -259 2631 300 \n",
"L 2681 563 \n",
"Q 2441 288 2122 144 \n",
"Q 1803 0 1434 0 \n",
"Q 903 0 598 351 \n",
"Q 294 703 294 1319 \n",
"Q 294 1803 478 2267 \n",
"Q 663 2731 997 3091 \n",
"Q 1219 3328 1514 3456 \n",
"Q 1809 3584 2131 3584 \n",
"Q 2484 3584 2746 3420 \n",
"Q 3009 3256 3138 2956 \n",
"L 3238 3500 \n",
"L 3816 3500 \n",
"z\n",
"M 2950 2216 \n",
"Q 2950 2641 2750 2872 \n",
"Q 2550 3103 2181 3103 \n",
"Q 1953 3103 1747 3012 \n",
"Q 1541 2922 1394 2759 \n",
"Q 1156 2491 1023 2127 \n",
"Q 891 1763 891 1375 \n",
"Q 891 944 1092 712 \n",
"Q 1294 481 1672 481 \n",
"Q 2219 481 2584 976 \n",
"Q 2950 1472 2950 2216 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" <path id=\"DejaVuSans-32\" d=\"M 1228 531 \n",
"L 3431 531 \n",
"L 3431 0 \n",
"L 469 0 \n",
"L 469 531 \n",
"Q 828 903 1448 1529 \n",
"Q 2069 2156 2228 2338 \n",
"Q 2531 2678 2651 2914 \n",
"Q 2772 3150 2772 3378 \n",
"Q 2772 3750 2511 3984 \n",
"Q 2250 4219 1831 4219 \n",
"Q 1534 4219 1204 4116 \n",
"Q 875 4013 500 3803 \n",
"L 500 4441 \n",
"Q 881 4594 1212 4672 \n",
"Q 1544 4750 1819 4750 \n",
"Q 2544 4750 2975 4387 \n",
"Q 3406 4025 3406 3419 \n",
"Q 3406 3131 3298 2873 \n",
"Q 3191 2616 2906 2266 \n",
"Q 2828 2175 2409 1742 \n",
"Q 1991 1309 1228 531 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" </defs>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-79\" transform=\"translate(0 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-3d\" transform=\"translate(78.662109 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-6c\" transform=\"translate(181.933594 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-6f\" transform=\"translate(209.716797 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-67\" transform=\"translate(270.898438 0.015625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(334.375 -16.390625) scale(0.7)\"/>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-78\" transform=\"translate(381.645508 0.015625)\"/>\n",
" </g>\n",
" </g>\n",
" <g id=\"line2d_16\">\n",
" <path d=\"M 387.3 132.63 \n",
"L 397.3 132.63 \n",
"L 407.3 132.63 \n",
"\" style=\"fill: none; stroke-dasharray: 3.2,0.8,0.5,0.8; stroke-dashoffset: 0; stroke: #ff7f0e; stroke-width: 0.5\"/>\n",
" </g>\n",
" <g id=\"text_13\">\n",
" <!-- $y=\\sqrt{x}$ -->\n",
" <g transform=\"translate(415.3 136.13) scale(0.1 -0.1)\">\n",
" <defs>\n",
" <path id=\"DejaVuSans-221a\" d=\"M 3488 5191 \n",
"L 4078 5191 \n",
"L 4078 4891 \n",
"L 3719 4891 \n",
"L 1863 -128 \n",
"L 1656 -128 \n",
"L 659 2631 \n",
"L 269 2491 \n",
"L 191 2741 \n",
"L 1075 3047 \n",
"L 1875 831 \n",
"L 3488 5191 \n",
"z\n",
"\" transform=\"scale(0.015625)\"/>\n",
" </defs>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-79\" transform=\"translate(0 0.890625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-3d\" transform=\"translate(78.662109 0.890625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-221a\" transform=\"translate(181.933594 0.890625)\"/>\n",
" <use xlink:href=\"#DejaVuSans-Oblique-78\" transform=\"translate(258.154297 1.0625)\"/>\n",
" <path d=\"M 245.654297 75.5 \n",
"L 245.654297 81.75 \n",
"L 329.833984 81.75 \n",
"L 329.833984 75.5 \n",
"L 245.654297 75.5 \n",
"z\n",
"\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <defs>\n",
" <clipPath id=\"pcb31c92243\">\n",
" <rect x=\"24.2\" y=\"7.2\" width=\"446.4\" height=\"221.76\"/>\n",
" </clipPath>\n",
" </defs>\n",
"</svg>\n"
],
"text/plain": [
"<Figure size 800x400 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 定制画布\n",
"plt.figure(figsize=(8, 4))\n",
"# 绘制折线图\n",
"plt.plot(x, y1, marker='.', label='$y=sin(x)$')\n",
"plt.plot(x, y2, label='$y=log_{2}x$', linewidth=3, color='#9c9c9c')\n",
"plt.plot(x, y3, label='$y=\\sqrt{x}$', linestyle='-.', linewidth=0.5)\n",
"# 显示图例\n",
"plt.legend(loc='center right')\n",
"# 显示图表\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "0b292f56-dab7-469e-89ed-0fb2114902aa",
"metadata": {},
"source": [
"#### 通用二元函数"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "8b67932a-481e-4e2d-9d83-00994a01d959",
"metadata": {},
"outputs": [],
"source": [
"array2 = np.array([0.1 + 0.2, 0.1 + 0.2 + 0.3])\n",
"array3 = np.array([0.3, 0.6])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "23581a64-7b02-4f3f-8a5f-ea49ec20e48a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([False, False])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2 == array3"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "ddcb612c-c7aa-44c6-b8d3-1ee123fba534",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.False_"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.all(array2 == array3)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "40d454ad-8c60-4132-8dde-10726180e552",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 比较两个数组元素是否(几乎)完全相等 - 有误差容忍度\n",
"np.allclose(array2, array3)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "f1aab287-9d50-4b32-94fa-26d7f183fde3",
"metadata": {},
"outputs": [],
"source": [
"array4 = np.array([1, 2, 3, 4, 5, 6])\n",
"array5 = np.array([2, 4, 6, 8, 10])"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "ea25f2ad-e007-486b-a402-eb3436c9346c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 4, 6])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 交集\n",
"np.intersect1d(array4, array5)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "e4d2116c-c895-4597-95dc-fda67a1c99a8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 2, 3, 4, 5, 6, 8, 10])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 并集\n",
"np.union1d(array4, array5)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "5348c4f2-4222-4904-bd07-a3c85e62e4c7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 3, 5])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 差集\n",
"np.setdiff1d(array4, array5)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "9cd3f3e5-a986-469f-97ba-c739aa4b8577",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 3, 5, 8, 10])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 对称差\n",
"np.setxor1d(array4, array5)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "78eb3a98-5992-452e-ab13-eaf578cab7a0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([False, True, False, True, False, True])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 成员运算\n",
"# np.in1d(array4, array5)\n",
"np.isin(array4, array5)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "c7bbc624-dfdc-41d9-bfa2-7a4f3a387bce",
"metadata": {},
"outputs": [],
"source": [
"# 杰卡德相似度\n",
"user_a = np.array(['平板电脑', '尿不湿', '手机', '键盘', '手机支架', '奶瓶', '婴儿辅食', '基围虾', '巴沙鱼', '生抽', '沙拉酱'])\n",
"user_b = np.array(['平板电脑', '键盘', '充电宝', '补光灯', '生抽', '散热器', '笔记本电脑', '双肩包', '登山杖', '露营帐篷', '睡袋'])\n",
"user_c = np.array(['沐浴露', '维C泡腾片', '牛奶', '尿不湿', '平板电脑', '奶瓶', '婴儿辅食', '手机', '磨牙棒', '生抽', '基围虾'])"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "c4132979-1ef5-4e2b-93a4-2d6bfc66f38b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.15789473684210525"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.intersect1d(user_a, user_b).size / np.union1d(user_a, user_b).size"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "46dda506-908b-405a-8e9b-c14090da05b7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.4666666666666667"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.intersect1d(user_a, user_c).size / np.union1d(user_a, user_c).size"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb5435f8-b8d4-4b37-88fb-13149a62660e",
"metadata": {},
"outputs": [],
"source": [
"np.setdiff1d(user_a, user_c)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f8366b08-779d-4369-9f27-a9b4b9125782",
"metadata": {},
"outputs": [],
"source": [
"np.setdiff1d(user_c, user_a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b1ce07b5-ec70-4512-814a-e210148ed205",
"metadata": {},
"outputs": [],
"source": [
"# 余弦相似度\n",
"user = np.array([5, 1, 3])\n",
"mov1 = np.array([4, 5, 1])\n",
"mov2 = np.array([5, 1, 5])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ab70c0f-cfe7-4e10-b162-0feefb36f884",
"metadata": {},
"outputs": [],
"source": [
"# linear algebra\n",
"# np.dot - 点积\n",
"# np.linalg.norm - 模长\n",
"np.dot(user, mov1) / (np.linalg.norm(user) * np.linalg.norm(mov1))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "21a1caaf-2cf4-4cf8-836c-d244f4133098",
"metadata": {},
"outputs": [],
"source": [
"# np.arcos - 反余弦函数 - 弧度\n",
"# np.degrees - 弧度换算角度\n",
"np.degrees(np.arccos(np.dot(user, mov1) / (np.linalg.norm(user) * np.linalg.norm(mov1))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b90ecb1-67d0-48be-84c5-54bf212f1292",
"metadata": {},
"outputs": [],
"source": [
"np.degrees(np.arccos(np.dot(user, mov2) / (np.linalg.norm(user) * np.linalg.norm(mov2))))"
]
},
{
"cell_type": "markdown",
"id": "c81c6238-f28c-44e8-ac54-94a69f5a6c4a",
"metadata": {},
"source": [
"#### 其他常用函数"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1df2d086-58cd-4324-b2cf-98d8d971a4d7",
"metadata": {},
"outputs": [],
"source": [
"array6 = np.array([1, 2, 3, 1, 1, 2, 2, 4, 5, 7, 3, 6, 6])\n",
"array6"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "70df0735-6680-4996-8cc3-10ac5d9102ab",
"metadata": {},
"outputs": [],
"source": [
"# 去重\n",
"array7 = np.unique(array6)\n",
"array7"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "79ad92e0-da87-401d-aa70-0514a5b61d0e",
"metadata": {},
"outputs": [],
"source": [
"array8 = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])\n",
"array9 = np.array([[4, 4, 4], [5, 5, 5], [6, 6, 6]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b099b5ee-d7df-4864-89f3-bb1c7a748a59",
"metadata": {},
"outputs": [],
"source": [
"# 在0轴方向垂直堆叠 - vertical\n",
"array10 = np.vstack((array8, array9))\n",
"array10"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "204b8e34-43e3-4a4f-9e8c-8734e72a041f",
"metadata": {},
"outputs": [],
"source": [
"# 在1轴的方向堆叠 - horizontal\n",
"np.hstack((array8, array9))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3301cc02-3ecf-4516-b2ff-e0fa6a9041c7",
"metadata": {},
"outputs": [],
"source": [
"# 数组的拼接\n",
"np.concatenate((array8, array9), axis=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c956190c-1382-4416-9ab2-09a19f5567f6",
"metadata": {},
"outputs": [],
"source": [
"# 堆叠出更高维的数组\n",
"np.stack((array8, array9), axis=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a956be87-65e9-4391-b2be-bfbaaab7e7dc",
"metadata": {},
"outputs": [],
"source": [
"np.stack((array8, array9), axis=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df88bd16-64c3-40af-adba-f7faed516159",
"metadata": {},
"outputs": [],
"source": [
"# 将一个数组拆分成多个数组\n",
"np.vsplit(array10, 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "592ffc62-2a47-48bc-8789-3cad653d2893",
"metadata": {},
"outputs": [],
"source": [
"# 追加元素\n",
"np.append(array6, [10, 11, 12])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a3355ca-1ee7-4d8f-8567-c10fed5055f6",
"metadata": {},
"outputs": [],
"source": [
"# 插入元素\n",
"np.insert(array6, 1, [10, 20])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b824b66-7471-4669-aa99-8f62a6b8cb2b",
"metadata": {},
"outputs": [],
"source": [
"array11 = np.random.randint(1, 100, 10)\n",
"array11"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2ff82b6f-9a96-40c2-a4d9-4aaec7d42e9a",
"metadata": {},
"outputs": [],
"source": [
"# 抽取元素 - 相当于布尔索引的作用\n",
"np.extract(array11 < 50, array11)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc00dac0-0b97-435c-960d-06c141de0b78",
"metadata": {},
"outputs": [],
"source": [
"# 给出一组条件和对应的处理数据的表达式,满足条件就执行对应的表达式,不满足条件取默认值\n",
"np.select([array11 < 30, array11 > 50], [array11 * 10, array11 // 10], default=100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d7b8700-a431-4da4-99c6-54eda9f065dd",
"metadata": {},
"outputs": [],
"source": [
"# 给出一个条件和两个表达式满足条件执行表达式1不满足条件执行表达式2\n",
"np.where(array11 < 50, array11 * 10, array11 // 10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc672588-f36b-4951-902c-6b70ef83d1af",
"metadata": {},
"outputs": [],
"source": [
"array11"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa96b613-cef4-475e-b88c-f6ee8194ef4c",
"metadata": {},
"outputs": [],
"source": [
"# 滚动数组元素\n",
"np.roll(array11, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bfeed7b4-d835-4a1a-9858-d27d4bc363c3",
"metadata": {},
"outputs": [],
"source": [
"np.roll(array11, -2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52b9da9d-dd2b-4fea-984c-6d4b94efedab",
"metadata": {},
"outputs": [],
"source": [
"np.roll(array10, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53aa8382-51e2-4634-bb9f-4ff84ddaef60",
"metadata": {},
"outputs": [],
"source": [
"np.roll(array10, 2, axis=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f2de13b-2773-4a87-a854-ee9486dfcc0d",
"metadata": {},
"outputs": [],
"source": [
"array12 = np.arange(1, 10).reshape((3, 3))\n",
"array12"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3808483f-2811-45fd-adbb-a10bcc9d7dc6",
"metadata": {},
"outputs": [],
"source": [
"np.roll(array12, 2, axis=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f6d8d16-2e35-4926-ba13-d5e9cf77660d",
"metadata": {},
"outputs": [],
"source": [
"np.roll(array12, 1, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29b76e3a-8414-4658-b0c6-7795f2186fbe",
"metadata": {},
"outputs": [],
"source": [
"# 替换数组元素\n",
"np.put(array11, [1, 3, 5, 7], [33, 88])\n",
"array11"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb4653ef-5c5a-4d3c-adb2-0f10d79ca6c6",
"metadata": {},
"outputs": [],
"source": [
"np.place(array11, array11 > 50, [44, 99])\n",
"array11"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03bcc628-1471-44ef-ad56-88468c08548d",
"metadata": {},
"outputs": [],
"source": [
"guido_image = plt.imread('res/guido.jpg')\n",
"guido_image.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16fa2543-195b-47d6-8e29-2682800f91aa",
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(np.flip(guido_image, axis=0))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "259830fa-6cd3-43be-bffe-560050f795b9",
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(np.flip(guido_image, axis=1))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6059087d-85ba-4151-9af0-8870876a6b1a",
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(np.flip(guido_image, axis=2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "70ebbf72-87c9-41d8-8c46-f1eccb531206",
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(guido_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "015548b9-819c-49ba-a13d-7777662a7414",
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(guido_image.swapaxes(0, 1))"
]
},
{
"cell_type": "markdown",
"id": "7c4c00de-f16f-4aac-ae37-e0b9df0eb6c2",
"metadata": {},
"source": [
"#### 普通函数矢量化"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa07c8bc-9558-4d2c-b12c-b0e3814cbb48",
"metadata": {},
"outputs": [],
"source": [
"# 面试官讲一讲Python语言中的装饰器\n",
"# 用一个函数去装饰另一个函数或者一个类并为其提供额外的能力(横切关注功能)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "02e4d3e6-9bc7-462e-8be4-800a1dcdc632",
"metadata": {},
"outputs": [],
"source": [
"# 面试题:写一个装饰器,如果原函数返回字符串,那么将字符串每个单词首字母大写\n",
"from functools import wraps\n",
"\n",
"\n",
"def titlize_str(func):\n",
"\n",
" @wraps(func)\n",
" def wrapper(*args, **kwargs):\n",
" result = func(*args, **kwargs)\n",
" if isinstance(result, str):\n",
" result = result.title()\n",
" return result\n",
"\n",
" return wrapper"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e798956c-d543-4a50-a12b-f7314b98bf40",
"metadata": {},
"outputs": [],
"source": [
"@titlize_str\n",
"def say_hello(name):\n",
" return 'hello, ' + name"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "82145cd0-bcb2-44b8-90ef-ac461e2120bd",
"metadata": {},
"outputs": [],
"source": [
"# 如果不使用@语法糖(便捷语法),也可以通过下面的方式应用装饰器\n",
"# say_hello = titlize_str(say_hello)\n",
"# say_hello('tom')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "108ca8a5-ec63-458b-b95b-df759b68cb51",
"metadata": {},
"outputs": [],
"source": [
"say_hello('tom')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3d846bc7-718f-4344-b90e-c7803a104887",
"metadata": {},
"outputs": [],
"source": [
"# 获取原函数\n",
"say_hello = say_hello.__wrapped__\n",
"say_hello('tom')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ea620684-3375-4876-b097-49e81bf225c9",
"metadata": {},
"outputs": [],
"source": [
"# 优化代码的执行性能:空间换时间\n",
"from functools import lru_cache\n",
"\n",
"\n",
"@lru_cache(maxsize=128)\n",
"def fib(n):\n",
" \"\"\"获取第n个斐波那契数\"\"\"\n",
" if n in (1, 2):\n",
" return 1\n",
" return fib(n - 1) + fib(n - 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9f72372e-9cc9-4751-8ee7-e8d9f40727bd",
"metadata": {},
"outputs": [],
"source": [
"for i in range(1, 121):\n",
" print(i, fib(i))"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "29d231a2-57cd-4786-85cd-1366f5378185",
"metadata": {},
"outputs": [],
"source": [
"# 通过vectorize装饰器将普通函数做矢量化处理\n",
"@np.vectorize\n",
"def fac(n):\n",
" if n == 0:\n",
" return 1\n",
" return n * fac(n - 1)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "7c04cc06-ba86-4b1b-a4e1-75e4527f6dde",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4, 5, 6, 7, 8])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp = np.arange(1, 9)\n",
"temp"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "38e8026a-6e75-4e1b-a646-98c86736797f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 2, 6, 24, 120, 720, 5040, 40320])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fac(temp)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "4ac8bb35-87f0-44d0-930e-3fc0c5fb63c3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([26, 68, 73, 33, 64, 54, 26, 40, 60, 36]),\n",
" array([37, 56, 65, 30, 57, 36, 61, 54, 34, 52]))"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1 = np.random.randint(20, 80, 10)\n",
"x2 = np.random.randint(30, 70, 10)\n",
"x1, x2"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "4baced9b-fee2-4c2b-b7ca-dc5914b120b9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 4, 1, 3, 1, 18, 1, 2, 2, 4])"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from math import gcd, lcm\n",
"\n",
"gcd = np.vectorize(gcd)\n",
"gcd(x1, x2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "08c6a075-8b61-4716-92c5-126cd78108c1",
"metadata": {},
"outputs": [],
"source": [
"lcm = np.vectorize(lcm)\n",
"lcm(x1, x2)"
]
},
{
"cell_type": "markdown",
"id": "859bba4b-a0cf-4140-a8de-b2f3fffcd355",
"metadata": {},
"source": [
"### 广播机制\n",
"\n",
"两个形状shape属性不一样的数组如果要做运算要先通过广播机制使其形状一样才能运算。<br>\n",
"如果要执行广播机制使得两个数组形状一样,需要满足以下两个条件其中一个:\n",
"\n",
"1. 两个数组后缘维度shape属性从后往前看对应的部分相同。\n",
"2. 两个数组后缘维度不同但是其中一方为1。"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3339c56a-b68c-401e-a27c-134be60ccf14",
"metadata": {},
"outputs": [],
"source": [
"temp1 = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]])\n",
"temp2 = np.array([1, 2, 3])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ecc9498-792c-4de1-a120-585003b19087",
"metadata": {},
"outputs": [],
"source": [
"temp1 + temp2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cb20c46-3f87-4346-80ab-7e2786ca1475",
"metadata": {},
"outputs": [],
"source": [
"temp3 = np.array([[1], [2], [3], [4]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58932222-bb83-43b7-8cef-b9574898dbb5",
"metadata": {},
"outputs": [],
"source": [
"temp1 + temp3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "74b6c376-05ac-4049-a4b9-5f0614de780e",
"metadata": {},
"outputs": [],
"source": [
"temp4 = np.array([1 ,2, 3])\n",
"temp5 = np.array([[3], [2], [1]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eefe6354-4614-4dfb-aa94-3d146b770b3b",
"metadata": {},
"outputs": [],
"source": [
"temp4.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "04dd44a5-6bea-41eb-aad0-b0a18d64a512",
"metadata": {},
"outputs": [],
"source": [
"temp5.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "764eb0c3-991d-4a5f-a4eb-7da2c0b445bd",
"metadata": {},
"outputs": [],
"source": [
"temp4 + temp5"
]
},
{
"cell_type": "markdown",
"id": "8f1022cb-c07a-4149-aafd-9f53d235da4f",
"metadata": {},
"source": [
"### 矩阵"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8a16c29a-088a-473b-b856-3dbd6660f9cd",
"metadata": {},
"outputs": [],
"source": [
"m1 = np.array([[1, 0, 2], [-1, 3, 1]])\n",
"m2 = np.array([[3, 1], [2, 1], [1, 0]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "74a54196-ca95-4425-9212-62869795aed7",
"metadata": {},
"outputs": [],
"source": [
"m1.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1afbceea-782d-49c9-b6d4-d0848f3fdd99",
"metadata": {},
"outputs": [],
"source": [
"m2.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc65a37a-84eb-4aac-b63f-5625a0c15a3a",
"metadata": {},
"outputs": [],
"source": [
"m1 @ m2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67be1c96-a20c-4862-87cb-8579d3a303f5",
"metadata": {},
"outputs": [],
"source": [
"np.matmul(m1, m2)"
]
},
{
"cell_type": "markdown",
"id": "5fd602a8-61fc-4c5c-94a6-a930bcf6fb2f",
"metadata": {},
"source": [
"$$\n",
"\\begin{cases}\n",
"x_1 + 2x_2 + x_3 = 8 \\\\\n",
"3x_1 + 7x_2 + 2x_3 = 23 \\\\\n",
"2x_1 + 2x_2 + x_3 = 9\n",
"\\end{cases}\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "d41b4856-79c3-4f48-9d8e-58c8d6045884",
"metadata": {},
"source": [
"$$\n",
"\\boldsymbol{A} = \\begin{bmatrix}\n",
"1 & 2 & 1\\\\\n",
"3 & 7 & 2\\\\\n",
"2 & 2 & 1\n",
"\\end{bmatrix}, \\quad\n",
"\\boldsymbol{x} = \\begin{bmatrix}\n",
"x_1 \\\\\n",
"x_2\\\\\n",
"x_3\n",
"\\end{bmatrix}, \\quad\n",
"\\boldsymbol{b} = \\begin{bmatrix}\n",
"8 \\\\\n",
"23\\\\\n",
"9\n",
"\\end{bmatrix}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "edb85b44-50b3-4115-8539-5023a19bb2a1",
"metadata": {},
"outputs": [],
"source": [
"m3 = np.arange(1, 10, dtype='f8').reshape(3, 3)\n",
"m3[-1, -1] = 8\n",
"m3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0b90dbf4-05f7-43ad-a37d-20d48d03dd3a",
"metadata": {},
"outputs": [],
"source": [
"# 计算矩阵的秩\n",
"np.linalg.matrix_rank(m3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a442ab40-97a9-4cf5-8d61-bfcaa3561655",
"metadata": {},
"outputs": [],
"source": [
"# 逆矩阵 - 奇异矩阵不能求逆矩阵\n",
"# LinAlgError: Singular matrix\n",
"np.linalg.inv(m3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19be7ff8-6a71-40ad-8e37-20008c56be7a",
"metadata": {},
"outputs": [],
"source": [
"# 有唯一解决的条件:系数矩阵的秩等于增广矩阵的秩,同时跟未知数的个数相同。\n",
"# 秩rank线性无关的行或者列的数量。\n",
"# 线性相关:一个向量可以通过其他向量做线性变换(数乘和加法)得到,那么它们就是线性相关的。"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87043db3-e2bd-4a70-950a-d74163afc4d1",
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1, 2, 1], [3, 7, 2], [2, 2, 1]])\n",
"b = np.array([8, 23, 9]).reshape(-1, 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "479d272b-2b46-4374-93f3-54e13af52d59",
"metadata": {},
"outputs": [],
"source": [
"# 系数矩阵的秩\n",
"np.linalg.matrix_rank(A)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "faa35591-09da-4232-9b38-72ee2fb824cc",
"metadata": {},
"outputs": [],
"source": [
"# 增广矩阵的秩\n",
"np.linalg.matrix_rank(np.hstack((A, b)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8d02dee-7d86-4f9d-8e33-77b5a76784a3",
"metadata": {},
"outputs": [],
"source": [
"# 解线性方程组\n",
"np.linalg.solve(A, b)"
]
},
{
"cell_type": "markdown",
"id": "336ee288-5be1-41e5-89cb-e22f465efdd2",
"metadata": {},
"source": [
"$$\n",
"A \\cdot x = b\n",
"$$\n",
"$$\n",
"A^{-1} \\cdot A \\cdot x = A^{-1} \\cdot b\n",
"$$\n",
"$$\n",
"I \\cdot x = A^{-1} \\cdot b\n",
"$$\n",
"$$\n",
"x = A^{-1} \\cdot b\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c2bd2fd-8867-4dad-8bbc-b5b175f690b7",
"metadata": {},
"outputs": [],
"source": [
"# 通过逆矩阵解线性方程组\n",
"np.linalg.inv(A) @ b"
]
},
{
"cell_type": "markdown",
"id": "b876a47b-a2ab-497a-b564-69750ddb8666",
"metadata": {},
"source": [
"#### 补充 - 用矩阵运算实现图像处理"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "95e138b5-ed1d-40c3-acbc-821b6ae8cf41",
"metadata": {},
"outputs": [],
"source": [
"# 安装opencv库\n",
"# %pip install opencv-python"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "669c59e5-a477-4bf3-b87b-7486e9d2e9ef",
"metadata": {},
"outputs": [],
"source": [
"def basic_matrix(translation):\n",
" \"\"\"基础变换矩阵\"\"\"\n",
" return np.array([[1, 0, translation[0]], [0, 1, translation[1]], [0, 0, 1]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0338ad44-3142-428f-b1b6-45e691962900",
"metadata": {},
"outputs": [],
"source": [
"import copy\n",
"\n",
"def adjust_transform_for_image(img, trans_matrix):\n",
" \"\"\"根据图像调整变换矩阵\"\"\"\n",
" height, width, *_ = img.shape\n",
" center = np.array([0.5 * width, 0.5 * height])\n",
" return basic_matrix(center) @ trans_matrix @ basic_matrix(-center)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "368578be-3f94-4a37-be51-c6a5dbce1512",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"\n",
"def apply_transform(img, transform, border_value=(204, 204, 204)):\n",
" \"\"\"仿射变换\"\"\"\n",
" return cv2.warpAffine(\n",
" img,\n",
" transform[:2, :],\n",
" dsize=(img.shape[1], img.shape[0]),\n",
" flags=cv2.INTER_LINEAR,\n",
" borderMode=cv2.BORDER_CONSTANT,\n",
" borderValue=border_value\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "28b22755-4aed-4cd2-a3a2-57bd73f8eb00",
"metadata": {},
"outputs": [],
"source": [
"def apply(img, trans_matrix):\n",
" \"\"\"应用变换\"\"\"\n",
" temp_matrix = adjust_transform_for_image(img, trans_matrix)\n",
" out_img = apply_transform(img, temp_matrix)\n",
" return out_img"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1526fbcd-ad60-4d7c-892c-5c0ae2b7ef3f",
"metadata": {},
"outputs": [],
"source": [
"def scale(img, x_ratio, y_ratio):\n",
" \"\"\"缩放\"\"\"\n",
" scale_matrix = np.array([\n",
" [x_ratio, 0, 0], \n",
" [0, y_ratio, 0], \n",
" [0, 0, 1]\n",
" ])\n",
" return apply(img, scale_matrix)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e443448-9b6c-488b-83a5-0de7ac4fca18",
"metadata": {},
"outputs": [],
"source": [
"def rotate(img, degree):\n",
" \"\"\"旋转\"\"\"\n",
" rad = np.deg2rad(degree)\n",
" rotate_matrix = np.array([\n",
" [np.cos(rad), -np.sin(rad), 0], \n",
" [np.sin(rad), np.cos(rad), 0], \n",
" [0, 0, 1]\n",
" ])\n",
" return apply(img, rotate_matrix)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a09187e-2707-49c2-8b8d-84ffa28b6f7e",
"metadata": {},
"outputs": [],
"source": [
"def transvect(img, ratio):\n",
" \"\"\"剪切影射\"\"\"\n",
" transvect_matrix = np.array([\n",
" [1, ratio, 0],\n",
" [0, 1, 0],\n",
" [0, 0, 1]\n",
" ])\n",
" return apply(img, transvect_matrix)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9561e506-3b64-49e6-9e9d-8731a2951646",
"metadata": {},
"outputs": [],
"source": [
"scaled_img = scale(guido_image, 1.25, 0.75)\n",
"plt.imshow(scaled_img)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56cb960b-b3f9-4109-95ba-4388a2b1762e",
"metadata": {},
"outputs": [],
"source": [
"rotated_img = rotate(guido_image, -45)\n",
"plt.imshow(rotated_img)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc2ac604-0fd7-43d5-be18-407b78e46f58",
"metadata": {},
"outputs": [],
"source": [
"transvected_img = transvect(guido_image, -0.3)\n",
"plt.imshow(transvected_img)"
]
},
{
"cell_type": "markdown",
"id": "cd91252a-31d4-40d5-82ef-71f22f0bd39c",
"metadata": {},
"source": [
"#### 补充 - 用scipy处理图像"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9052864d-a32d-4cd3-9e2a-8df1654b8a67",
"metadata": {},
"outputs": [],
"source": [
"from scipy.ndimage import gaussian_filter, sobel\n",
"\n",
"# 获取灰度图\n",
"guido_image = plt.imread('res/guido.jpg')\n",
"gray_image = np.mean(guido_image, axis=2)\n",
"\n",
"plt.figure(figsize=(12, 4))\n",
"\n",
"# 灰度图\n",
"plt.subplot(1, 4, 1)\n",
"plt.imshow(gray_image, cmap=plt.cm.gray)\n",
"\n",
"# 模糊和锐化\n",
"plt.subplot(1, 4, 2)\n",
"blurred_image = gaussian_filter(gray_image, 3)\n",
"plt.imshow(blurred_image, cmap=plt.cm.gray)\n",
"\n",
"plt.subplot(1, 4, 3)\n",
"filtered_image = gaussian_filter(blurred_image, 1)\n",
"sharpen_image = blurred_image + 32 * (blurred_image - filtered_image)\n",
"plt.imshow(sharpen_image, cmap=plt.cm.gray)\n",
"\n",
"# 边缘图\n",
"plt.subplot(1, 4, 4)\n",
"# 使用索贝尔算子(邻点灰度加权差)进行边缘检测\n",
"edge_image = sobel(gray_image)\n",
"plt.imshow(edge_image, cmap=plt.cm.gray)\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dbbc6edf-c643-4cb4-a98b-05e806413478",
"metadata": {},
"outputs": [],
"source": [
"from scipy.ndimage import rotate, zoom\n",
"\n",
"plt.figure(figsize=(12, 4))\n",
"\n",
"# 旋转\n",
"plt.subplot(1, 3, 1)\n",
"rotated_image = rotate(guido_image, -16, reshape=True)\n",
"plt.imshow(rotated_image)\n",
"\n",
"# 旋转\n",
"plt.subplot(1, 3, 2)\n",
"rotated_image = rotate(guido_image, -16, reshape=False)\n",
"plt.imshow(rotated_image)\n",
"\n",
"# 缩放\n",
"plt.subplot(1, 3, 3)\n",
"scaled_image = zoom(guido_image, zoom=(0.8, 1.25, 1))\n",
"plt.imshow(scaled_image)\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "331862d6-60cd-442f-97a4-bbbfbabfc3fa",
"metadata": {},
"source": [
"#### 补充 - 视频流人脸识别"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "383fa323-db82-4a8b-a13b-9cbe9b8c48d3",
"metadata": {},
"outputs": [],
"source": [
"# 安装face_recognition库\n",
"# %pip install face_recognition"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c1cf7b8-9d89-43d4-8954-616bce780183",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import face_recognition\n",
"# from PIL import Image\n",
"\n",
"plt.figure(figsize=(12, 8))\n",
"\n",
"image = face_recognition.load_image_file('res/Solvay.jpg')\n",
"locations = face_recognition.face_locations(image)\n",
"for location in locations:\n",
" top, right, bottom, left = location\n",
" # Image.fromarray(image[top:bottom, left:right]).show()\n",
" cv2.rectangle(image, (left, top), (right, bottom), (255, 0, 0), 2)\n",
"plt.imshow(image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa76d9c2-c8c3-4cec-8c91-b6b54fe6e32e",
"metadata": {},
"outputs": [],
"source": [
"# import cv2\n",
"# import face_recognition\n",
"# import numpy as np\n",
"\n",
"# # 获取摄像头\n",
"# video_capture = cv2.VideoCapture(0)\n",
"\n",
"# # 加载图片获取脸部特征\n",
"# obama_image = face_recognition.load_image_file(\"res/obama.jpg\")\n",
"# obama_face_encoding = face_recognition.face_encodings(obama_image)[0]\n",
"# luohao_image = face_recognition.load_image_file(\"res/luohao.png\")\n",
"# luohao_face_encoding = face_recognition.face_encodings(luohao_image)[0]\n",
"# guido_image = face_recognition.load_image_file(\"res/guido.jpg\")\n",
"# guido_face_encoding = face_recognition.face_encodings(guido_image)[0]\n",
"\n",
"# # 保存脸部特征和对应的名字\n",
"# known_face_encodings = [\n",
"# obama_face_encoding,\n",
"# luohao_face_encoding,\n",
"# guido_face_encoding\n",
"# ]\n",
"# known_face_names = [\n",
"# \"Barack\",\n",
"# \"Hao\",\n",
"# \"Guido\"\n",
"# ]\n",
"\n",
"# face_locations = []\n",
"# face_encodings = []\n",
"# face_names = []\n",
"# process_this_frame = True\n",
"\n",
"# while True:\n",
"# # 从视频中读取一帧数据\n",
"# ret, frame = video_capture.read()\n",
"\n",
"# # 调整为原始尺寸的四分之一(加速处理)\n",
"# small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)\n",
"\n",
"# # BGR转成RGB\n",
"# rgb_small_frame = small_frame[:, :, ::-1]\n",
"\n",
"# if process_this_frame:\n",
"# # 找到所有的人脸位置和脸部特征保存在列表中\n",
"# face_locations = face_recognition.face_locations(rgb_small_frame)\n",
"# face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)\n",
"\n",
"# face_names = []\n",
"# for face_encoding in face_encodings:\n",
"# # 比较脸部特征\n",
"# matches = face_recognition.compare_faces(known_face_encodings, face_encoding)\n",
"# name = \"Unknown\"\n",
"\n",
"# # 通过距离判定最佳匹配并获取对应的名字\n",
"# face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)\n",
"# best_match_index = np.argmin(face_distances)\n",
"# if matches[best_match_index]:\n",
"# name = known_face_names[best_match_index]\n",
"\n",
"# face_names.append(name)\n",
"\n",
"# process_this_frame = not process_this_frame\n",
"\n",
"# # 显示结果\n",
"# for (top, right, bottom, left), name in zip(face_locations, face_names):\n",
"# # 恢复正常的尺寸\n",
"# top, right, bottom, left = top * 4, right * 4, bottom * 4, left * 4\n",
"# # 绘制一个标识人脸的矩形框\n",
"# cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)\n",
"# # 绘制一个填写名字的矩形框\n",
"# cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)\n",
"# # 绘制识别出的人脸对应的名字\n",
"# cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255), 1)\n",
" \n",
"# cv2.imshow('Video', frame)\n",
" \n",
"# # 按键盘上的q键退出窗口 \n",
"# if cv2.waitKey(1) & 0xFF == ord('q'):\n",
"# break\n",
"\n",
"# video_capture.release()\n",
"# cv2.destroyAllWindows()"
]
},
{
"cell_type": "markdown",
"id": "b6989fb7-a3ab-4889-80be-f4ecce033e5c",
"metadata": {},
"source": [
"### 多项式"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c719ca9-de74-4c2e-aaf2-d0a9c833f512",
"metadata": {},
"outputs": [],
"source": [
"# NumPy老版本用poly1d表示多项式\n",
"p1 = np.poly1d([3, 0, 2, 1])\n",
"p2 = np.poly1d([1, 2, 3])\n",
"print(p1)\n",
"print(p2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6aab279-fa9f-4d4e-b597-b3c51b478777",
"metadata": {},
"outputs": [],
"source": [
"# 多项式加法\n",
"print(p1 + p2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "375a1989-7231-432c-bf33-06fce490cacf",
"metadata": {},
"outputs": [],
"source": [
"# 多项式乘法\n",
"print(p1 * p2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4df5662a-c0bc-434d-b53f-623e09b51a76",
"metadata": {},
"outputs": [],
"source": [
"# 令x=2计算多项式的值\n",
"p2(2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df150df6-9904-45b7-8704-dbb033552dcb",
"metadata": {},
"outputs": [],
"source": [
"# 求导\n",
"print(p1.deriv())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d2d3002c-59d6-4e00-b00f-69f3f6ac6609",
"metadata": {},
"outputs": [],
"source": [
"# 求不定积分\n",
"print(p1.integ())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0965b9e9-42fc-425c-90cb-d265e3f6e42f",
"metadata": {},
"outputs": [],
"source": [
"p3 = np.poly1d([1, 3, 2])\n",
"print(p3)\n",
"# 令多项式等于0求解x\n",
"print(p3.roots)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5bda2d2-0d61-46db-ac48-0f7ec3438660",
"metadata": {},
"outputs": [],
"source": [
"type(p3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a71c4799-183e-4096-9903-7edf9464feb7",
"metadata": {},
"outputs": [],
"source": [
"from numpy.polynomial import Polynomial\n",
"\n",
"# NumPy新版本用Polynomial表示多项式\n",
"p1 = Polynomial([1, 2, 0, 3])\n",
"print(p1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0002c6e3-697b-425c-a17c-802e10f07981",
"metadata": {},
"outputs": [],
"source": [
"print(p1.deriv())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f9dde21-0854-4ae6-9703-9599a4204003",
"metadata": {},
"outputs": [],
"source": [
"print(p1.integ())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb191b28-fb2c-460b-9c87-0041b128ba16",
"metadata": {},
"outputs": [],
"source": [
"# 最高次项\n",
"p1.degree()"
]
},
{
"cell_type": "markdown",
"id": "2d6c3103-c5b0-413f-b2de-324b0394e3ae",
"metadata": {},
"source": [
"### 最小二乘解"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7163cb46-44bb-487c-8dd2-7c530574ecc0",
"metadata": {},
"outputs": [],
"source": [
"# 每月收入\n",
"x = np.array([3200, 4811, 5386, 5564, 6120, 6691, 6906, 7483, 7587, 7890,\n",
" 8090, 8300, 8650, 8835, 8975, 9070, 9100, 9184, 9247, 9313, \n",
" 9465, 9558, 9853, 9938, 10020, 10242, 10343, 10731, 10885, 10990, \n",
" 11100, 11227, 11313, 11414, 11630, 11806, 11999, 12038, 12400, 12547, \n",
" 12890, 13050, 13360, 13850, 14890, 14990, 15500, 16899, 17010, 19880])\n",
"# 每月网购支出\n",
"y = np.array([1761, 882, 1106, 182, 1532, 1978, 2174, 2117, 2134, 1924, \n",
" 2207, 2876, 2617, 2683, 3054, 3277, 3345, 3462, 3401, 3591,\n",
" 3596, 3671, 3829, 3907, 3852, 4288, 4359, 4099, 4300, 4367,\n",
" 5019, 4873, 4674, 5174, 4666, 5797, 5782, 5451, 5487, 5448,\n",
" 6002, 6439, 6309, 6045, 5935, 6928, 7356, 6682, 6672, 6582])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a68a1d0d-0dc9-4437-bed9-6e9aff589df1",
"metadata": {},
"outputs": [],
"source": [
"# 定性分析 - 散点图\n",
"plt.scatter(x, y)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "89030441-eefd-4a02-ad41-ba154dbf87b2",
"metadata": {},
"outputs": [],
"source": [
"from scipy import stats\n",
"\n",
"# 夏皮洛检验(正态性判定)\n",
"stats.shapiro(x), stats.shapiro(y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "93573426-e1c0-4025-8b8f-2d83d69e103c",
"metadata": {},
"outputs": [],
"source": [
"# 定量分析 - 相关系数 - correlation coefficient\n",
"# 皮尔逊相关系数(标准化的协方差 - [-1, 1]\n",
"# 1. 连续值且成对出现\n",
"# 2. 没有异常值\n",
"# 3. 来自于正态总体\n",
"np.corrcoef(x, y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b6e47de-8e3b-4be4-a5e4-f1d95649e6f9",
"metadata": {},
"outputs": [],
"source": [
"# 计算皮尔逊相关系数\n",
"stats.pearsonr(x, y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f8e53b1e-b2b1-4222-a69a-dcbca0041d0e",
"metadata": {},
"outputs": [],
"source": [
"history_data = {key: value for key, value in zip(x, y)}\n",
"len(history_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1530478e-ec70-4f87-828f-1d6798c53c5e",
"metadata": {},
"outputs": [],
"source": [
"data = np.random.randint(1, 100, 15).tolist()\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d246563-86fd-418f-b058-4aae08d9b9c1",
"metadata": {},
"outputs": [],
"source": [
"import heapq\n",
"\n",
"# 通过堆heap结构快速的找到TopN元素\n",
"print(heapq.nsmallest(3, data))\n",
"print(heapq.nlargest(5, data))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "866f9fd9-58aa-4c03-9415-99aa236958d4",
"metadata": {},
"outputs": [],
"source": [
"# 目标:因为月收入和网购支出之间有强相关关系,所以我们可以通过月收入预测网购支出\n",
"# 方法1输入一个月收入找到跟这个收入最接近的N条数据用它们的平均值预测对应的网购支出\n",
"# KNN - k最近邻算法找到k个最近的邻居用这k个邻居的数据来做出预测\n",
"import heapq\n",
"\n",
"\n",
"def predicate_by_knn(income, k=5):\n",
" \"\"\"KNN算法\"\"\"\n",
" keys = heapq.nsmallest(k, history_data, key=lambda x: (x - income) ** 2)\n",
" return np.mean([history_data[key] for key in keys]).round(2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "399b9b6a-6625-4dc2-a09c-c0699c538a46",
"metadata": {},
"outputs": [],
"source": [
"predicate_by_knn(12800)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "db0ee4bf-83f8-489f-a543-80854571da60",
"metadata": {},
"outputs": [],
"source": [
"predicate_by_knn(6800)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e514003-c143-4823-91eb-57f7608f6137",
"metadata": {},
"outputs": [],
"source": [
"predicate_by_knn(20000, k=3)"
]
},
{
"cell_type": "markdown",
"id": "aa44d3ad-aa3f-44db-889a-8a69b9a9be65",
"metadata": {},
"source": [
"回归模型:\n",
"$$ Y = aX + b $$\n",
"\n",
"损失函数:\n",
"$$ MSE = \\frac{1} {N} \\sum (\\hat{y_i} - y_i)^2 $$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "032eb740-ac9a-4526-9eb4-b6770907b07a",
"metadata": {},
"outputs": [],
"source": [
"# MSE - Mean Squared Error\n",
"def get_loss(a, b):\n",
" \"\"\"损失函数\"\"\"\n",
" y_hat = a * x + b\n",
" return np.mean((y_hat - y) ** 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d56ccc4-5a0d-4b3a-89ab-01cf690c4fba",
"metadata": {},
"outputs": [],
"source": [
"# 蒙特卡洛模拟(随机瞎蒙法)\n",
"import random\n",
"\n",
"min_loss = np.inf\n",
"ba, bb = None, None\n",
"\n",
"for _ in range(10000):\n",
" a = random.random() * 0.5 + 0.5\n",
" b = random.random() * 1000 - 2000\n",
" curr_loss = get_loss(a, b)\n",
" if curr_loss < min_loss:\n",
" min_loss = curr_loss\n",
" ba, bb = a, b\n",
" print(min_loss)\n",
"print(ba, bb)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af5f7d02-f921-4712-82e3-1ce43c86c708",
"metadata": {},
"outputs": [],
"source": [
"plt.scatter(x, y)\n",
"plt.plot(x, ba * x + bb, color='r', linewidth=4)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "207c2da0-371f-40ea-b19f-f56cca8a7d30",
"metadata": {},
"outputs": [],
"source": [
"def predicate_by_regression(income):\n",
" return round(ba * income + bb, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9e3af32-654c-4704-9f32-cfe41f067d06",
"metadata": {},
"outputs": [],
"source": [
"predicate_by_regression(6800)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5133079a-59f8-4435-af1e-fb5bc13c457d",
"metadata": {},
"outputs": [],
"source": [
"predicate_by_regression(12800)"
]
},
{
"cell_type": "markdown",
"id": "200b9821-b8c8-41df-ae8e-d96bf5049415",
"metadata": {},
"source": [
"将回归模型带入损失函数:\n",
"$$ f(a, b) = \\frac {1} {N} \\sum_{i=1}^{N}(y_i - (ax_i + b))^2 $$\n",
"\n",
"如何让$f(a, b)$取到最小值???\n",
"\n",
"求偏导数并令其等于0。\n",
"$$ \\frac {\\partial {f(a, b)}} {\\partial {a}} = \\frac {2} {N} \\sum_{i=1}^{N}(-x_iy_i + x_i^2a + x_ib) = 0 $$ \n",
"$$ \\frac {\\partial {f(a, b)}} {\\partial {b}} = \\frac {2} {N} \\sum_{i=1}^{N}(-y_i + x_ia + b) = 0 $$\n",
"\n",
"求解得到:\n",
"$$a = \\frac{\\sum(x_{i} - \\bar{x})(y_{i} - \\bar{y})}{\\sum(x_{i} - \\bar{x})^{2}}$$\n",
"$$b = \\bar{y} - a\\bar{x}$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ab79db5-85c7-44ad-8ffa-2684fb499f7a",
"metadata": {},
"outputs": [],
"source": [
"x_bar, y_bar = np.mean(x), np.mean(y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "332777b5-bc3f-44d2-bd54-f8e9a19497aa",
"metadata": {},
"outputs": [],
"source": [
"ba = np.dot((x - x_bar), (y - y_bar)) / np.sum((x - x_bar) ** 2)\n",
"bb = y_bar - ba * x_bar\n",
"ba, bb"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "795f6a40-136a-4772-9e2b-5798022e408d",
"metadata": {},
"outputs": [],
"source": [
"plt.scatter(x, y)\n",
"plt.plot(x, ba * x + bb, color='r', linewidth=4)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2bfac239-7bf0-4784-a124-bbfdb4cdafe8",
"metadata": {},
"outputs": [],
"source": [
"# 拟合出一个线性回归模型\n",
"np.polyfit(x, y, deg=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d5adc92-73e2-40df-843b-02efb28c6ae3",
"metadata": {},
"outputs": [],
"source": [
"# 拟合出一个多项式回归模型\n",
"a, b, c = np.polyfit(x, y, deg=2)\n",
"a, b, c"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c42ee939-e4b9-479e-ad73-66e806e29c6c",
"metadata": {},
"outputs": [],
"source": [
"plt.scatter(x, y)\n",
"plt.plot(x, a * x ** 2 + b * x + c, color='r', linewidth=4)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3363f359-3615-4ffa-a040-41e39c83b38f",
"metadata": {},
"outputs": [],
"source": [
"Polynomial.fit(x, y, deg=1).convert().coef"
]
}
],
"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.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}