{
"cells": [
{
"cell_type": "markdown",
"id": "f11bbe2b-7325-4bf0-abfb-4b4e64292145",
"metadata": {},
"source": [
"## NumPy入门\n",
"\n",
"NumPy是Python数据科学三方库中最为重要的基石,提供了数据存储和运算的能力,其他很多跟数据科学相关的库底层都依赖了NumPy。NumPy的核心是名为`ndarray`的数据类型,用来表示任意维度的数组,相较于Python的`list`,它具有以下优势:\n",
"\n",
"1. 有更好的性能,可以利用硬件的并行计算能力和缓存优化,相较于`list`在处理数据的性能上有着数量级的差异。\n",
"2. 功能更加强大,`ndarray`提供了丰富的运算和方法来处理数据,NumPy中还针对数组操作封装了大量的函数。\n",
"3. 向量化操作,NumPy中的函数以及`ndarray`的方法都是对作用于整个数组,无需使用显示的循环,代码更加简单优雅。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "15630f70-be3c-4690-96a6-0b134a685efb",
"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": "8a115f09-0477-4e62-a910-d9284f32fbd1",
"metadata": {},
"outputs": [],
"source": [
"# %save hello.py"
]
},
{
"cell_type": "markdown",
"id": "a8c3b5a1-9ad1-4511-b7f8-beacce89cf69",
"metadata": {},
"source": [
"### 创建数组对象\n",
"\n",
"1. 通过`array`/`asarray`函数将列表处理成数组对象\n",
"2. 通过`arange`函数指定起始值、终止值和跨度创建数组对象\n",
"3. 通过`linspace`函数指定起始值、终止值和元素个数创建等差数列\n",
"4. 通过`logspace`函数指定起始值(指数)、终止值(指数)、元素个数、底数(默认10)创建等比数列\n",
"5. 通过`fromstring`/`fromfile`函数从字符串或文件中读取数据创建数组对象\n",
"6. 通过`fromiter`函数通过迭代器获取数据创建数组对象\n",
"7. 通过生成随机元素的方式创建数组对象\n",
"8. 通过`zeros`/`zeros_like`函数创建全0元素的数组对象\n",
"9. 通过`ones`/`ones_like`函数创建全1元素的数组对象\n",
"10. 通过`full`函数指定元素值创建数组对象\n",
"11. 通过`eye`函数创建单位矩阵\n",
"12. 通过`tile`/`repeat`函数重复元素创建数组对象"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a035b671-2f91-473c-ac2b-e25291cf664b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4, 5], dtype=int32)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 方法一:通过array函数将列表处理成数组对象\n",
"array1 = np.array([1, 2, 3, 4, 5], dtype='i4')\n",
"array1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "28a8c2f7-c197-4d5e-9006-d99242edefee",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(array1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "95eae152-6bfc-4707-bd04-50c7363e9315",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2 = np.array([[1, 2, 3], [4, 5, 6]])\n",
"array2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3c662a6d-d017-4a85-b93c-8538f334db22",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 方法二:通过arange函数指定范围创建数组对象\n",
"array3 = np.arange(1, 10)\n",
"array3"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "954605a1-1004-4595-ac90-523feac7f4e9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49,\n",
" 52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array4 = np.arange(1, 100, 3)\n",
"array4"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e479f343-7033-4b73-a0ce-e7f04444e915",
"metadata": {},
"outputs": [],
"source": [
"# 方法三:通过linspace函数创建等差数列\n",
"array5 = np.linspace(-2 * np.pi, 2 * np.pi, 120)\n",
"array6 = np.sin(array5)\n",
"array7 = np.cos(array5)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "57b0e126-3276-4f10-b1d1-288597842d35",
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_format = 'svg'\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "62a6b0ed-acaf-45a1-90b0-7caf73184d09",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize=(8, 4))\n",
"# 绘制折线图\n",
"plt.plot(array5, array6, marker='.', color='darkgreen')\n",
"plt.plot(array5, array7, marker='.', color='coral')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "fc6ed6f5-6844-47df-8dde-fd9e598af48d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 方法四:通过logspace函数创建等比数列\n",
"array8 = np.logspace(0, 10, num=11, base=2, dtype='i8')\n",
"array8"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f3d2ba81-cf13-4d96-afef-f9221b4b4a68",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 11, 111, 2, 22, 222])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 方法五:通过fromstring/fromfile/fromregex函数从字符串读取数据创建数组\n",
"array9 = np.fromstring('1, 11, 111, 2, 22, 222', sep=',', dtype='i8')\n",
"array9"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1c691969-f93c-4e32-9ba5-ab5e074a6409",
"metadata": {},
"outputs": [],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"\n",
"InteractiveShell.ast_node_interactivity = 'last_expr'"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "d8eb5b58-d129-459f-9969-543191fb1966",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array10 = np.fromfile('res/prime.txt', dtype='i8', sep='\\n', count=15)\n",
"array10"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ff01ce19-fc81-41db-88a2-647299ec940c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 面试官:请说一下Python中的迭代器是什么?它跟生成器是什么关系?\n",
"# 迭代器是实现了迭代器协议的对象。在Python中迭代器协议是两个魔术方法:__iter__、__next__\n",
"# 我们可以通过next函数或者for-in循环从迭代器中获取数据\n",
"# 迭代器的编写相对比较麻烦,所以在Python中可以用创建生成器的方式简化迭代器语法\n",
"\n",
"\n",
"def fib(count):\n",
" a, b = 0, 1\n",
" for _ in range(count):\n",
" a, b = b, a + b\n",
" yield a\n",
"\n",
"\n",
"gen = fib(50)\n",
"gen"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "5a2c15a6-5744-4eb6-8f94-3132f7e0b1b6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 1, 2, 3, 5,\n",
" 8, 13, 21, 34, 55,\n",
" 89, 144, 233, 377, 610,\n",
" 987, 1597, 2584, 4181, 6765,\n",
" 10946, 17711, 28657, 46368, 75025,\n",
" 121393, 196418, 317811, 514229, 832040,\n",
" 1346269, 2178309, 3524578, 5702887, 9227465,\n",
" 14930352, 24157817, 39088169, 63245986, 102334155,\n",
" 165580141, 267914296, 433494437, 701408733, 1134903170,\n",
" 1836311903, 2971215073, 4807526976, 7778742049, 12586269025])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 方法六:通过fromiter函数从迭代器中读取数据创建数组对象\n",
"array11 = np.fromiter(fib(50), dtype='i8')\n",
"array11"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "ca62e6fb-f0c9-4f12-acdb-978507e37f94",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[90, 45, 91, 71],\n",
" [85, 2, 98, 76],\n",
" [58, 50, 72, 13],\n",
" [66, 90, 26, 69],\n",
" [23, 44, 68, 98]])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 方法七:通过生成随机元素创建数组对象\n",
"array12 = np.random.randint(0, 101, (5, 4))\n",
"array12"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "2ec313a2-bdd6-492f-9176-172b1ec54534",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.35742922, 0.49173669, 0.14993948, 0.15556126, 0.48435648,\n",
" 0.57329703, 0.7256331 , 0.96709102, 0.79687864, 0.95782978])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array13 = np.random.random(10)\n",
"array13"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "c8ca9327-c30e-4068-b0bc-c18c49a48c89",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([155., 173., 172., ..., 171., 176., 157.])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array14 = np.random.normal(169, 8.5, 5000).round(0)\n",
"array14"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "e6f0fe78-41bf-45c9-a433-cf3cc40cdc11",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 绘制直方图\n",
"plt.hist(array14, bins=15, color='#6B8A7A')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "88de114b-c346-4150-b33c-f2d14dd84193",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0., 0.],\n",
" [0., 0., 0., 0.],\n",
" [0., 0., 0., 0.],\n",
" [0., 0., 0., 0.],\n",
" [0., 0., 0., 0.]])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 方法八:通过zeros/zeros_like函数创建全0元素的数组对象\n",
"array15 = np.zeros((5, 4))\n",
"array15"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "b03bc9d0-1274-46a4-a9b6-28f634a9d034",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0],\n",
" [0, 0, 0]])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array16 = np.zeros_like(array2)\n",
"array16"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "db9f61e1-d50a-4f7f-a1b4-66798c0976ef",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
" [1., 1., 1., 1.]])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 方法九:通过ones/ones_like函数创建全0元素的数组对象\n",
"array17 = np.ones((5, 4))\n",
"array17"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "07ddd6d3-2e91-4a1b-857d-5f8b6867904d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 1, 1],\n",
" [1, 1, 1]])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array18 = np.ones_like(array2)\n",
"array18"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "6b45e1db-8e1c-4af9-9495-c3a7f06b9311",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[100, 100, 100, 100],\n",
" [100, 100, 100, 100],\n",
" [100, 100, 100, 100],\n",
" [100, 100, 100, 100],\n",
" [100, 100, 100, 100]])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 方法十:通过full函数指定值和形状创建数组对象\n",
"array19 = np.full((5, 4), 100)\n",
"array19"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "1ae4620a-11bd-464b-abc2-83f7f8b7e8ba",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 方法十一:通过eye函数创建单位矩阵\n",
"# identify matrix --> I --> eye\n",
"array20 = np.eye(10)\n",
"array20"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "0ae939c8-0977-42b0-8d8a-351f55b0471e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,\n",
" 3, 3, 3, 3, 3, 3, 3, 3])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 方法十二:通过repeat/tile函数重复元素创建数组对象\n",
"array21 = np.repeat([1, 2, 3], 10)\n",
"array21"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "c1a8d343-a30d-4144-baa8-b000a65c070d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1,\n",
" 2, 3, 1, 2, 3, 1, 2, 3])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array22 = np.tile([1, 2, 3], 10)\n",
"array22"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "96e8c639-1a14-453c-89a5-6fd74ca89a88",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 36, 33, 28],\n",
" [ 36, 33, 28],\n",
" [ 36, 33, 28],\n",
" ...,\n",
" [ 32, 31, 29],\n",
" [ 32, 31, 27],\n",
" [ 31, 32, 26]],\n",
"\n",
" [[ 37, 34, 29],\n",
" [ 38, 35, 30],\n",
" [ 38, 35, 30],\n",
" ...,\n",
" [ 31, 30, 28],\n",
" [ 31, 30, 26],\n",
" [ 30, 31, 25]],\n",
"\n",
" [[ 38, 35, 30],\n",
" [ 38, 35, 30],\n",
" [ 38, 35, 30],\n",
" ...,\n",
" [ 30, 29, 27],\n",
" [ 30, 29, 25],\n",
" [ 29, 30, 25]],\n",
"\n",
" ...,\n",
"\n",
" [[239, 178, 123],\n",
" [237, 176, 121],\n",
" [235, 174, 119],\n",
" ...,\n",
" [ 78, 68, 56],\n",
" [ 76, 66, 54],\n",
" [ 73, 65, 52]],\n",
"\n",
" [[238, 177, 120],\n",
" [236, 175, 118],\n",
" [234, 173, 116],\n",
" ...,\n",
" [ 80, 70, 58],\n",
" [ 78, 68, 56],\n",
" [ 74, 67, 51]],\n",
"\n",
" [[237, 176, 119],\n",
" [236, 175, 118],\n",
" [234, 173, 116],\n",
" ...,\n",
" [ 83, 71, 59],\n",
" [ 81, 69, 57],\n",
" [ 77, 68, 53]]], dtype=uint8)"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 补充:读图片获得一个三维数组对象\n",
"guido_image = plt.imread('res/guido.jpg')\n",
"guido_image"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "dff598ad-ce04-4115-bffc-b70decd6a54e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(750, 500, 3)"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"guido_image.shape"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "b8e6b35a-e9bb-489e-ab54-9fc2874b5708",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.imshow(guido_image)"
]
},