组队学习资料

This commit is contained in:
hanhuijin
2020-10-11 20:53:49 +08:00
parent 0b58fe3b96
commit 0e86fa6b30
16 changed files with 2578 additions and 25 deletions

View File

@@ -0,0 +1,169 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 常量\n",
"\n",
"## numpy.nan\n",
"\n",
"- 表示空值。\n",
"\n",
"```\n",
"nan = NaN = NAN\n",
"```\n",
"\n",
"\n",
"【例】两个`numpy.nan`是不相等的。\n",
"\n",
"```python\n",
"import numpy as np\n",
"\n",
"print(np.nan == np.nan) # False\n",
"print(np.nan != np.nan) # True\n",
"```\n",
"\n",
"- `numpy.isnan(x, *args, **kwargs)` Test element-wise for NaN and return result as a boolean array.\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([1, 1, 8, np.nan, 10])\n",
"print(x)\n",
"# [ 1. 1. 8. nan 10.]\n",
"\n",
"y = np.isnan(x)\n",
"print(y)\n",
"# [False False False True False]\n",
"\n",
"z = np.count_nonzero(y)\n",
"print(z) # 1\n",
"```\n",
"\n",
"\n",
"\n",
"## numpy.inf\n",
"- 表示正无穷大。\n",
"\n",
"```\n",
"Inf = inf = infty = Infinity = PINF\n",
"```\n",
"\n",
"【例】\n",
"```python\n",
"\n",
"```\n",
"\n",
"## numpy.pi\n",
"\n",
"- 表示圆周率\n",
"\n",
"```python\n",
"pi = 3.1415926535897932384626433...\n",
"```\n",
"\n",
"## numpy.e\n",
"\n",
"- 表示自然常数\n",
"\n",
"```python\n",
"e = 2.71828182845904523536028747135266249775724709369995...\n",
"```\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'numpy' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-0dfb6dd16de6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnumpy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'numpy' is not defined"
]
}
],
"source": [
"numpy.e"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -0,0 +1,356 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 时间日期和时间增量\n",
"\n",
"## datetime64 基础\n",
"\n",
"在 numpy 中,我们很方便的将字符串转换成时间日期类型 `datetime64``datetime` 已被 python 包含的日期时间库所占用)。\n",
"\n",
"`datatime64`是带单位的日期时间类型,其单位如下:\n",
"\n",
"日期单位 | 代码含义|时间单位 | 代码含义\n",
":---:|:---:|:---:|:---:\n",
"Y | 年 |h | 小时\n",
"M | 月 |m | 分钟\n",
"W | 周 |s | 秒\n",
"D | 天 |ms | 毫秒\n",
"- | - |us | 微秒\n",
"- | - |ns | 纳秒\n",
"- | - |ps | 皮秒\n",
"- | - |fs | 飞秒\n",
"- | - |as | 阿托秒\n",
"\n",
"注意:\n",
"- 1秒 = 1000 毫秒milliseconds\n",
"- 1毫秒 = 1000 微秒microseconds\n",
"\n",
"【例】从字符串创建 datetime64 类型时默认情况下numpy 会根据字符串自动选择对应的单位。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.datetime64('2020-03-01')\n",
"print(a, a.dtype) # 2020-03-01 datetime64[D]\n",
"\n",
"a = np.datetime64('2020-03')\n",
"print(a, a.dtype) # 2020-03 datetime64[M]\n",
"\n",
"a = np.datetime64('2020-03-08 20:00:05')\n",
"print(a, a.dtype) # 2020-03-08T20:00:05 datetime64[s]\n",
"\n",
"a = np.datetime64('2020-03-08 20:00')\n",
"print(a, a.dtype) # 2020-03-08T20:00 datetime64[m]\n",
"\n",
"a = np.datetime64('2020-03-08 20')\n",
"print(a, a.dtype) # 2020-03-08T20 datetime64[h]\n",
"```\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"【例】从字符串创建 datetime64 类型时,可以强制指定使用的单位。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.datetime64('2020-03', 'D')\n",
"print(a, a.dtype) # 2020-03-01 datetime64[D]\n",
"\n",
"a = np.datetime64('2020-03', 'Y')\n",
"print(a, a.dtype) # 2020 datetime64[Y]\n",
"\n",
"print(np.datetime64('2020-03') == np.datetime64('2020-03-01')) # True\n",
"print(np.datetime64('2020-03') == np.datetime64('2020-03-02')) #False\n",
"```\n",
"\n",
"由上例可以看出2019-03 和 2019-03-01 所表示的其实是同一个时间。\n",
"事实上,如果两个 datetime64 对象具有不同的单位,它们可能仍然代表相同的时刻。并且从较大的单位(如月份)转换为较小的单位(如天数)是安全的。\n",
"\n",
"\n",
"【例】从字符串创建 datetime64 数组时,如果单位不统一,则一律转化成其中最小的单位。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.array(['2020-03', '2020-03-08', '2020-03-08 20:00'], dtype='datetime64')\n",
"print(a, a.dtype)\n",
"# ['2020-03-01T00:00' '2020-03-08T00:00' '2020-03-08T20:00'] datetime64[m]\n",
"```\n",
"\n",
"\n",
"【例】使用`arange()`创建 datetime64 数组,用于生成日期范围。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.arange('2020-08-01', '2020-08-10', dtype=np.datetime64)\n",
"print(a)\n",
"# ['2020-08-01' '2020-08-02' '2020-08-03' '2020-08-04' '2020-08-05'\n",
"# '2020-08-06' '2020-08-07' '2020-08-08' '2020-08-09']\n",
"print(a.dtype) # datetime64[D]\n",
"\n",
"a = np.arange('2020-08-01 20:00', '2020-08-10', dtype=np.datetime64)\n",
"print(a)\n",
"# ['2020-08-01T20:00' '2020-08-01T20:01' '2020-08-01T20:02' ...\n",
"# '2020-08-09T23:57' '2020-08-09T23:58' '2020-08-09T23:59']\n",
"print(a.dtype) # datetime64[m]\n",
"\n",
"a = np.arange('2020-05', '2020-12', dtype=np.datetime64)\n",
"print(a)\n",
"# ['2020-05' '2020-06' '2020-07' '2020-08' '2020-09' '2020-10' '2020-11']\n",
"print(a.dtype) # datetime64[M]\n",
"```\n",
"\n",
"## datetime64 和 timedelta64 运算\n",
"\n",
"【例】timedelta64 表示两个 datetime64 之间的差。timedelta64 也是带单位的,并且和相减运算中的两个 datetime64 中的较小的单位保持一致。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.datetime64('2020-03-08') - np.datetime64('2020-03-07')\n",
"b = np.datetime64('2020-03-08') - np.datetime64('2020-03-07 08:00')\n",
"c = np.datetime64('2020-03-08') - np.datetime64('2020-03-07 23:00', 'D')\n",
"\n",
"print(a, a.dtype) # 1 days timedelta64[D]\n",
"print(b, b.dtype) # 956178240 minutes timedelta64[m]\n",
"print(c, c.dtype) # 1 days timedelta64[D]\n",
"\n",
"a = np.datetime64('2020-03') + np.timedelta64(20, 'D')\n",
"b = np.datetime64('2020-06-15 00:00') + np.timedelta64(12, 'h')\n",
"print(a, a.dtype) # 2020-03-21 datetime64[D]\n",
"print(b, b.dtype) # 2020-06-15T12:00 datetime64[m]\n",
"```\n",
"\n",
"【例】生成 timedelta64时要注意年'Y')和月('M')这两个单位无法和其它单位进行运算(一年有几天?一个月有几个小时?这些都是不确定的)。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.timedelta64(1, 'Y')\n",
"b = np.timedelta64(a, 'M')\n",
"print(a) # 1 years\n",
"print(b) # 12 months\n",
"\n",
"c = np.timedelta64(1, 'h')\n",
"d = np.timedelta64(c, 'm')\n",
"print(c) # 1 hours\n",
"print(d) # 60 minutes\n",
"\n",
"print(np.timedelta64(a, 'D'))\n",
"# TypeError: Cannot cast NumPy timedelta64 scalar from metadata [Y] to [D] according to the rule 'same_kind'\n",
"\n",
"print(np.timedelta64(b, 'D'))\n",
"# TypeError: Cannot cast NumPy timedelta64 scalar from metadata [M] to [D] according to the rule 'same_kind'\n",
"```\n",
"\n",
"\n",
"【例】timedelta64 的运算。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.timedelta64(1, 'Y')\n",
"b = np.timedelta64(6, 'M')\n",
"c = np.timedelta64(1, 'W')\n",
"d = np.timedelta64(1, 'D')\n",
"e = np.timedelta64(10, 'D')\n",
"\n",
"print(a) # 1 years\n",
"print(b) # 6 months\n",
"print(a + b) # 18 months\n",
"print(a - b) # 6 months\n",
"print(2 * a) # 2 years\n",
"print(a / b) # 2.0\n",
"print(c / d) # 7.0\n",
"print(c % e) # 7 days\n",
"```\n",
"\n",
"【例】numpy.datetime64 与 datetime.datetime 相互转换\n",
"```python\n",
"import numpy as np\n",
"import datetime\n",
"\n",
"dt = datetime.datetime(year=2020, month=6, day=1, hour=20, minute=5, second=30)\n",
"dt64 = np.datetime64(dt, 's')\n",
"print(dt64, dt64.dtype)\n",
"# 2020-06-01T20:05:30 datetime64[s]\n",
"\n",
"dt2 = dt64.astype(datetime.datetime)\n",
"print(dt2, type(dt2))\n",
"# 2020-06-01 20:05:30 <class 'datetime.datetime'>\n",
"```\n",
"\n",
"## datetime64 的应用\n",
"\n",
"为了允许在只有一周中某些日子有效的上下文中使用日期时间NumPy包含一组“busday”工作日功能。\n",
"\n",
"- `numpy.busday_offset(dates, offsets, roll='raise', weekmask='1111100', holidays=None, busdaycal=None, out=None)` First adjusts the date to fall on a valid day according to the roll rule, then applies offsets to the given dates counted in valid days.\n",
"\n",
"参数`roll`{'raise', 'nat', 'forward', 'following', 'backward', 'preceding', 'modifiedfollowing', 'modifiedpreceding'}\n",
"- 'raise' means to raise an exception for an invalid day.\n",
"- 'nat' means to return a NaT (not-a-time) for an invalid day.\n",
"- 'forward' and 'following' mean to take the first valid day later in time.\n",
"- 'backward' and 'preceding' mean to take the first valid day earlier in time.\n",
"\n",
"\n",
"【例】将指定的偏移量应用于工作日,单位天('D')。计算下一个工作日,如果当前日期为非工作日,默认报错。可以指定 `forward` 或 `backward` 规则来避免报错。(一个是向前取第一个有效的工作日,一个是向后取第一个有效的工作日)\n",
"```python\n",
"import numpy as np\n",
"\n",
"# 2020-07-10 星期五\n",
"a = np.busday_offset('2020-07-10', offsets=1)\n",
"print(a) # 2020-07-13\n",
"\n",
"a = np.busday_offset('2020-07-11', offsets=1)\n",
"print(a)\n",
"# ValueError: Non-business day date in busday_offset\n",
"\n",
"a = np.busday_offset('2020-07-11', offsets=0, roll='forward')\n",
"b = np.busday_offset('2020-07-11', offsets=0, roll='backward')\n",
"print(a) # 2020-07-13\n",
"print(b) # 2020-07-10\n",
"\n",
"a = np.busday_offset('2020-07-11', offsets=1, roll='forward')\n",
"b = np.busday_offset('2020-07-11', offsets=1, roll='backward')\n",
"print(a) # 2020-07-14\n",
"print(b) # 2020-07-13\n",
"```\n",
"\n",
"可以指定偏移量为 0 来获取当前日期向前或向后最近的工作日,当然,如果当前日期本身就是工作日,则直接返回当前日期。\n",
"\n",
"- `numpy.is_busday(dates, weekmask='1111100', holidays=None, busdaycal=None, out=None)` Calculates which of the given dates are valid days, and which are not.\n",
"\n",
"【例】返回指定日期是否是工作日。\n",
"```python\n",
"import numpy as np\n",
"\n",
"# 2020-07-10 星期五\n",
"a = np.is_busday('2020-07-10')\n",
"b = np.is_busday('2020-07-11')\n",
"print(a) # True\n",
"print(b) # False\n",
"```\n",
"\n",
"【例】统计一个 `datetime64[D]` 数组中的工作日天数。\n",
"```python\n",
"import numpy as np\n",
"\n",
"# 2020-07-10 星期五\n",
"begindates = np.datetime64('2020-07-10')\n",
"enddates = np.datetime64('2020-07-20')\n",
"a = np.arange(begindates, enddates, dtype='datetime64')\n",
"b = np.count_nonzero(np.is_busday(a))\n",
"print(a)\n",
"# ['2020-07-10' '2020-07-11' '2020-07-12' '2020-07-13' '2020-07-14'\n",
"# '2020-07-15' '2020-07-16' '2020-07-17' '2020-07-18' '2020-07-19']\n",
"print(b) # 6\n",
"```\n",
"\n",
"\n",
"【例】自定义周掩码值,即指定一周中哪些星期是工作日。\n",
"```python\n",
"import numpy as np\n",
"\n",
"# 2020-07-10 星期五\n",
"a = np.is_busday('2020-07-10', weekmask=[1, 1, 1, 1, 1, 0, 0])\n",
"b = np.is_busday('2020-07-10', weekmask=[1, 1, 1, 1, 0, 0, 1])\n",
"print(a) # True\n",
"print(b) # False\n",
"```\n",
"\n",
"- `numpy.busday_count(begindates, enddates, weekmask='1111100', holidays=[], busdaycal=None, out=None)`Counts the number of valid days between `begindates` and `enddates`, not including the day of `enddates`.\n",
"\n",
"【例】返回两个日期之间的工作日数量。\n",
"```python\n",
"import numpy as np\n",
"\n",
"# 2020-07-10 星期五\n",
"begindates = np.datetime64('2020-07-10')\n",
"enddates = np.datetime64('2020-07-20')\n",
"a = np.busday_count(begindates, enddates)\n",
"b = np.busday_count(enddates, begindates)\n",
"print(a) # 6\n",
"print(b) # -6\n",
"```\n",
"\n",
"\n",
"---\n",
"<b>参考图文</b>\n",
"\n",
"- https://www.jianshu.com/p/336cd77d9914\n",
"- https://www.cnblogs.com/gl1573/p/10549547.html#h2datetime64\n",
"- https://www.numpy.org.cn/reference/arrays/datetime.html#%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E5%8D%95%E4%BD%8D\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -0,0 +1,617 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"[toc]\n",
"\n",
"---\n",
"# 数组的创建\n",
"\n",
"导入 numpy。\n",
"\n",
"```\n",
"import numpy as np\n",
"```\n",
"\n",
"numpy 提供的最重要的数据结构是`ndarray`,它是 python 中`list`的扩展。\n",
"\n",
"## 1. 依据现有数据来创建 ndarray\n",
"\n",
"### **a通过array()函数进行创建。**\n",
"\n",
"```python\n",
"def array(p_object, dtype=None, copy=True, order='K', subok=False, ndmin=0): \n",
"``` \n",
"\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"# 创建一维数组\n",
"a = np.array([0, 1, 2, 3, 4])\n",
"b = np.array((0, 1, 2, 3, 4))\n",
"print(a, type(a))\n",
"# [0 1 2 3 4] <class 'numpy.ndarray'>\n",
"print(b, type(b))\n",
"# [0 1 2 3 4] <class 'numpy.ndarray'>\n",
"\n",
"# 创建二维数组\n",
"c = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"print(c, type(c))\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]\n",
"# [21 22 23 24 25]\n",
"# [26 27 28 29 30]\n",
"# [31 32 33 34 35]] <class 'numpy.ndarray'>\n",
"\n",
"# 创建三维数组\n",
"d = np.array([[(1.5, 2, 3), (4, 5, 6)],\n",
" [(3, 2, 1), (4, 5, 6)]])\n",
"print(d, type(d))\n",
"# [[[1.5 2. 3. ]\n",
"# [4. 5. 6. ]]\n",
"#\n",
"# [[3. 2. 1. ]\n",
"# [4. 5. 6. ]]] <class 'numpy.ndarray'>\n",
"```\n",
"\n",
"### **b通过asarray()函数进行创建**\n",
"\n",
"`array()`和`asarray()`都可以将结构数据转化为 ndarray但是`array()`和`asarray()`主要区别就是当数据源是**ndarray** 时,`array()`仍然会 copy 出一个副本,占用新的内存,但不改变 dtype 时 `asarray()`不会。\n",
"\n",
"```python\n",
"def asarray(a, dtype=None, order=None):\n",
" return array(a, dtype, copy=False, order=order)\n",
"```\n",
"\n",
"【例】`array()`和`asarray()`都可以将结构数据转化为 ndarray\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]\n",
"y = np.array(x)\n",
"z = np.asarray(x)\n",
"x[1][2] = 2\n",
"print(x,type(x))\n",
"# [[1, 1, 1], [1, 1, 2], [1, 1, 1]] <class 'list'>\n",
"\n",
"print(y,type(y))\n",
"# [[1 1 1]\n",
"# [1 1 1]\n",
"# [1 1 1]] <class 'numpy.ndarray'>\n",
"\n",
"print(z,type(z))\n",
"# [[1 1 1]\n",
"# [1 1 1]\n",
"# [1 1 1]] <class 'numpy.ndarray'>\n",
"```\n",
"\n",
"【例】`array()`和`asarray()`的区别。(`array()`和`asarray()`主要区别就是当数据源是**ndarray** 时,`array()`仍然会 copy 出一个副本,占用新的内存,但不改变 dtype 时 `asarray()`不会。)\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])\n",
"y = np.array(x)\n",
"z = np.asarray(x)\n",
"w = np.asarray(x, dtype=np.int)\n",
"x[1][2] = 2\n",
"print(x,type(x),x.dtype)\n",
"# [[1 1 1]\n",
"# [1 1 2]\n",
"# [1 1 1]] <class 'numpy.ndarray'> int32\n",
"\n",
"print(y,type(y),y.dtype)\n",
"# [[1 1 1]\n",
"# [1 1 1]\n",
"# [1 1 1]] <class 'numpy.ndarray'> int32\n",
"\n",
"print(z,type(z),z.dtype)\n",
"# [[1 1 1]\n",
"# [1 1 2]\n",
"# [1 1 1]] <class 'numpy.ndarray'> int32\n",
"\n",
"print(w,type(w),w.dtype)\n",
"# [[1 1 1]\n",
"# [1 1 2]\n",
"# [1 1 1]] <class 'numpy.ndarray'> int32\n",
"```\n",
"\n",
"\n",
"【例】更改为较大的dtype时其大小必须是array的最后一个axis的总大小以字节为单位的除数\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])\n",
"print(x, x.dtype)\n",
"# [[1 1 1]\n",
"# [1 1 1]\n",
"# [1 1 1]] int32\n",
"x.dtype = np.float\n",
"\n",
"# ValueError: When changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array.\n",
"```\n",
"\n",
"### **c通过fromfunction()函数进行创建**\n",
"\n",
"给函数绘图的时候可能会用到`fromfunction()`,该函数可从函数中创建数组。\n",
"\n",
"```python\n",
"def fromfunction(function, shape, **kwargs):\n",
"```\n",
"\n",
"\n",
"【例】通过在每个坐标上执行一个函数来构造数组。\n",
"\n",
"```python\n",
"import numpy as np\n",
"\n",
"def f(x, y):\n",
" return 10 * x + y\n",
"\n",
"x = np.fromfunction(f, (5, 4), dtype=int)\n",
"print(x)\n",
"# [[ 0 1 2 3]\n",
"# [10 11 12 13]\n",
"# [20 21 22 23]\n",
"# [30 31 32 33]\n",
"# [40 41 42 43]]\n",
"\n",
"x = np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)\n",
"print(x)\n",
"# [[ True False False]\n",
"# [False True False]\n",
"# [False False True]]\n",
"\n",
"x = np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)\n",
"print(x)\n",
"# [[0 1 2]\n",
"# [1 2 3]\n",
"# [2 3 4]]\n",
"```\n",
"\n",
"\n",
"\n",
"## 2. 依据 ones 和 zeros 填充方式\n",
"\n",
"在机器学习任务中经常做的一件事就是初始化参数,需要用常数值或者随机值来创建一个固定大小的矩阵。\n",
"\n",
"### **a零数组**\n",
"\n",
"- `zeros()`函数:返回给定形状和类型的零数组。\n",
"- `zeros_like()`函数:返回与给定数组形状和类型相同的零数组。\n",
"\n",
"```python\n",
"def zeros(shape, dtype=None, order='C'):\n",
"def zeros_like(a, dtype=None, order='K', subok=True, shape=None):\n",
"```\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.zeros(5)\n",
"print(x) # [0. 0. 0. 0. 0.]\n",
"x = np.zeros([2, 3])\n",
"print(x)\n",
"# [[0. 0. 0.]\n",
"# [0. 0. 0.]]\n",
"\n",
"x = np.array([[1, 2, 3], [4, 5, 6]])\n",
"y = np.zeros_like(x)\n",
"print(y)\n",
"# [[0 0 0]\n",
"# [0 0 0]]\n",
"```\n",
"\n",
"### **b1数组**\n",
"\n",
"- `ones()`函数返回给定形状和类型的1数组。\n",
"- `ones_like()`函数返回与给定数组形状和类型相同的1数组。\n",
"\n",
"```python\n",
"def ones(shape, dtype=None, order='C'):\n",
"def ones_like(a, dtype=None, order='K', subok=True, shape=None):\n",
"```\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.ones(5)\n",
"print(x) # [1. 1. 1. 1. 1.]\n",
"x = np.ones([2, 3])\n",
"print(x)\n",
"# [[1. 1. 1.]\n",
"# [1. 1. 1.]]\n",
"\n",
"x = np.array([[1, 2, 3], [4, 5, 6]])\n",
"y = np.ones_like(x)\n",
"print(y)\n",
"# [[1 1 1]\n",
"# [1 1 1]]\n",
"```\n",
"\n",
"### **c空数组**\n",
"\n",
"- `empty()`函数:返回一个空数组,数组元素为随机数。\n",
"- `empty_like`函数:返回与给定数组具有相同形状和类型的新数组。\n",
"\n",
"```python\n",
"def empty(shape, dtype=None, order='C'): \n",
"def empty_like(prototype, dtype=None, order='K', subok=True, shape=None):\n",
"```\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.empty(5)\n",
"print(x)\n",
"# [1.95821574e-306 1.60219035e-306 1.37961506e-306 \n",
"# 9.34609790e-307 1.24610383e-306]\n",
"\n",
"x = np.empty((3, 2))\n",
"print(x)\n",
"# [[1.60220393e-306 9.34587382e-307]\n",
"# [8.45599367e-307 7.56598449e-307]\n",
"# [1.33509389e-306 3.59412896e-317]]\n",
"\n",
"x = np.array([[1, 2, 3], [4, 5, 6]])\n",
"y = np.empty_like(x)\n",
"print(y)\n",
"# [[ 7209029 6422625 6619244]\n",
"# [ 100 707539280 504]]\n",
"```\n",
"\n",
"### **d单位数组**\n",
"\n",
"- `eye()`函数返回一个对角线上为1其它地方为零的单位数组。\n",
"- `identity()`函数:返回一个方的单位数组。\n",
"\n",
"```python\n",
"def eye(N, M=None, k=0, dtype=float, order='C'):\n",
"def identity(n, dtype=None):\n",
"```\n",
"\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.eye(4)\n",
"print(x)\n",
"# [[1. 0. 0. 0.]\n",
"# [0. 1. 0. 0.]\n",
"# [0. 0. 1. 0.]\n",
"# [0. 0. 0. 1.]]\n",
"\n",
"x = np.eye(2, 3)\n",
"print(x)\n",
"# [[1. 0. 0.]\n",
"# [0. 1. 0.]]\n",
"\n",
"x = np.identity(4)\n",
"print(x)\n",
"# [[1. 0. 0. 0.]\n",
"# [0. 1. 0. 0.]\n",
"# [0. 0. 1. 0.]\n",
"# [0. 0. 0. 1.]]\n",
"```\n",
"\n",
"### **e对角数组**\n",
"\n",
"- `diag()`函数:提取对角线或构造对角数组。\n",
"\n",
"```python\n",
"def diag(v, k=0):\n",
"```\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.arange(9).reshape((3, 3))\n",
"print(x)\n",
"# [[0 1 2]\n",
"# [3 4 5]\n",
"# [6 7 8]]\n",
"print(np.diag(x)) # [0 4 8]\n",
"print(np.diag(x, k=1)) # [1 5]\n",
"print(np.diag(x, k=-1)) # [3 7]\n",
"\n",
"v = [1, 3, 5, 7]\n",
"x = np.diag(v)\n",
"print(x)\n",
"# [[1 0 0 0]\n",
"# [0 3 0 0]\n",
"# [0 0 5 0]\n",
"# [0 0 0 7]]\n",
"```\n",
"\n",
"### **f常数数组**\n",
"\n",
"- `full()`函数:返回一个常数数组。\n",
"- `full_like()`函数:返回与给定数组具有相同形状和类型的常数数组。\n",
"\n",
"```python\n",
"def full(shape, fill_value, dtype=None, order='C'):\n",
"def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None):\n",
"```\n",
"\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.full((2,), 7)\n",
"print(x)\n",
"# [7 7]\n",
"\n",
"x = np.full(2, 7)\n",
"print(x)\n",
"# [7 7]\n",
"\n",
"x = np.full((2, 7), 7)\n",
"print(x)\n",
"# [[7 7 7 7 7 7 7]\n",
"# [7 7 7 7 7 7 7]]\n",
"\n",
"x = np.array([[1, 2, 3], [4, 5, 6]])\n",
"y = np.full_like(x, 7)\n",
"print(y)\n",
"# [[7 7 7]\n",
"# [7 7 7]]\n",
"```\n",
"\n",
"\n",
"\n",
"## 3. 利用数值范围来创建ndarray\n",
" - `arange()`函数:返回给定间隔内的均匀间隔的值。\n",
" - `linspace()`函数:返回指定间隔内的等间隔数字。\n",
" - `logspace()`函数:返回数以对数刻度均匀分布。\n",
" - `numpy.random.rand()` 返回一个由[0,1)内的随机数组成的数组。\n",
"\n",
"```python\n",
"def arange([start,] stop[, step,], dtype=None): \n",
"def linspace(start, stop, num=50, endpoint=True, retstep=False, \n",
" dtype=None, axis=0):\n",
"def logspace(start, stop, num=50, endpoint=True, base=10.0, \n",
" dtype=None, axis=0):\n",
"def rand(d0, d1, ..., dn): \n",
"```\n",
"\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.arange(5)\n",
"print(x) # [0 1 2 3 4]\n",
"\n",
"x = np.arange(3, 7, 2)\n",
"print(x) # [3 5]\n",
"\n",
"x = np.linspace(start=0, stop=2, num=9)\n",
"print(x) \n",
"# [0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ]\n",
"\n",
"x = np.logspace(0, 1, 5)\n",
"print(np.around(x, 2))\n",
"# [ 1. 1.78 3.16 5.62 10. ] \n",
" #np.around 返回四舍五入后的值,可指定精度。\n",
" # around(a, decimals=0, out=None)\n",
" # a 输入数组\n",
" # decimals 要舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置\n",
"\n",
"\n",
"x = np.linspace(start=0, stop=1, num=5)\n",
"x = [10 ** i for i in x]\n",
"print(np.around(x, 2))\n",
"# [ 1. 1.78 3.16 5.62 10. ]\n",
"\n",
"x = np.random.random(5)\n",
"print(x)\n",
"# [0.41768753 0.16315577 0.80167915 0.99690199 0.11812291]\n",
"\n",
"x = np.random.random([2, 3])\n",
"print(x)\n",
"# [[0.41151858 0.93785153 0.57031309]\n",
"# [0.13482333 0.20583516 0.45429181]]\n",
"```\n",
"\n",
"\n",
"\n",
"---\n",
"## 4. 结构数组的创建\n",
"\n",
"结构数组,首先需要定义结构,然后利用`np.array()`来创建数组,其参数`dtype`为定义的结构。\n",
"\n",
"### **a利用字典来定义结构**\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"personType = np.dtype({\n",
" 'names': ['name', 'age', 'weight'],\n",
" 'formats': ['U30', 'i8', 'f8']})\n",
"\n",
"a = np.array([('Liming', 24, 63.9), ('Mike', 15, 67.), ('Jan', 34, 45.8)],\n",
" dtype=personType)\n",
"print(a, type(a))\n",
"# [('Liming', 24, 63.9) ('Mike', 15, 67. ) ('Jan', 34, 45.8)]\n",
"# <class 'numpy.ndarray'>\n",
"```\n",
"\n",
"### **b利用包含多个元组的列表来定义结构**\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"personType = np.dtype([('name', 'U30'), ('age', 'i8'), ('weight', 'f8')])\n",
"a = np.array([('Liming', 24, 63.9), ('Mike', 15, 67.), ('Jan', 34, 45.8)],\n",
" dtype=personType)\n",
"print(a, type(a))\n",
"# [('Liming', 24, 63.9) ('Mike', 15, 67. ) ('Jan', 34, 45.8)]\n",
"# <class 'numpy.ndarray'>\n",
"\n",
"# 结构数组的取值方式和一般数组差不多,可以通过下标取得元素:\n",
"print(a[0])\n",
"# ('Liming', 24, 63.9)\n",
"\n",
"print(a[-2:])\n",
"# [('Mike', 15, 67. ) ('Jan', 34, 45.8)]\n",
"\n",
"# 我们可以使用字段名作为下标获取对应的值\n",
"print(a['name'])\n",
"# ['Liming' 'Mike' 'Jan']\n",
"print(a['age'])\n",
"# [24 15 34]\n",
"print(a['weight'])\n",
"# [63.9 67. 45.8]\n",
"```\n",
"\n",
"---\n",
"# 数组的属性\n",
"\n",
"在使用 numpy 时,你会想知道数组的某些信息。很幸运,在这个包里边包含了很多便捷的方法,可以给你想要的信息。\n",
"\n",
"\n",
"\n",
" - `numpy.ndarray.ndim`用于返回数组的维数(轴的个数)也称为秩,一维数组的秩为 1二维数组的秩为 2以此类推。\n",
" - `numpy.ndarray.shape`表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 `ndim` 属性(秩)。\n",
" - `numpy.ndarray.size`数组中所有元素的总量,相当于数组的`shape`中所有元素的乘积,例如矩阵的元素总量为行与列的乘积。\n",
" - `numpy.ndarray.dtype` `ndarray` 对象的元素类型。\n",
" - `numpy.ndarray.itemsize`以字节的形式返回数组中每一个元素的大小。\n",
"\n",
"```python\n",
"class ndarray(object):\n",
" shape = property(lambda self: object(), lambda self, v: None, lambda self: None)\n",
" dtype = property(lambda self: object(), lambda self, v: None, lambda self: None)\n",
" size = property(lambda self: object(), lambda self, v: None, lambda self: None)\n",
" ndim = property(lambda self: object(), lambda self, v: None, lambda self: None)\n",
" itemsize = property(lambda self: object(), lambda self, v: None, lambda self: None)\n",
"```\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.array([1, 2, 3, 4, 5])\n",
"print(a.shape) # (5,)\n",
"print(a.dtype) # int32\n",
"print(a.size) # 5\n",
"print(a.ndim) # 1\n",
"print(a.itemsize) # 4\n",
"\n",
"b = np.array([[1, 2, 3], [4, 5, 6.0]])\n",
"print(b.shape) # (2, 3)\n",
"print(b.dtype) # float64\n",
"print(b.size) # 6\n",
"print(b.ndim) # 2\n",
"print(b.itemsize) # 8\n",
"```\n",
"\n",
"在`ndarray`中所有元素必须是同一类型,否则会自动向下转换,`int->float->str`。\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.array([1, 2, 3, 4, 5])\n",
"print(a) # [1 2 3 4 5]\n",
"b = np.array([1, 2, 3, 4, '5'])\n",
"print(b) # ['1' '2' '3' '4' '5']\n",
"c = np.array([1, 2, 3, 4, 5.0])\n",
"print(c) # [1. 2. 3. 4. 5.]\n",
"```\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "307.188px"
},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -0,0 +1,169 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 常量\n",
"\n",
"## numpy.nan\n",
"\n",
"- 表示空值。\n",
"\n",
"```\n",
"nan = NaN = NAN\n",
"```\n",
"\n",
"\n",
"【例】两个`numpy.nan`是不相等的。\n",
"\n",
"```python\n",
"import numpy as np\n",
"\n",
"print(np.nan == np.nan) # False\n",
"print(np.nan != np.nan) # True\n",
"```\n",
"\n",
"- `numpy.isnan(x, *args, **kwargs)` Test element-wise for NaN and return result as a boolean array.\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([1, 1, 8, np.nan, 10])\n",
"print(x)\n",
"# [ 1. 1. 8. nan 10.]\n",
"\n",
"y = np.isnan(x)\n",
"print(y)\n",
"# [False False False True False]\n",
"\n",
"z = np.count_nonzero(y)\n",
"print(z) # 1\n",
"```\n",
"\n",
"\n",
"\n",
"## numpy.inf\n",
"- 表示正无穷大。\n",
"\n",
"```\n",
"Inf = inf = infty = Infinity = PINF\n",
"```\n",
"\n",
"【例】\n",
"```python\n",
"\n",
"```\n",
"\n",
"## numpy.pi\n",
"\n",
"- 表示圆周率\n",
"\n",
"```python\n",
"pi = 3.1415926535897932384626433...\n",
"```\n",
"\n",
"## numpy.e\n",
"\n",
"- 表示自然常数\n",
"\n",
"```python\n",
"e = 2.71828182845904523536028747135266249775724709369995...\n",
"```\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'numpy' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-0dfb6dd16de6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnumpy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'numpy' is not defined"
]
}
],
"source": [
"numpy.e"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -0,0 +1,250 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 数据类型\n",
"\n",
"## 常见数据类型\n",
"\n",
"Python 原生的数据类型相对较少, bool、int、float、str等。这在不需要关心数据在计算机中表示的所有方式的应用中是方便的。然而对于科学计算通常需要更多的控制。为了加以区分 numpy 在这些类型名称末尾都加了“_”。\n",
"\n",
"下表列举了常用 numpy 基本类型。\n",
"\n",
"\n",
"类型 | 备注 | 说明 \n",
"---|---|---\n",
"bool_ = bool8 | 8位 | 布尔类型\n",
"int8 = byte | 8位 | 整型\n",
"int16 = short |\t16位| 整型\n",
"int32 = intc | 32位| 整型\n",
"int_ = int64 = long = int0 = intp | 64位| 整型\n",
"uint8 = ubyte |8位 | 无符号整型\n",
"uint16 = ushort|16位| 无符号整型\n",
"uint32 = uintc|32位| 无符号整型\n",
"uint64 = uintp = uint0 = uint| 64位| 无符号整型\n",
"float16 = half|16位 | 浮点型\n",
"float32 = single| 32位| 浮点型\n",
"float_ = float64 = double| 64位| 浮点型\n",
"str_ = unicode_ = str0 = unicode| |Unicode 字符串\n",
"datetime64| |日期时间类型\n",
"timedelta64| |表示两个时间之间的间隔\n",
"\n",
"\n",
"\n",
"## 创建数据类型\n",
"\n",
"numpy 的数值类型实际上是 dtype 对象的实例。\n",
"\n",
"```python\n",
"class dtype(object):\n",
" def __init__(self, obj, align=False, copy=False):\n",
" pass\n",
"```\n",
"\n",
"\n",
"\n",
"\n",
"每个内建类型都有一个唯一定义它的字符代码,如下:\n",
"\n",
"字符 | \t对应类型|备注\n",
"---|---|---\n",
"b\t|boolean | 'b1'\n",
"i\t|signed integer | 'i1', 'i2', 'i4', 'i8'\n",
"u\t|unsigned integer | 'u1', 'u2' ,'u4' ,'u8'\n",
"f\t|floating-point | 'f2', 'f4', 'f8'\n",
"c\t|complex floating-point |\n",
"m\t|timedelta64 |表示两个时间之间的间隔\n",
"M\t|datetime64 |日期时间类型\n",
"O\t|object |\n",
"S\t|(byte-)string | S3表示长度为3的字符串\n",
"U\t|Unicode | Unicode 字符串\n",
"V\t|void\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.dtype('b1')\n",
"print(a.type) # <class 'numpy.bool_'>\n",
"print(a.itemsize) # 1\n",
"\n",
"a = np.dtype('i1')\n",
"print(a.type) # <class 'numpy.int8'>\n",
"print(a.itemsize) # 1\n",
"a = np.dtype('i2')\n",
"print(a.type) # <class 'numpy.int16'>\n",
"print(a.itemsize) # 2\n",
"a = np.dtype('i4')\n",
"print(a.type) # <class 'numpy.int32'>\n",
"print(a.itemsize) # 4\n",
"a = np.dtype('i8')\n",
"print(a.type) # <class 'numpy.int64'>\n",
"print(a.itemsize) # 8\n",
"\n",
"a = np.dtype('u1')\n",
"print(a.type) # <class 'numpy.uint8'>\n",
"print(a.itemsize) # 1\n",
"a = np.dtype('u2')\n",
"print(a.type) # <class 'numpy.uint16'>\n",
"print(a.itemsize) # 2\n",
"a = np.dtype('u4')\n",
"print(a.type) # <class 'numpy.uint32'>\n",
"print(a.itemsize) # 4\n",
"a = np.dtype('u8')\n",
"print(a.type) # <class 'numpy.uint64'>\n",
"print(a.itemsize) # 8\n",
"\n",
"a = np.dtype('f2')\n",
"print(a.type) # <class 'numpy.float16'>\n",
"print(a.itemsize) # 2\n",
"a = np.dtype('f4')\n",
"print(a.type) # <class 'numpy.float32'>\n",
"print(a.itemsize) # 4\n",
"a = np.dtype('f8')\n",
"print(a.type) # <class 'numpy.float64'>\n",
"print(a.itemsize) # 8\n",
"\n",
"a = np.dtype('S')\n",
"print(a.type) # <class 'numpy.bytes_'>\n",
"print(a.itemsize) # 0\n",
"a = np.dtype('S3')\n",
"print(a.type) # <class 'numpy.bytes_'>\n",
"print(a.itemsize) # 3\n",
"\n",
"a = np.dtype('U3')\n",
"print(a.type) # <class 'numpy.str_'>\n",
"print(a.itemsize) # 12\n",
"```\n",
"\n",
"## 数据类型信息\n",
"\n",
"Python 的浮点数通常是64位浮点数几乎等同于 `np.float64`。\n",
"\n",
"NumPy和Python整数类型的行为在整数溢出方面存在显着差异与 NumPy 不同Python 的`int` 是灵活的。这意味着Python整数可以扩展以容纳任何整数并且不会溢出。\n",
"\n",
"Machine limits for integer types.\n",
"```python\n",
"class iinfo(object):\n",
" def __init__(self, int_type):\n",
" pass\n",
" def min(self):\n",
" pass\n",
" def max(self):\n",
" pass\n",
"```\n",
"【例】\n",
"\n",
"```python\n",
"import numpy as np\n",
"\n",
"ii16 = np.iinfo(np.int16)\n",
"print(ii16.min) # -32768\n",
"print(ii16.max) # 32767\n",
"\n",
"ii32 = np.iinfo(np.int32)\n",
"print(ii32.min) # -2147483648\n",
"print(ii32.max) # 2147483647\n",
"```\n",
"\n",
"Machine limits for floating point types.\n",
"```python\n",
"class finfo(object):\n",
" def _init(self, dtype):\n",
"```\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"ff16 = np.finfo(np.float16)\n",
"print(ff16.bits) # 16\n",
"print(ff16.min) # -65500.0\n",
"print(ff16.max) # 65500.0\n",
"print(ff16.eps) # 0.000977\n",
"\n",
"ff32 = np.finfo(np.float32)\n",
"print(ff32.bits) # 32\n",
"print(ff32.min) # -3.4028235e+38\n",
"print(ff32.max) # 3.4028235e+38\n",
"print(ff32.eps) # 1.1920929e-07\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "218.2px"
},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -0,0 +1,356 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 时间日期和时间增量\n",
"\n",
"## datetime64 基础\n",
"\n",
"在 numpy 中,我们很方便的将字符串转换成时间日期类型 `datetime64``datetime` 已被 python 包含的日期时间库所占用)。\n",
"\n",
"`datatime64`是带单位的日期时间类型,其单位如下:\n",
"\n",
"日期单位 | 代码含义|时间单位 | 代码含义\n",
":---:|:---:|:---:|:---:\n",
"Y | 年 |h | 小时\n",
"M | 月 |m | 分钟\n",
"W | 周 |s | 秒\n",
"D | 天 |ms | 毫秒\n",
"- | - |us | 微秒\n",
"- | - |ns | 纳秒\n",
"- | - |ps | 皮秒\n",
"- | - |fs | 飞秒\n",
"- | - |as | 阿托秒\n",
"\n",
"注意:\n",
"- 1秒 = 1000 毫秒milliseconds\n",
"- 1毫秒 = 1000 微秒microseconds\n",
"\n",
"【例】从字符串创建 datetime64 类型时默认情况下numpy 会根据字符串自动选择对应的单位。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.datetime64('2020-03-01')\n",
"print(a, a.dtype) # 2020-03-01 datetime64[D]\n",
"\n",
"a = np.datetime64('2020-03')\n",
"print(a, a.dtype) # 2020-03 datetime64[M]\n",
"\n",
"a = np.datetime64('2020-03-08 20:00:05')\n",
"print(a, a.dtype) # 2020-03-08T20:00:05 datetime64[s]\n",
"\n",
"a = np.datetime64('2020-03-08 20:00')\n",
"print(a, a.dtype) # 2020-03-08T20:00 datetime64[m]\n",
"\n",
"a = np.datetime64('2020-03-08 20')\n",
"print(a, a.dtype) # 2020-03-08T20 datetime64[h]\n",
"```\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"【例】从字符串创建 datetime64 类型时,可以强制指定使用的单位。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.datetime64('2020-03', 'D')\n",
"print(a, a.dtype) # 2020-03-01 datetime64[D]\n",
"\n",
"a = np.datetime64('2020-03', 'Y')\n",
"print(a, a.dtype) # 2020 datetime64[Y]\n",
"\n",
"print(np.datetime64('2020-03') == np.datetime64('2020-03-01')) # True\n",
"print(np.datetime64('2020-03') == np.datetime64('2020-03-02')) #False\n",
"```\n",
"\n",
"由上例可以看出2019-03 和 2019-03-01 所表示的其实是同一个时间。\n",
"事实上,如果两个 datetime64 对象具有不同的单位,它们可能仍然代表相同的时刻。并且从较大的单位(如月份)转换为较小的单位(如天数)是安全的。\n",
"\n",
"\n",
"【例】从字符串创建 datetime64 数组时,如果单位不统一,则一律转化成其中最小的单位。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.array(['2020-03', '2020-03-08', '2020-03-08 20:00'], dtype='datetime64')\n",
"print(a, a.dtype)\n",
"# ['2020-03-01T00:00' '2020-03-08T00:00' '2020-03-08T20:00'] datetime64[m]\n",
"```\n",
"\n",
"\n",
"【例】使用`arange()`创建 datetime64 数组,用于生成日期范围。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.arange('2020-08-01', '2020-08-10', dtype=np.datetime64)\n",
"print(a)\n",
"# ['2020-08-01' '2020-08-02' '2020-08-03' '2020-08-04' '2020-08-05'\n",
"# '2020-08-06' '2020-08-07' '2020-08-08' '2020-08-09']\n",
"print(a.dtype) # datetime64[D]\n",
"\n",
"a = np.arange('2020-08-01 20:00', '2020-08-10', dtype=np.datetime64)\n",
"print(a)\n",
"# ['2020-08-01T20:00' '2020-08-01T20:01' '2020-08-01T20:02' ...\n",
"# '2020-08-09T23:57' '2020-08-09T23:58' '2020-08-09T23:59']\n",
"print(a.dtype) # datetime64[m]\n",
"\n",
"a = np.arange('2020-05', '2020-12', dtype=np.datetime64)\n",
"print(a)\n",
"# ['2020-05' '2020-06' '2020-07' '2020-08' '2020-09' '2020-10' '2020-11']\n",
"print(a.dtype) # datetime64[M]\n",
"```\n",
"\n",
"## datetime64 和 timedelta64 运算\n",
"\n",
"【例】timedelta64 表示两个 datetime64 之间的差。timedelta64 也是带单位的,并且和相减运算中的两个 datetime64 中的较小的单位保持一致。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.datetime64('2020-03-08') - np.datetime64('2020-03-07')\n",
"b = np.datetime64('2020-03-08') - np.datetime64('2020-03-07 08:00')\n",
"c = np.datetime64('2020-03-08') - np.datetime64('2020-03-07 23:00', 'D')\n",
"\n",
"print(a, a.dtype) # 1 days timedelta64[D]\n",
"print(b, b.dtype) # 956178240 minutes timedelta64[m]\n",
"print(c, c.dtype) # 1 days timedelta64[D]\n",
"\n",
"a = np.datetime64('2020-03') + np.timedelta64(20, 'D')\n",
"b = np.datetime64('2020-06-15 00:00') + np.timedelta64(12, 'h')\n",
"print(a, a.dtype) # 2020-03-21 datetime64[D]\n",
"print(b, b.dtype) # 2020-06-15T12:00 datetime64[m]\n",
"```\n",
"\n",
"【例】生成 timedelta64时要注意年'Y')和月('M')这两个单位无法和其它单位进行运算(一年有几天?一个月有几个小时?这些都是不确定的)。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.timedelta64(1, 'Y')\n",
"b = np.timedelta64(a, 'M')\n",
"print(a) # 1 years\n",
"print(b) # 12 months\n",
"\n",
"c = np.timedelta64(1, 'h')\n",
"d = np.timedelta64(c, 'm')\n",
"print(c) # 1 hours\n",
"print(d) # 60 minutes\n",
"\n",
"print(np.timedelta64(a, 'D'))\n",
"# TypeError: Cannot cast NumPy timedelta64 scalar from metadata [Y] to [D] according to the rule 'same_kind'\n",
"\n",
"print(np.timedelta64(b, 'D'))\n",
"# TypeError: Cannot cast NumPy timedelta64 scalar from metadata [M] to [D] according to the rule 'same_kind'\n",
"```\n",
"\n",
"\n",
"【例】timedelta64 的运算。\n",
"```python\n",
"import numpy as np\n",
"\n",
"a = np.timedelta64(1, 'Y')\n",
"b = np.timedelta64(6, 'M')\n",
"c = np.timedelta64(1, 'W')\n",
"d = np.timedelta64(1, 'D')\n",
"e = np.timedelta64(10, 'D')\n",
"\n",
"print(a) # 1 years\n",
"print(b) # 6 months\n",
"print(a + b) # 18 months\n",
"print(a - b) # 6 months\n",
"print(2 * a) # 2 years\n",
"print(a / b) # 2.0\n",
"print(c / d) # 7.0\n",
"print(c % e) # 7 days\n",
"```\n",
"\n",
"【例】numpy.datetime64 与 datetime.datetime 相互转换\n",
"```python\n",
"import numpy as np\n",
"import datetime\n",
"\n",
"dt = datetime.datetime(year=2020, month=6, day=1, hour=20, minute=5, second=30)\n",
"dt64 = np.datetime64(dt, 's')\n",
"print(dt64, dt64.dtype)\n",
"# 2020-06-01T20:05:30 datetime64[s]\n",
"\n",
"dt2 = dt64.astype(datetime.datetime)\n",
"print(dt2, type(dt2))\n",
"# 2020-06-01 20:05:30 <class 'datetime.datetime'>\n",
"```\n",
"\n",
"## datetime64 的应用\n",
"\n",
"为了允许在只有一周中某些日子有效的上下文中使用日期时间NumPy包含一组“busday”工作日功能。\n",
"\n",
"- `numpy.busday_offset(dates, offsets, roll='raise', weekmask='1111100', holidays=None, busdaycal=None, out=None)` First adjusts the date to fall on a valid day according to the roll rule, then applies offsets to the given dates counted in valid days.\n",
"\n",
"参数`roll`{'raise', 'nat', 'forward', 'following', 'backward', 'preceding', 'modifiedfollowing', 'modifiedpreceding'}\n",
"- 'raise' means to raise an exception for an invalid day.\n",
"- 'nat' means to return a NaT (not-a-time) for an invalid day.\n",
"- 'forward' and 'following' mean to take the first valid day later in time.\n",
"- 'backward' and 'preceding' mean to take the first valid day earlier in time.\n",
"\n",
"\n",
"【例】将指定的偏移量应用于工作日,单位天('D')。计算下一个工作日,如果当前日期为非工作日,默认报错。可以指定 `forward` 或 `backward` 规则来避免报错。(一个是向前取第一个有效的工作日,一个是向后取第一个有效的工作日)\n",
"```python\n",
"import numpy as np\n",
"\n",
"# 2020-07-10 星期五\n",
"a = np.busday_offset('2020-07-10', offsets=1)\n",
"print(a) # 2020-07-13\n",
"\n",
"a = np.busday_offset('2020-07-11', offsets=1)\n",
"print(a)\n",
"# ValueError: Non-business day date in busday_offset\n",
"\n",
"a = np.busday_offset('2020-07-11', offsets=0, roll='forward')\n",
"b = np.busday_offset('2020-07-11', offsets=0, roll='backward')\n",
"print(a) # 2020-07-13\n",
"print(b) # 2020-07-10\n",
"\n",
"a = np.busday_offset('2020-07-11', offsets=1, roll='forward')\n",
"b = np.busday_offset('2020-07-11', offsets=1, roll='backward')\n",
"print(a) # 2020-07-14\n",
"print(b) # 2020-07-13\n",
"```\n",
"\n",
"可以指定偏移量为 0 来获取当前日期向前或向后最近的工作日,当然,如果当前日期本身就是工作日,则直接返回当前日期。\n",
"\n",
"- `numpy.is_busday(dates, weekmask='1111100', holidays=None, busdaycal=None, out=None)` Calculates which of the given dates are valid days, and which are not.\n",
"\n",
"【例】返回指定日期是否是工作日。\n",
"```python\n",
"import numpy as np\n",
"\n",
"# 2020-07-10 星期五\n",
"a = np.is_busday('2020-07-10')\n",
"b = np.is_busday('2020-07-11')\n",
"print(a) # True\n",
"print(b) # False\n",
"```\n",
"\n",
"【例】统计一个 `datetime64[D]` 数组中的工作日天数。\n",
"```python\n",
"import numpy as np\n",
"\n",
"# 2020-07-10 星期五\n",
"begindates = np.datetime64('2020-07-10')\n",
"enddates = np.datetime64('2020-07-20')\n",
"a = np.arange(begindates, enddates, dtype='datetime64')\n",
"b = np.count_nonzero(np.is_busday(a))\n",
"print(a)\n",
"# ['2020-07-10' '2020-07-11' '2020-07-12' '2020-07-13' '2020-07-14'\n",
"# '2020-07-15' '2020-07-16' '2020-07-17' '2020-07-18' '2020-07-19']\n",
"print(b) # 6\n",
"```\n",
"\n",
"\n",
"【例】自定义周掩码值,即指定一周中哪些星期是工作日。\n",
"```python\n",
"import numpy as np\n",
"\n",
"# 2020-07-10 星期五\n",
"a = np.is_busday('2020-07-10', weekmask=[1, 1, 1, 1, 1, 0, 0])\n",
"b = np.is_busday('2020-07-10', weekmask=[1, 1, 1, 1, 0, 0, 1])\n",
"print(a) # True\n",
"print(b) # False\n",
"```\n",
"\n",
"- `numpy.busday_count(begindates, enddates, weekmask='1111100', holidays=[], busdaycal=None, out=None)`Counts the number of valid days between `begindates` and `enddates`, not including the day of `enddates`.\n",
"\n",
"【例】返回两个日期之间的工作日数量。\n",
"```python\n",
"import numpy as np\n",
"\n",
"# 2020-07-10 星期五\n",
"begindates = np.datetime64('2020-07-10')\n",
"enddates = np.datetime64('2020-07-20')\n",
"a = np.busday_count(begindates, enddates)\n",
"b = np.busday_count(enddates, begindates)\n",
"print(a) # 6\n",
"print(b) # -6\n",
"```\n",
"\n",
"\n",
"---\n",
"<b>参考图文</b>\n",
"\n",
"- https://www.jianshu.com/p/336cd77d9914\n",
"- https://www.cnblogs.com/gl1573/p/10549547.html#h2datetime64\n",
"- https://www.numpy.org.cn/reference/arrays/datetime.html#%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E5%8D%95%E4%BD%8D\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -548,9 +548,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python35",
"display_name": "Python 3",
"language": "python",
"name": "python35"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
@@ -562,7 +562,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
"version": "3.8.3"
},
"toc": {
"base_numbering": 1,

View File

@@ -84,9 +84,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python35",
"display_name": "Python 3",
"language": "python",
"name": "python35"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
@@ -98,7 +98,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
"version": "3.8.3"
},
"toc": {
"base_numbering": 1,

View File

@@ -181,9 +181,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python35",
"display_name": "Python 3",
"language": "python",
"name": "python35"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
@@ -195,7 +195,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
"version": "3.8.3"
},
"toc": {
"base_numbering": 1,

View File

@@ -292,9 +292,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python35",
"display_name": "Python 3",
"language": "python",
"name": "python35"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
@@ -306,7 +306,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
"version": "3.8.3"
},
"toc": {
"base_numbering": 1,

View File

@@ -548,9 +548,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python35",
"display_name": "Python 3",
"language": "python",
"name": "python35"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
@@ -562,7 +562,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
"version": "3.8.3"
},
"toc": {
"base_numbering": 1,

View File

@@ -755,9 +755,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python35",
"display_name": "Python 3",
"language": "python",
"name": "python35"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
@@ -782,7 +782,7 @@
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
"toc_window_display": false
},
"varInspector": {
"cols": {

View File

@@ -176,9 +176,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python35",
"display_name": "Python 3",
"language": "python",
"name": "python35"
"name": "python3"
},
"language_info": {
"codemirror_mode": {

View File

@@ -0,0 +1,596 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 副本与视图\n",
"在 Numpy 中,尤其是在做数组运算或数组操作时,返回结果不是数组的 **副本** 就是 **视图**。\n",
"\n",
"在 Numpy 中,所有赋值运算不会为数组和数组中的任何元素创建副本。\n",
"\n",
"\n",
"- `numpy.ndarray.copy()` 函数创建一个副本。 对副本数据进行修改,不会影响到原始数据,它们物理内存不在同一位置。\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
"y = x\n",
"y[0] = -1\n",
"print(x)\n",
"# [-1 2 3 4 5 6 7 8]\n",
"print(y)\n",
"# [-1 2 3 4 5 6 7 8]\n",
"\n",
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
"y = x.copy()\n",
"y[0] = -1\n",
"print(x)\n",
"# [1 2 3 4 5 6 7 8]\n",
"print(y)\n",
"# [-1 2 3 4 5 6 7 8]\n",
"```\n",
"\n",
"\n",
"\n",
"【例】数组切片操作返回的对象只是原数组的视图。\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"y = x\n",
"y[::2, :3:2] = -1\n",
"print(x)\n",
"# [[-1 12 -1 14 15]\n",
"# [16 17 18 19 20]\n",
"# [-1 22 -1 24 25]\n",
"# [26 27 28 29 30]\n",
"# [-1 32 -1 34 35]]\n",
"print(y)\n",
"# [[-1 12 -1 14 15]\n",
"# [16 17 18 19 20]\n",
"# [-1 22 -1 24 25]\n",
"# [26 27 28 29 30]\n",
"# [-1 32 -1 34 35]]\n",
"\n",
"x = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"y = x.copy()\n",
"y[::2, :3:2] = -1\n",
"print(x)\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]\n",
"# [21 22 23 24 25]\n",
"# [26 27 28 29 30]\n",
"# [31 32 33 34 35]]\n",
"print(y)\n",
"# [[-1 12 -1 14 15]\n",
"# [16 17 18 19 20]\n",
"# [-1 22 -1 24 25]\n",
"# [26 27 28 29 30]\n",
"# [-1 32 -1 34 35]]\n",
"```\n",
"\n",
"\n",
"# 索引与切片\n",
"\n",
"数组索引机制指的是用方括号([])加序号的形式引用单个数组元素,它的用处很多,比如抽取元素,选取数组的几个元素,甚至为其赋一个新值。\n",
"\n",
"## 整数索引\n",
"\n",
"【例】要获取数组的单个元素,指定元素的索引即可。\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
"print(x[2]) # 3\n",
"\n",
"x = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"print(x[2]) # [21 22 23 24 25]\n",
"print(x[2][1]) # 22\n",
"print(x[2, 1]) # 22\n",
"```\n",
"\n",
"## 切片索引\n",
"\n",
"切片操作是指抽取数组的一部分元素生成新数组。对 python **列表**进行切片操作得到的数组是原数组的**副本**,而对 **Numpy** 数据进行切片操作得到的数组则是指向相同缓冲区的**视图**。\n",
"\n",
"\n",
"如果想抽取(或查看)数组的一部分,必须使用切片语法,也就是,把几个用冒号( `start:stop:step` )隔开的数字置于方括号内。\n",
"\n",
"为了更好地理解切片语法还应该了解不明确指明起始和结束位置的情况。如省去第一个数字numpy 会认为第一个数字是0如省去第二个数字numpy 则会认为第二个数字是数组的最大索引值如省去最后一个数字它将会被理解为1也就是抽取所有元素而不再考虑间隔。\n",
"\n",
"\n",
"【例】对一维数组的切片\n",
"\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
"print(x[0:2]) # [1 2]\n",
"print(x[1:5:2]) # [2 4]\n",
"print(x[2:]) # [3 4 5 6 7 8]\n",
"print(x[:2]) # [1 2]\n",
"print(x[-2:]) # [7 8]\n",
"print(x[:-2]) # [1 2 3 4 5 6]\n",
"print(x[:]) # [1 2 3 4 5 6 7 8]\n",
"print(x[::-1]) # [8 7 6 5 4 3 2 1]\n",
"```\n",
"\n",
"【例】对二维数组切片\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"print(x[0:2])\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]]\n",
"\n",
"print(x[1:5:2])\n",
"# [[16 17 18 19 20]\n",
"# [26 27 28 29 30]]\n",
"\n",
"print(x[2:])\n",
"# [[21 22 23 24 25]\n",
"# [26 27 28 29 30]\n",
"# [31 32 33 34 35]]\n",
"\n",
"print(x[:2])\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]]\n",
"\n",
"print(x[-2:])\n",
"# [[26 27 28 29 30]\n",
"# [31 32 33 34 35]]\n",
"\n",
"print(x[:-2])\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]\n",
"# [21 22 23 24 25]]\n",
"\n",
"print(x[:])\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]\n",
"# [21 22 23 24 25]\n",
"# [26 27 28 29 30]\n",
"# [31 32 33 34 35]]\n",
"\n",
"print(x[2, :]) # [21 22 23 24 25]\n",
"print(x[:, 2]) # [13 18 23 28 33]\n",
"print(x[0, 1:4]) # [12 13 14]\n",
"print(x[1:4, 0]) # [16 21 26]\n",
"print(x[1:3, 2:4])\n",
"# [[18 19]\n",
"# [23 24]]\n",
"\n",
"print(x[:, :])\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]\n",
"# [21 22 23 24 25]\n",
"# [26 27 28 29 30]\n",
"# [31 32 33 34 35]]\n",
"\n",
"print(x[::2, ::2])\n",
"# [[11 13 15]\n",
"# [21 23 25]\n",
"# [31 33 35]]\n",
"\n",
"print(x[::-1, :])\n",
"# [[31 32 33 34 35]\n",
"# [26 27 28 29 30]\n",
"# [21 22 23 24 25]\n",
"# [16 17 18 19 20]\n",
"# [11 12 13 14 15]]\n",
"\n",
"print(x[:, ::-1])\n",
"# [[15 14 13 12 11]\n",
"# [20 19 18 17 16]\n",
"# [25 24 23 22 21]\n",
"# [30 29 28 27 26]\n",
"# [35 34 33 32 31]]\n",
"```\n",
"\n",
"\n",
"通过对每个以逗号分隔的维度执行单独的切片,你可以对多维数组进行切片。因此,对于二维数组,我们的第一片定义了行的切片,第二片定义了列的切片。\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"print(x)\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]\n",
"# [21 22 23 24 25]\n",
"# [26 27 28 29 30]\n",
"# [31 32 33 34 35]]\n",
"\n",
"x[0::2, 1::3] = 0\n",
"print(x)\n",
"# [[11 0 13 14 0]\n",
"# [16 17 18 19 20]\n",
"# [21 0 23 24 0]\n",
"# [26 27 28 29 30]\n",
"# [31 0 33 34 0]]\n",
"```\n",
"\n",
"## dots 索引\n",
"\n",
"NumPy 允许使用`...`表示足够多的冒号来构建完整的索引列表。\n",
"\n",
"比如,如果 `x` 是 5 维数组:\n",
"\n",
"- `x[1,2,...]` 等于 `x[1,2,:,:,:]`\n",
"- `x[...,3]` 等于 `x[:,:,:,:,3]` \n",
"- `x[4,...,5,:]` 等于 `x[4,:,:,5,:]`\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.random.randint(1, 100, [2, 2, 3])\n",
"print(x)\n",
"# [[[ 5 64 75]\n",
"# [57 27 31]]\n",
"# \n",
"# [[68 85 3]\n",
"# [93 26 25]]]\n",
"\n",
"print(x[1, ...])\n",
"# [[68 85 3]\n",
"# [93 26 25]]\n",
"\n",
"print(x[..., 2])\n",
"# [[75 31]\n",
"# [ 3 25]]\n",
"```\n",
"\n",
"\n",
"\n",
"## 整数数组索引\n",
"\n",
"【例】方括号内传入多个索引值,可以同时选择多个元素。\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
"r = [0, 1, 2]\n",
"print(x[r])\n",
"# [1 2 3]\n",
"\n",
"r = [0, 1, -1]\n",
"print(x[r])\n",
"# [1 2 8]\n",
"\n",
"x = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"\n",
"r = [0, 1, 2]\n",
"print(x[r])\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]\n",
"# [21 22 23 24 25]]\n",
"\n",
"r = [0, 1, -1]\n",
"print(x[r])\n",
"\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]\n",
"# [31 32 33 34 35]]\n",
"\n",
"r = [0, 1, 2]\n",
"c = [2, 3, 4]\n",
"y = x[r, c]\n",
"print(y)\n",
"# [13 19 25]\n",
"```\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
"r = np.array([[0, 1], [3, 4]])\n",
"print(x[r])\n",
"# [[1 2]\n",
"# [4 5]]\n",
"\n",
"x = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"\n",
"r = np.array([[0, 1], [3, 4]])\n",
"print(x[r])\n",
"# [[[11 12 13 14 15]\n",
"# [16 17 18 19 20]]\n",
"#\n",
"# [[26 27 28 29 30]\n",
"# [31 32 33 34 35]]]\n",
"\n",
"# 获取了 5X5 数组中的四个角的元素。\n",
"# 行索引是 [0,0] 和 [4,4],而列索引是 [0,4] 和 [0,4]。\n",
"r = np.array([[0, 0], [4, 4]])\n",
"c = np.array([[0, 4], [0, 4]])\n",
"y = x[r, c]\n",
"print(y)\n",
"# [[11 15]\n",
"# [31 35]]\n",
"```\n",
"\n",
"【例】可以借助切片`:`与整数数组组合。\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"\n",
"y = x[0:3, [1, 2, 2]]\n",
"print(y)\n",
"# [[12 13 13]\n",
"# [17 18 18]\n",
"# [22 23 23]]\n",
"```\n",
"\n",
"- `numpy. take(a, indices, axis=None, out=None, mode='raise')` Take elements from an array along an axis.\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
"r = [0, 1, 2]\n",
"print(np.take(x, r))\n",
"# [1 2 3]\n",
"\n",
"r = [0, 1, -1]\n",
"print(np.take(x, r))\n",
"# [1 2 8]\n",
"\n",
"x = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"\n",
"r = [0, 1, 2]\n",
"print(np.take(x, r, axis=0))\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]\n",
"# [21 22 23 24 25]]\n",
"\n",
"r = [0, 1, -1]\n",
"print(np.take(x, r, axis=0))\n",
"# [[11 12 13 14 15]\n",
"# [16 17 18 19 20]\n",
"# [31 32 33 34 35]]\n",
"\n",
"r = [0, 1, 2]\n",
"c = [2, 3, 4]\n",
"y = np.take(x, [r, c])\n",
"print(y)\n",
"# [[11 12 13]\n",
"# [13 14 15]]\n",
"```\n",
"\n",
"\n",
"## 布尔索引\n",
"\n",
"我们可以通过一个布尔数组来索引目标数组。\n",
"\n",
"【例】\n",
"\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
"y = x > 5\n",
"print(y)\n",
"# [False False False False False True True True]\n",
"print(x[x > 5])\n",
"# [6 7 8]\n",
"\n",
"x = np.array([np.nan, 1, 2, np.nan, 3, 4, 5])\n",
"y = np.logical_not(np.isnan(x))\n",
"print(x[y])\n",
"# [1. 2. 3. 4. 5.]\n",
"\n",
"x = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"y = x > 25\n",
"print(y)\n",
"# [[False False False False False]\n",
"# [False False False False False]\n",
"# [False False False False False]\n",
"# [ True True True True True]\n",
"# [ True True True True True]]\n",
"print(x[x > 25])\n",
"# [26 27 28 29 30 31 32 33 34 35]\n",
"```\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"import matplotlib.pyplot as plt\n",
"\n",
"x = np.linspace(0, 2 * np.pi, 50)\n",
"y = np.sin(x)\n",
"print(len(x)) # 50\n",
"plt.plot(x, y)\n",
"\n",
"mask = y >= 0\n",
"print(len(x[mask])) # 25\n",
"print(mask)\n",
"'''\n",
"[ True True True True True True True True True True True True\n",
" True True True True True True True True True True True True\n",
" True False False False False False False False False False False False\n",
" False False False False False False False False False False False False\n",
" False False]\n",
"'''\n",
"plt.plot(x[mask], y[mask], 'bo')\n",
"\n",
"mask = np.logical_and(y >= 0, x <= np.pi / 2)\n",
"print(mask)\n",
"'''\n",
"[ True True True True True True True True True True True True\n",
" True False False False False False False False False False False False\n",
" False False False False False False False False False False False False\n",
" False False False False False False False False False False False False\n",
" False False]\n",
"'''\n",
"\n",
"plt.plot(x[mask], y[mask], 'go')\n",
"plt.show()\n",
"```\n",
"\n",
"![](https://img-blog.csdnimg.cn/20191109183704335.png)\n",
"\n",
"我们利用这些条件来选择图上的不同点。蓝色点(在图中还包括绿点,但绿点掩盖了蓝色点),显示值 大于0 的所有点。绿色点表示值 大于0 且 小于0.5π 的所有点。\n",
"\n",
"\n",
"\n",
"\n",
"---\n",
"# 数组迭代\n",
"\n",
"除了for循环Numpy 还提供另外一种更为优雅的遍历方法。\n",
"\n",
"- `apply_along_axis(func1d, axis, arr)` Apply a function to 1-D slices along the given axis.\n",
"\n",
"【例】\n",
"```python\n",
"import numpy as np\n",
"\n",
"x = np.array([[11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20],\n",
" [21, 22, 23, 24, 25],\n",
" [26, 27, 28, 29, 30],\n",
" [31, 32, 33, 34, 35]])\n",
"\n",
"y = np.apply_along_axis(np.sum, 0, x)\n",
"print(y) # [105 110 115 120 125]\n",
"y = np.apply_along_axis(np.sum, 1, x)\n",
"print(y) # [ 65 90 115 140 165]\n",
"\n",
"y = np.apply_along_axis(np.mean, 0, x)\n",
"print(y) # [21. 22. 23. 24. 25.]\n",
"y = np.apply_along_axis(np.mean, 1, x)\n",
"print(y) # [13. 18. 23. 28. 33.]\n",
"\n",
"\n",
"def my_func(x):\n",
" return (x[0] + x[-1]) * 0.5\n",
"\n",
"\n",
"y = np.apply_along_axis(my_func, 0, x)\n",
"print(y) # [21. 22. 23. 24. 25.]\n",
"y = np.apply_along_axis(my_func, 1, x)\n",
"print(y) # [13. 18. 23. 28. 33.]\n",
"```\n",
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "307.2px"
},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -2,7 +2,9 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"scrolled": false
},
"source": [
"# 副本与视图\n",
"在 Numpy 中,尤其是在做数组运算或数组操作时,返回结果不是数组的 **副本** 就是 **视图**。\n",
@@ -121,12 +123,14 @@
"\n",
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
"print(x[0:2]) # [1 2]\n",
"#用下标0~5,以2为步长选取数组\n",
"print(x[1:5:2]) # [2 4]\n",
"print(x[2:]) # [3 4 5 6 7 8]\n",
"print(x[:2]) # [1 2]\n",
"print(x[-2:]) # [7 8]\n",
"print(x[:-2]) # [1 2 3 4 5 6]\n",
"print(x[:]) # [1 2 3 4 5 6 7 8]\n",
"#利用负数下标翻转数组\n",
"print(x[::-1]) # [8 7 6 5 4 3 2 1]\n",
"```\n",
"\n",
@@ -401,6 +405,28 @@
"# [13 14 15]]\n",
"```\n",
"\n",
"应注意使用切片索引到numpy数组时生成的数组视图将始终是原始数组的子数组,\n",
" 但是整数数组索引,不是其子数组,是形成新的数组。\n",
"切片索引\n",
"```python\n",
"import numpy as np\n",
"\n",
"a=np.array([[1,2],[3,4],[5,6]])\n",
"b=a[0:1,0:1]\n",
"b[0,0]=2\n",
"print(a[0,0]==b)\n",
"#[[True]]\n",
"```\n",
"整数数组索引\n",
"```python\n",
"import numpy as np\n",
"\n",
"a=np.array([[1,2],[3,4],[5,6]])\n",
"b=a[0,0]\n",
"b=2\n",
"print(a[0,0]==b)\n",
"#False\n",
"```\n",
"\n",
"## 布尔索引\n",
"\n",
@@ -523,13 +549,27 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python35",
"display_name": "Python 3",
"language": "python",
"name": "python35"
"name": "python3"
},
"language_info": {
"codemirror_mode": {

View File

@@ -56,9 +56,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python35",
"display_name": "Python 3",
"language": "python",
"name": "python35"
"name": "python3"
},
"language_info": {
"codemirror_mode": {