
{
"cell_type": "markdown",
"id": "0e6bc238-b4f7-45ed-8d3e-194576f67fa9",
"metadata": {},
"source": [
"### 数组对象的属性\n",
"\n",
"1. `size` - 元素的个数\n",
"2. `dtype` - 元素的数据类型\n",
"3. `ndim` - 数组的维度\n",
"4. `shape` - 数组的形状\n",
"5. `itemsize` - 每个元素占用的内存空间大小(字节)\n",
"6. `nbytes` - 所有元素占用的内存空间大小(字节)\n",
"7. `T` - 转置\n",
"8. `flags` - 内存信息\n",
"9. `base` - 根基"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "699ac4e0-a11a-4f23-9469-052371e6a140",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4, 5], dtype=int32)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "292a05b0-351f-4e6b-8968-aebe3b859b0e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 大小 - 元素个数\n",
"array1.size"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "0b382c41-8a42-4946-8d82-27c959d08cf8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('int32')"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 数据类型\n",
"array1.dtype"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "17c532d9-f1bf-4da9-9a98-4b450997d32b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 维度\n",
"array1.ndim"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "c1d57809-19cd-4540-a23a-05d5d639b98b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5,)"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 形状 - 元组\n",
"array1.shape"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "3376c6e2-ec8e-4743-81dd-2409fd869a52",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 每个元素占用内存空间大小(字节)\n",
"array1.itemsize"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "29f66406-f940-434f-8d03-c0706cfa412b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 所有元素占用内存空间大小(字节)\n",
"array1.nbytes"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "68066920-3062-41cf-9cf7-012850461d70",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "43e80ef1-edcf-45b7-8143-d4a159a71c0b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 4],\n",
" [2, 5],\n",
" [3, 6]])"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2.T"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "ea0cc4f7-b504-458f-acdb-ff79d9730a19",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2.size"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "b268f3dc-4652-4736-8831-b21e1f3e76d8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('int64')"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2.dtype"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "b93e1712-3e62-4464-90e7-d90847e1763e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2.ndim"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "0f17fd54-6969-4104-9d0e-cfc70678e663",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 3)"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2.shape"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "f573369f-07ef-4dea-a46a-a4f5d837ec5f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2.itemsize"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "cc8887de-4600-47f4-beb3-37979ec079f4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"48"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2.nbytes"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "9457f0c9-b433-4ec5-961d-6e8ebedad2db",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
" C_CONTIGUOUS : True\n",
" F_CONTIGUOUS : False\n",
" OWNDATA : True\n",
" WRITEABLE : True\n",
" ALIGNED : True\n",
" WRITEBACKIFCOPY : False"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2.flags"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "22adf5d7-c491-4558-af40-fa1a7fef7d6b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1125000"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"guido_image.size"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "adf08994-775b-4276-8392-95a9da821fb8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('uint8')"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"guido_image.dtype"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "81f03343-beac-4fe2-9b5e-9e72ca4387ae",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"guido_image.ndim"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "2d01a9e3-62f1-4145-83c3-3f16ac647975",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(750, 500, 3)"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"guido_image.shape"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "47576187-1b9b-4c08-b63a-9abe6a1352a9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"guido_image.itemsize"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "7304e015-1f67-4920-a045-baf66c60e9df",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1125000"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"guido_image.nbytes"
]
},
{
"cell_type": "markdown",
"id": "bca272d8-c859-4c9f-ad8f-76b8ba2926bf",
"metadata": {},
"source": [
"### 数组对象的运算\n",
"\n",
"#### 算术运算\n",
"\n",
"1. 与标量运算\n",
"2. 与数组运算 - 两个数组形状相同"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "5bec613e-df64-4a14-82a8-5ffa05d8ec48",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([11, 12, 13, 14, 15], dtype=int32)"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1 + 10"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "c2c2001a-4743-44ba-93e6-31b089196e31",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 5, 10, 15],\n",
" [20, 25, 30]])"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2 * 5"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "575bb5dc-bb20-471e-9237-e500d2f3796a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 4, 9],\n",
" [16, 25, 36]])"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2 ** 2"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "82bd25ad-a422-46cb-aa1c-41dba47fd54b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 1, 3],\n",
" [4, 7, 2]])"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp1 = np.random.randint(1, 10, (2, 3))\n",
"temp1"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "2a2a9ce0-41ef-417b-a082-b1a8b15213c6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2, 3, 6],\n",
" [ 8, 12, 8]])"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp1 + array2"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "af6ac896-2992-4e78-8fcc-9d2a55ccd188",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 9],\n",
" [16, 35, 12]])"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp1 * array2"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "1cf8cb80-3ba2-4b6a-8985-a4b63bdceda2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 1, 27],\n",
" [ 256, 16807, 64]])"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp1 ** array2"
]
},
{
"cell_type": "markdown",
"id": "f6f13e4c-754a-4017-8898-52b00c97a910",
"metadata": {},
"source": [
"#### 比较运算\n",
"\n",
"1. 与标量运算\n",
"2. 与数组运算"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "bd4f7a63-341a-4a8a-9042-a2c286e606c6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([False, False, False, True, True])"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1 > 3"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "68ebdc1a-ca87-439b-9aaf-bc59742c04f0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[False, False, False],\n",
" [ True, True, True]])"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array2 > 3"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "9d292f4f-3067-4fce-85aa-b4787bb90d24",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[False, False, False],\n",
" [False, True, False]])"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp1 > array2"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "ea44e117-566f-4fad-a9ee-6ab380461a75",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, False, True],\n",
" [ True, False, False]])"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp1 == array2"
]
},
{
"cell_type": "markdown",
"id": "5e2ed3d9-72ba-42ac-a1c3-00f469d7a3bc",
"metadata": {},
"source": [
"#### 逻辑运算\n",
"\n",
"1. 与标量的运算\n",
"2. 与数组的运算"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "621c9302-b475-4151-8627-36001424a38d",
"metadata": {},
"outputs": [],
"source": [
"temp2 = np.array([True, False, True, False, True])\n",
"temp3 = np.array([True, False, False, False, True])"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "355c4862-058d-47a0-9d36-331881f26c6e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, False, True, False, True])"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp2 & True"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "711b9eda-9fe2-49b6-903c-3921507abafa",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, True, True, True, True])"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp2 | True"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "0ec28456-84a7-4161-8bc6-765c4410ca7a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, False, False, False, True])"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp2 & temp3"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "11c56fd5-ae20-4e55-96c5-aacf2b4e3df1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, False, True, False, True])"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp2 | temp3"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "afcc3303-0705-42b0-a503-b3efbd68590a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([False, True, False, True, False])"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"~temp2"
]
},
{
"cell_type": "markdown",
"id": "df7b1f3f-4d89-45ea-9982-69ae9069dc8c",
"metadata": {},
"source": [
"#### 索引运算\n",
"\n",
"1. 普通索引 - 跟列表的索引运算类似\n",
"2. 花式索引 - 用列表或数组充当数组的索引\n",
"3. 布尔索引 - 用保存布尔值的数组充当索引\n",
"4. 切片索引 - 跟列表的切片运算类似"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "e845e1b3-d137-4832-b016-ed8d64c18a8f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([42, 49, 40, 75, 55, 99, 44, 80, 74])"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4 = np.random.randint(1, 100, 9)\n",
"temp4"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "a6b49f68-f43b-42be-aa8b-0bbe4ca52f79",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(99)"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4[5]"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "35bc13a1-1ce8-4bf8-ba6a-310d145788da",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(99)"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4[-4]"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "4f53346f-a086-400f-84a7-a7ce264826bd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([42, 49, 40, 75, 55, 99, 44, 80, 74])"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4[5] = 99\n",
"temp4"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "0f6f4528-191c-4ca8-809d-6503d4076a53",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[95, 91, 74, 23, 37],\n",
" [90, 74, 38, 87, 24],\n",
" [ 9, 85, 23, 33, 36],\n",
" [86, 76, 57, 12, 22]])"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5 = np.random.randint(1, 100, (4, 5))\n",
"temp5"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "7bcbf6e0-7e2e-4e41-bab8-cd9b99e67ad3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(38)"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[1][2]"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "a7a9a1c8-ecbc-4e7e-9f4e-3a7e8dadb249",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(38)"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[1, 2]"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "979c64b0-a600-4229-859c-252bb597185d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[95, 91, 74, 23, 37],\n",
" [90, 74, 38, 87, 24],\n",
" [ 9, 85, 23, 33, 36],\n",
" [86, 76, 57, 12, 99]])"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[-1, -1] = 99\n",
"temp5"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "4a5823e6-d3ff-411e-8f06-858dbdac006b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[95, 91, 74, 23, 37],\n",
" [90, 74, 38, 87, 24],\n",
" [ 9, 85, 23, 33, 36],\n",
" [86, 55, 57, 12, 99]])"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[-1, 1] = 55\n",
"temp5"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "6282b8c7-d23a-4079-ad98-88bc606ff93f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[36, 33, 28],\n",
" [36, 33, 28],\n",
" [36, 33, 28],\n",
" ...,\n",
" [32, 31, 29],\n",
" [32, 31, 27],\n",
" [31, 32, 26]], dtype=uint8)"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"guido_image[0]"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "0f7e83f3-4ab2-44a6-b537-c32645fe2abc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([36, 33, 28], dtype=uint8)"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"guido_image[0, 0]"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "03477e7b-04f2-4de3-bd4d-f070b1983304",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.uint8(33)"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"guido_image[0, 0, 1]"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "0e4f6f3f-0cef-4e6a-89d9-d3281f59c5d8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([49, 49, 49, 40, 40, 80, 99, 99])"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 花式索引 - fancy index - 用放整数的列表或者数组充当数组的索引\n",
"temp4[[1, 1, 1, 2, 2, -2, -4, -4]]"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "1ead8a5d-f4c0-4f5a-8709-b072ba676118",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([23, 74, 74, 33, 23, 23, 23])"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[[0, 1, 1, 2, 0, 0, 0], [3, 1, 1, -2, -2, -2, -2]]"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "5e31efc0-8f1a-4a8d-ab85-8e1cc592d5d8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([42, 75, 99, 80])"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 布尔索引 - 用放布尔值的数组或列表充当数组的索引 - 实现数据筛选\n",
"temp4[[True, False, False, True, False, True, False, True, False]]"
]
},
{
"cell_type": "code",
"execution_count": 86,
"id": "6145ef49-423f-40e0-acc7-8a8f897f4fb6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([False, False, False, True, False, True, False, True, True])"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4 > 70"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "d48715c6-c743-4457-9896-211d1ad74f97",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([75, 99, 80, 74])"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4[temp4 > 70]"
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "efe7652f-1a75-4eed-9b33-2d52dfd25626",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, False, True, False, False, False, True, True, True])"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4 % 2 == 0"
]
},
{
"cell_type": "code",
"execution_count": 89,
"id": "4f2d6b1b-259d-4721-8dc6-326be4a73d57",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([42, 40, 44, 80, 74])"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4[temp4 % 2 == 0]"
]
},
{
"cell_type": "code",
"execution_count": 90,
"id": "261f57fd-e06a-4aca-8c84-e70b43b42795",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([False, False, False, False, False, False, False, True, True])"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(temp4 > 70) & (temp4 % 2 == 0)"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "5a52756e-e275-4750-b559-708bbe8fc045",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([80, 74])"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4[(temp4 > 70) & (temp4 % 2 == 0)]"
]
},
{
"cell_type": "code",
"execution_count": 92,
"id": "51be708e-afc5-47b2-9392-6c53738eb7d1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([42, 40, 75, 99, 44, 80, 74])"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4[(temp4 > 70) | (temp4 % 2 == 0)]"
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "93bd6975-bf01-4801-81e7-fc0bf7b21285",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True, True, False, False],\n",
" [ True, True, False, True, False],\n",
" [False, True, False, False, False],\n",
" [ True, False, False, False, True]])"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5 > 70"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "df324e6a-85d1-40de-acce-bfbb35e8a4cf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([95, 91, 74, 90, 74, 87, 85, 86, 99])"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[temp5 > 70]"
]
},
{
"cell_type": "code",
"execution_count": 95,
"id": "6388f55b-40af-4cd1-abbc-29da10641580",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([74, 90, 74, 86])"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[(temp5 > 70) & (temp5 % 2 == 0)]"
]
},
{
"cell_type": "code",
"execution_count": 96,
"id": "e30c750e-5517-4314-b6d1-4fc28c5a454b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([42, 49, 40, 75, 55, 99, 44, 80, 74])"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4"
]
},
{
"cell_type": "code",
"execution_count": 97,
"id": "6e294ace-b236-4554-b34c-6f2b00bd295f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([40, 75, 55, 99, 44])"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 切片索引 - slice\n",
"temp4[2:7]"
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "1519ca3a-9844-4b20-a00f-96b61780d998",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([40, 55, 44])"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 切片索引 - slice\n",
"temp4[2:7:2]"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "9b96d847-5aec-4e7f-b6a7-48f4deecf454",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([44, 99, 55, 75, 40])"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp4[6:1:-1]"
]
},
{
"cell_type": "code",
"execution_count": 100,
"id": "5dbcfa29-9930-471e-81d3-100f22e6293d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[95, 91, 74, 23, 37],\n",
" [90, 74, 38, 87, 24],\n",
" [ 9, 85, 23, 33, 36],\n",
" [86, 55, 57, 12, 99]])"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5"
]
},
{
"cell_type": "code",
"execution_count": 101,
"id": "a1307d26-d1a3-4201-9ffa-314a81300712",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[74, 38, 87],\n",
" [85, 23, 33]])"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[1:3, 1:4]"
]
},
{
"cell_type": "code",
"execution_count": 102,
"id": "e95ddb0d-ee31-4690-a91b-a0f0137ce07a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[33, 36],\n",
" [12, 99]])"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[2:, 3:]"
]
},
{
"cell_type": "code",
"execution_count": 103,
"id": "d69e9936-049a-4a66-af22-bb7feff1b9e7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[23, 33],\n",
" [57, 12]])"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[2:, 2:4]"
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "befe1ce3-4742-4fd9-97de-3d9608ccf4c1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[95, 91, 74],\n",
" [90, 74, 38],\n",
" [ 9, 85, 23]])"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[:3, :3]"
]
},
{
"cell_type": "code",
"execution_count": 105,
"id": "ba3fcdcd-f999-41d4-9e96-858dc0f3d70d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[95, 91, 74],\n",
" [90, 74, 38],\n",
" [ 9, 85, 23],\n",
" [86, 55, 57]])"
]
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp5[:, :3]"
]
},
{
"cell_type": "code",
"execution_count": 106,
"id": "753b5012-78fd-4a21-997e-132c5a15f636",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgAAAABACAYAAABsv8+/AAAAE3RFWHRUaXRsZQBncmF5IGNvbG9ybWFw9iBr6wAAABl0RVh0RGVzY3JpcHRpb24AZ3JheSBjb2xvcm1hcH2S+3MAAAAwdEVYdEF1dGhvcgBNYXRwbG90bGliIHYzLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZ2GZxVMAAAAydEVYdFNvZnR3YXJlAE1hdHBsb3RsaWIgdjMuOS4yLCBodHRwczovL21hdHBsb3RsaWIub3JnTz9adAAAAUBJREFUeJzt1rENQjEQBcFn998zRBRA8CXEziQO7POle7a9tu2cs/L5ce9N3n/7zvx/zj/9n/322/87++8AgBwBAABBAgAAggQAAAQJAAAIEgAAECQAACBIAABAkAAAgCABAABBAgAAggQAAAQJAAAIEgAAECQAACBIAABAkAAAgCABAABBAgAAggQAAAQJAAAIEgAAECQAACBIAABAkAAAgCABAABBAgAAggQAAAQJAAAIEgAAECQAACBIAABAkAAAgCABAABBAgAAggQAAAQJAAAIEgAAECQAACBIAABAkAAAgCABAABBAgAAggQAAAQJAAAIEgAAECQAACBIAABAkAAAgCABAABBAgAAggQAAAQJAAAIEgAAECQAACBIAABAkAAAgCABAABBAgAAggQAAAQJAAAIEgAAECQAACDoDY2LBHzusuGnAAAAAElFTkSuQmCC",
"text/html": [
"gray

"
],
"text/plain": [
""
]
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plt.get_cmap('gray')"
]
},
{
"cell_type": "code",
"execution_count": 107,
"id": "2de0bf4c-61f4-48d9-a12b-9accbb883962",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[False, False, False, ..., False, False, False],\n",
" [False, False, False, ..., False, False, False],\n",
" [False, False, False, ..., False, False, False],\n",
" ...,\n",
" [ True, True, True, ..., False, False, False],\n",
" [ True, True, True, ..., False, False, False],\n",
" [ True, True, True, ..., False, False, False]])"
]
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(guido_image, axis=2) >= 128"
]
},
{
"cell_type": "code",
"execution_count": 138,
"id": "780b60d2-c634-4294-baea-00e696143942",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 创建画布\n",
"plt.figure(figsize=(15, 9))\n",
"\n",
"# 原图\n",
"# 创建坐标系\n",
"plt.subplot(2, 4, 1)\n",
"plt.imshow(guido_image)\n",
"# 垂直翻转\n",
"plt.subplot(2, 4, 2)\n",
"plt.imshow(guido_image[::-1])\n",
"# 水平翻转\n",
"plt.subplot(2, 4, 3)\n",
"plt.imshow(guido_image[:, ::-1])\n",
"# 抠图\n",
"plt.subplot(2, 4, 4)\n",
"plt.imshow(guido_image[30:350, 80:310])\n",
"# 降采样\n",
"plt.subplot(2, 4, 5)\n",
"plt.imshow(guido_image[::10, ::10])\n",
"# 反色\n",
"plt.subplot(2, 4, 6)\n",
"plt.imshow(guido_image[:, :, ::-1])\n",
"# 灰度图\n",
"plt.subplot(2, 4, 7)\n",
"plt.imshow(guido_image[:, :, 0], cmap=plt.cm.gray)\n",
"# 二值化\n",
"plt.subplot(2, 4, 8)\n",
"plt.imshow(np.mean(guido_image, axis=2) >= 128, cmap='gray')\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 154,
"id": "abe85cca-abf4-4805-9e69-b2971021e741",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 局部马赛克效果\n",
"guido_image_copy = guido_image.copy()\n",
"\n",
"n = 12\n",
"\n",
"for i in range(120, 350, n):\n",
" for j in range(120, 310, n):\n",
" color = guido_image_copy[i, j]\n",
" guido_image_copy[i: i + n, j: j + n] = color\n",
"\n",
"plt.imshow(guido_image_copy)"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "0e1f177e-db9d-4585-8c15-2cb5a928ccd4",
"metadata": {},
"outputs": [],
"source": [
"# %pip install pillow"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "0143bdbe-afde-4741-8d11-2bcbe34de477",
"metadata": {},
"outputs": [],
"source": [
"# from PIL import Image\n",
"\n",
"# 灰度图\n",
"# Image.fromarray(guido_image[:, :, 0]).show()"
]
},
{
"cell_type": "code",
"execution_count": 112,
"id": "487ec2f5-97bc-413f-9a06-c8f6875ebab8",
"metadata": {},
"outputs": [],
"source": [
"# from PIL import ImageFilter\n",
"\n",
"# 滤镜效果\n",
"# Image.fromarray(guido_image).filter(ImageFilter.CONTOUR).show()"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "15aa7ec2-8bff-45fb-89b8-19790838aff3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(750, 500, 3)"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"obama_image = plt.imread('res/obama.jpg')\n",
"obama_image.shape"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "e1df160a-3e97-4594-a0ba-43bbd3c384ae",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.imshow(obama_image)"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "6c57d787-8a35-4a56-8b10-ceaa08b49612",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(750, 500, 3)"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp6 = (guido_image * 0.6 + obama_image * 0.4).astype('u1')\n",
"temp6.shape"
]
},
{
"cell_type": "code",
"execution_count": 116,
"id": "54ece4ed-4346-458a-8b57-58a9930c6dce",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.imshow(temp6)"
]
},
{
"cell_type": "code",
"execution_count": 117,
"id": "a56d7ce5-ec59-4d35-bbfd-52b33069c6f5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"temp7 = np.random.randint(0, 256, (16, 16, 3))\n",
"plt.imshow(temp7)"
]
},
{
"cell_type": "markdown",
"id": "6649825a-9c1e-4190-adc4-a98c8c97a553",
"metadata": {},
"source": [
"### 数组对象的方法\n",
"\n",
"1. 获取描述性统计信息\n",
" - `sum`\n",
" - `cumsum` / `cumprod`\n",
" - `mean`\n",
" - `np.median`\n",
" - `stats.mode`\n",
" - `max`\n",
" - `min`\n",
" - `ptp`\n",
" - `np.quantile` / `stats.iqr`\n",
" - `var`\n",
" - `std`\n",
" - `stats.variation`\n",
" - `stats.skew`\n",
" - `stats.kurtosis`\n",
"2. 其他相关方法\n",
" - `round`\n",
" - `argmax` / `argmin`\n",
" - `nonzero`\n",
" - `copy` / `view`\n",
" - `astype`\n",
" - `clip`\n",
" - `reshape` / `resize`\n",
" - `dump` / `np.load`\n",
" - `tofile`\n",
" - `fill`\n",
" - `flatten` / `ravel`\n",
" - `sort` / `argsort`\n",
" - `swapaxes` / `transpose`\n",
" - `tolist`"
]
},
{
"cell_type": "code",
"execution_count": 118,
"id": "22e444ec-9b9e-4807-986a-1b3b229d87af",
"metadata": {},
"outputs": [],
"source": [
"# %pip install -U scipy"
]
},
{
"cell_type": "code",
"execution_count": 119,
"id": "4d7745c2-30fa-4a15-bae5-39b9597c1462",
"metadata": {},
"outputs": [],
"source": [
"from scipy import stats"
]
},
{
"cell_type": "code",
"execution_count": 120,
"id": "d8b8fbbc-43d5-467b-a32a-116639baedac",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([76, 81, 85, 79, 83, 82, 91, 80, 87, 86, 70, 82, 84, 77, 83, 85, 76,\n",
" 74, 80, 80, 82, 76, 68, 77, 80, 78, 77, 73, 81, 76, 85, 81, 84, 85,\n",
" 74, 84, 70, 76, 78, 80, 86, 75, 94, 79, 84, 78, 72, 86, 74, 68])"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scores1 = np.fromstring(\n",
" '76, 81, 85, 79, 83, 82, 91, 80, 87, 86, '\n",
" '70, 82, 84, 77, 83, 85, 76, 74, 80, 80, '\n",
" '82, 76, 68, 77, 80, 78, 77, 73, 81, 76, '\n",
" '85, 81, 84, 85, 74, 84, 70, 76, 78, 80, '\n",
" '86, 75, 94, 79, 84, 78, 72, 86, 74, 68', \n",
" sep=',',\n",
" dtype='i8'\n",
")\n",
"scores1"
]
},
{
"cell_type": "code",
"execution_count": 121,
"id": "7bb13bb7-ba31-459c-85ce-ef0dc85abd96",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(3982)"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 求和\n",
"scores1.sum()"
]
},
{
"cell_type": "code",
"execution_count": 122,
"id": "f7d4eb31-33f0-436e-a53b-98276d22ddef",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(3982)"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sum(scores1)"
]
},
{
"cell_type": "code",
"execution_count": 123,
"id": "b450c23a-26b8-4766-b15a-5470efb8e37a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 76, 157, 242, 321, 404, 486, 577, 657, 744, 830, 900,\n",
" 982, 1066, 1143, 1226, 1311, 1387, 1461, 1541, 1621, 1703, 1779,\n",
" 1847, 1924, 2004, 2082, 2159, 2232, 2313, 2389, 2474, 2555, 2639,\n",
" 2724, 2798, 2882, 2952, 3028, 3106, 3186, 3272, 3347, 3441, 3520,\n",
" 3604, 3682, 3754, 3840, 3914, 3982])"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 累积和 - cumulative sum\n",
"scores1.cumsum()"
]
},
{
"cell_type": "code",
"execution_count": 124,
"id": "882ff41a-4d0f-4a15-adf3-272acc398bda",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 76, 157, 242, 321, 404, 486, 577, 657, 744, 830, 900,\n",
" 982, 1066, 1143, 1226, 1311, 1387, 1461, 1541, 1621, 1703, 1779,\n",
" 1847, 1924, 2004, 2082, 2159, 2232, 2313, 2389, 2474, 2555, 2639,\n",
" 2724, 2798, 2882, 2952, 3028, 3106, 3186, 3272, 3347, 3441, 3520,\n",
" 3604, 3682, 3754, 3840, 3914, 3982])"
]
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.cumsum(scores1)"
]
},
{
"cell_type": "code",
"execution_count": 125,
"id": "5e43fa2e-c30e-40c3-8159-89819e5e368f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(79.64)"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 算术平均\n",
"scores1.mean()"
]
},
{
"cell_type": "code",
"execution_count": 126,
"id": "2f59ffef-619d-4a03-a496-3972d13ee33e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(79.64)"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(scores1)"
]
},
{
"cell_type": "code",
"execution_count": 127,
"id": "3bfdf16c-894e-47e8-925c-604418cc0eb2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(79.44812732667022)"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 几何平均\n",
"stats.gmean(scores1)"
]
},
{
"cell_type": "code",
"execution_count": 128,
"id": "d73ce1b5-3956-4c12-9237-396ffdec44fd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(79.25499854665681)"
]
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 调和平均\n",
"stats.hmean(scores1)"
]
},
{
"cell_type": "code",
"execution_count": 129,
"id": "6d165492-6d0c-4f74-a2f1-75c95ca13b2d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(79.58695652173913)"
]
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 去尾平均\n",
"stats.tmean(scores1, [70, 90])"
]
},
{
"cell_type": "code",
"execution_count": 130,
"id": "99fbc4f2-220c-4ea0-9d24-8640c66c0115",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(79.58695652173913)"
]
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(scores1[(scores1 >= 70) & (scores1 <= 90)])"
]
},
{
"cell_type": "code",
"execution_count": 131,
"id": "8b536052-4a67-43e5-9e0a-6005bd73a66c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(80.0)"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 中位数\n",
"np.median(scores1)"
]
},
{
"cell_type": "code",
"execution_count": 132,
"id": "390f8e61-a108-497d-b3cf-54bd25ae3a0e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(np.int64(76), np.int64(5))"
]
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 众数\n",
"result = stats.mode(scores1)\n",
"result.mode, result.count"
]
},
{
"cell_type": "code",
"execution_count": 133,
"id": "bc729130-fdfa-47be-8a5c-ce7a474ab6ea",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(94)"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 最大值\n",
"scores1.max()"
]
},
{
"cell_type": "code",
"execution_count": 134,
"id": "f02f74e8-e27f-4169-8fda-2ebe4504f0d4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(94)"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.amax(scores1)"
]
},
{
"cell_type": "code",
"execution_count": 135,
"id": "0a32b324-df80-43c8-b8b0-12b36218ef2e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(68)"
]
},
"execution_count": 135,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 最小值\n",
"scores1.min()"
]
},
{
"cell_type": "code",
"execution_count": 136,
"id": "95680bd0-075a-4832-b7b9-76cea6653e2b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(68)"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.amin(scores1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "00d3a61a-d22b-4ee3-9327-8197abee91fc",
"metadata": {},
"outputs": [],
"source": [
"# 全距(极差)\n",
"np.ptp(scores1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6fd57ed-7bb3-4593-90ce-2a79a9ea8d2e",
"metadata": {},
"outputs": [],
"source": [
"# 四分位距离\n",
"q1, q3 = np.quantile(scores1, [0.25, 0.75])\n",
"q3 - q1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3668ed4b-4205-4f6a-9a9d-6135a238515a",
"metadata": {},
"outputs": [],
"source": [
"# inter-quartile range\n",
"stats.iqr(scores1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a3c5ed8-0331-4c94-8cac-6b947c7765fd",
"metadata": {},
"outputs": [],
"source": [
"# 总体方差\n",
"scores1.var()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2cdb1d1-179a-425f-930f-2ef7dda46a79",
"metadata": {},
"outputs": [],
"source": [
"np.var(scores1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "493d242d-fb94-4ed9-b316-7f722f908d99",
"metadata": {},
"outputs": [],
"source": [
"# 样本方差\n",
"scores1.var(ddof=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b70dac6d-24f1-473e-bb27-200384202db0",
"metadata": {},
"outputs": [],
"source": [
"np.var(scores1, ddof=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0670037e-0ae2-461f-b01a-0f47ed036813",
"metadata": {},
"outputs": [],
"source": [
"# 总体标准差\n",
"np.std(scores1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "22e3361d-3bbf-4175-94e1-719ca022e5dd",
"metadata": {},
"outputs": [],
"source": [
"# 样本标准差\n",
"np.std(scores1, ddof=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9b8db0c-6e78-4efb-a494-62977ce1e4e7",
"metadata": {},
"outputs": [],
"source": [
"# 变异系数\n",
"stats.variation(scores1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "029b2ccc-0dbb-460c-b84e-ea2b34cca3fb",
"metadata": {},
"outputs": [],
"source": [
"# 偏态系数\n",
"stats.skew(scores1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c35b17a1-5abc-4444-acbf-d757de430dcd",
"metadata": {},
"outputs": [],
"source": [
"# 峰度系数\n",
"stats.kurtosis(scores1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9fba00d6-866e-4ab1-9caf-18b17fe08440",
"metadata": {},
"outputs": [],
"source": [
"# 箱线图\n",
"plt.boxplot(scores1, showmeans=True, whis=1.5)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1cd46156-6a31-47c1-b3c6-4db92d91ac8c",
"metadata": {},
"outputs": [],
"source": [
"# 直方图\n",
"plt.hist(scores1, bins=6)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c5340e8-ed88-432f-8656-f987c0972b80",
"metadata": {},
"outputs": [],
"source": [
"# 设置随机数的种子\n",
"np.random.seed(12)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2102845-986b-4e22-9a73-0964af537386",
"metadata": {},
"outputs": [],
"source": [
"scores2 = np.random.randint(60, 101, (10, 3))\n",
"scores2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e9f4921-6001-45e6-bfe0-aca2fe2aa03e",
"metadata": {},
"outputs": [],
"source": [
"scores2.mean()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e116ac94-7cc2-48bc-b563-2e3ccd262378",
"metadata": {},
"outputs": [],
"source": [
"scores2.mean(axis=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b7fc404-06cf-4c3e-9ef2-b67bd1d3e517",
"metadata": {},
"outputs": [],
"source": [
"scores2.mean(axis=1).round(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3cf52cc-48ca-49d5-bbe7-6d977e8e8343",
"metadata": {},
"outputs": [],
"source": [
"# axis=0 - 默认值 - 沿着0轴计算\n",
"stats.describe(scores2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8b60634-56cc-489e-b898-792925aa79a9",
"metadata": {},
"outputs": [],
"source": [
"# axis=None - 不沿着任何一个轴计算\n",
"stats.describe(scores2, axis=None)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c0ba4905-ae46-40d6-a6a3-b97f830fbee5",
"metadata": {},
"outputs": [],
"source": [
"# axis=1 - 沿着1轴计算\n",
"result = stats.describe(scores2, axis=1)\n",
"result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9adade58-2bf1-4c46-be0d-0ef1682382f4",
"metadata": {},
"outputs": [],
"source": [
"result.mean.round(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24bca7b9-af35-4943-94f9-90ce5851d136",
"metadata": {},
"outputs": [],
"source": [
"result.variance.round(2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e90cd926-9f9d-4701-8f70-dc0f13ad24d9",
"metadata": {},
"outputs": [],
"source": [
"plt.boxplot(scores2, showmeans=True)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b58cd75-8df1-4593-a4cd-19c32f821f63",
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(14)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d792920-4ec1-4817-9e5a-b1d975dcb9a7",
"metadata": {},
"outputs": [],
"source": [
"temp8 = np.random.random(10)\n",
"temp8"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0385af53-d682-42a7-84da-a16b4353a6ad",
"metadata": {},
"outputs": [],
"source": [
"# 四舍五入\n",
"temp9 = temp8.round(1)\n",
"temp9"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7864d43f-b4ce-41d6-b8e3-cd646a12a0c6",
"metadata": {},
"outputs": [],
"source": [
"# 最大值的索引\n",
"temp8.argmax()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "336bc616-896f-4186-ad4d-5f6baa048564",
"metadata": {},
"outputs": [],
"source": [
"# 最小值的索引\n",
"temp8.argmin()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29cab4c1-1e56-4b19-a3b2-aeb30be03a88",
"metadata": {},
"outputs": [],
"source": [
"# 调整数组的形状\n",
"temp10 = temp8.reshape((5, 2))\n",
"# temp10 = temp8.reshape((5, 2)).copy()\n",
"temp10"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b8e2fe8-b917-4d40-ad48-d3935a1ab262",
"metadata": {},
"outputs": [],
"source": [
"temp10.base"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e266d969-8bd5-4572-9544-2569fd718156",
"metadata": {},
"outputs": [],
"source": [
"temp10.flags"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b7e5389-cdc3-4995-99f4-dc1386a5e291",
"metadata": {},
"outputs": [],
"source": [
"temp10.base is temp8"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2f6f2561-5392-4887-b0d2-3f639fb8be43",
"metadata": {},
"outputs": [],
"source": [
"temp10[2, 1] = 0.999999\n",
"temp10"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b739b914-57fd-4c3f-a106-a359e79391f9",
"metadata": {},
"outputs": [],
"source": [
"temp8"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb79055b-d7d6-4602-b960-890d33928e04",
"metadata": {},
"outputs": [],
"source": [
"temp8[3] = 0.0001\n",
"temp8"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f2932d9-45d3-427c-acef-a8ecc468747d",
"metadata": {},
"outputs": [],
"source": [
"temp10"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "089a7d87-ccbc-47c8-a934-15a2eb298242",
"metadata": {},
"outputs": [],
"source": [
"# 调整数组大小\n",
"temp8.resize((3, 5), refcheck=False)\n",
"temp8.round(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6af25425-046a-4f19-a5a5-235d6fd17753",
"metadata": {},
"outputs": [],
"source": [
"temp11 = np.resize(temp8, (4, 5)).round(1)\n",
"temp11"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "260a3d51-8041-4515-a611-971267a0be8c",
"metadata": {},
"outputs": [],
"source": [
"# 非零元素的索引\n",
"temp9.nonzero()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ab48494-d866-49ec-a214-586f1fa8beb2",
"metadata": {},
"outputs": [],
"source": [
"# 类型转换\n",
"temp12 = np.random.randint(-100, 101, 10)\n",
"temp12"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69b3c1e2-cca1-45d5-9d0c-2ab51ab66014",
"metadata": {},
"outputs": [],
"source": [
"temp12.astype(np.float64)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a7c4ce2-2838-4537-b70a-10f922f2806a",
"metadata": {},
"outputs": [],
"source": [
"temp12.astype('f8')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f15abc61-401e-4198-84ab-4aa086cfdf75",
"metadata": {},
"outputs": [],
"source": [
"temp12.astype('i1')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce590c02-4eab-4d4c-830c-07e0e887a771",
"metadata": {},
"outputs": [],
"source": [
"temp13 = temp12.astype('u1')\n",
"temp13"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c6b04d0-21e1-4be3-a927-095381341f57",
"metadata": {},
"outputs": [],
"source": [
"temp13.flags"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "711a115b-7a7d-48c1-b9de-b51673f89ab9",
"metadata": {},
"outputs": [],
"source": [
"temp12.astype('U')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "831f8c9c-f949-442c-ac51-66b3bfc59e1b",
"metadata": {},
"outputs": [],
"source": [
"# 修剪\n",
"temp9.clip(min=0.3, max=0.7)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d40724a4-a7fa-42c1-9ecc-3207579e7d7a",
"metadata": {},
"outputs": [],
"source": [
"# 将数组持久化到(文本)文件\n",
"temp11.tofile('temp11.txt', sep=',')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4246bae6-ab59-4ee0-ae8d-7d2cc3f60d97",
"metadata": {},
"outputs": [],
"source": [
"temp13 = np.fromfile('temp11.txt', sep=',').reshape(4, 5)\n",
"temp13"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9d38dba-acb8-4745-be3f-76e7b2605c09",
"metadata": {},
"outputs": [],
"source": [
"# 将数组持久化到(二进制)文件\n",
"temp11.dump('temp11')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "db38d63c-e279-4002-bf02-fed4f615319a",
"metadata": {},
"outputs": [],
"source": [
"# 从二进制文件(pickle序列化)中加载数组\n",
"temp14 = np.load('temp11', allow_pickle=True)\n",
"temp14"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3fc26c0e-b518-4b23-a019-9e8e11e363e2",
"metadata": {},
"outputs": [],
"source": [
"temp15 = np.random.randint(1, 100, (2, 3, 4))\n",
"temp15"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1884ac0d-65f9-410d-987f-7235ccd54d0f",
"metadata": {},
"outputs": [],
"source": [
"# 扁平化\n",
"temp16 = temp15.flatten()\n",
"temp16"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "369c19c8-ef7c-4740-b8ba-cf47f63f9fd7",
"metadata": {},
"outputs": [],
"source": [
"# 扁平化\n",
"temp17 = temp15.ravel()\n",
"temp17"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d7411df-a939-41f6-bee0-213b555ed666",
"metadata": {},
"outputs": [],
"source": [
"temp16.base is temp15"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "980f80da-d3d6-45c9-86d0-733e0098e8f0",
"metadata": {},
"outputs": [],
"source": [
"temp16.flags"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4db6df5-e0c4-4f7b-bc0a-50655c036e77",
"metadata": {},
"outputs": [],
"source": [
"temp17.base is temp15"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7339bcf2-a29f-4cf6-ac5f-8a81eb655698",
"metadata": {},
"outputs": [],
"source": [
"temp17.flags"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "06d8dee5-528e-4345-b5a7-ddc0413cda09",
"metadata": {},
"outputs": [],
"source": [
"temp16[0] = 999\n",
"temp16"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cfa3191-5441-4de7-a300-6bd8aa11cb30",
"metadata": {},
"outputs": [],
"source": [
"temp15"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a4ce0ee-2971-48b3-8834-9113464cf9af",
"metadata": {},
"outputs": [],
"source": [
"temp17[0] = 88\n",
"temp17"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7dd7fda5-d25e-4a9a-9202-9a5140fc3439",
"metadata": {},
"outputs": [],
"source": [
"temp15"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe00bb4c-1e19-4988-8020-f2f4162840f2",
"metadata": {},
"outputs": [],
"source": [
"# 排序 - 返回排序后的新数组\n",
"np.sort(temp16)[::-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8fe37ba0-9bce-4bb1-bddc-3100a755439d",
"metadata": {},
"outputs": [],
"source": [
"# 排序 - 就地排序\n",
"temp16.sort()\n",
"temp16"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afef80f1-5f26-46d3-9589-7eea82b224cc",
"metadata": {},
"outputs": [],
"source": [
"temp18 = np.random.randint(1, 100, 10)\n",
"temp18"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "43f1d0f6-2a74-4512-a6ee-d285de2ee2aa",
"metadata": {},
"outputs": [],
"source": [
"# 给出索引的顺序 - 花式索引\n",
"temp18[temp18.argsort()]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23fc033c-a649-4da8-a0f8-1c7739e3996c",
"metadata": {},
"outputs": [],
"source": [
"# 转置\n",
"temp11.transpose()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ac95b32-c9ce-404b-a26e-1e8a2351a44b",
"metadata": {},
"outputs": [],
"source": [
"temp11.T"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "796f513d-211f-4915-969c-1850d4f569a2",
"metadata": {},
"outputs": [],
"source": [
"# 交换轴\n",
"temp11.swapaxes(0, 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d9d0207-45d9-4ec0-bcef-9e7072501241",
"metadata": {},
"outputs": [],
"source": [
"temp15"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0fd160f1-710f-4591-853a-d2e960493bc4",
"metadata": {},
"outputs": [],
"source": [
"temp15.swapaxes(0, 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77569990-b8f2-4ff1-b1f6-d17a942f1a32",
"metadata": {},
"outputs": [],
"source": [
"temp15.swapaxes(1, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5970d5d4-f955-4af8-8c54-46a7fff9d0f9",
"metadata": {},
"outputs": [],
"source": [
"# 将数组处理成列表\n",
"list1 = temp16.tolist()\n",
"print(list1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c3e62e2-aaef-4250-a561-0f28dd51bf79",
"metadata": {},
"outputs": [],
"source": [
"list2 = temp11.tolist()\n",
"print(list2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37a0a054-34e3-4b2a-a07d-72a9599f012f",
"metadata": {},
"outputs": [],
"source": [
"list3 = temp15.tolist()\n",
"print(list3)"
]
}
],
"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
}