NumPy数值计算基础学习笔记
Author:萌狼蓝天
| data:2021年5月28日 | 微信公众号:萌狼蓝天 | 哔哩哔哩:萌狼蓝天 | CSDN :萌狼蓝天
| CSDN:萌狼蓝天 |
哔哩哔哩:萌狼蓝天 |
微信公众号:mllt9920 | 除此之外。该学习笔记若是出现在的其他博客、网站、则是未经授权的盗版。
[ 声明 ] | 该学习笔记由萌狼蓝天整理,免费公开,发布于CSDN(ID:萌狼蓝天),不欢迎任何人COPY转载。(可以收藏,留链接)
如有错误,欢迎指正
文章目录
(一)数组创建与数组基本操作
1.numpy.array
(1)使用numpy模块的array函数创建一维数组
import numpy as np
arr1=np.array([1,2,3,4])#创建一维数组
print('创建的数组为:',arr1)
创建的数组为: [1 2 3 4]
(2)使用numpy模块的array函数创建二维数组
import numpy as np
arr2=np.array([[1,2,3],[4,5,6],[7,8,9]])
print('创建的数组为:\n',arr2)
创建的数组为:
[[1 2 3]
[4 5 6]
[7 8 9]]
(3)查看数组维度
print('arr2数组维度为:',arr2.shape)#查看数组结构
arr2数组维度为: (3, 3)
(4)查看数组类型
print('数组类型为:',arr2.dtype)#查看数组类型
数组类型为: int32
(5)查看数组元素个数
print('数组元素个数为:',arr2.size)#查看元素个数
数组元素个数为: 9
(6)查看数组每个元素的大小
print('数组每个元素的大小为:',arr2.itemsize)#查看数组每个元素的大小
数组每个元素的大小为: 4
(7)重设数组shape属性
arr2.shape=3,3 #重新设置shape
print('重新设置shape后的arr2为:\n',arr2)
重新设置shape后的arr2为:
[[1 2 3]
[4 5 6]
[7 8 9]]
2.numpy.arange
####(1)使用numpy模块的arange函数创建数组
nums=np.arange(1,9,0.5)
print('使用arange创建的数组为:',nums)
使用arange创建的数组为: [1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7. 7.5 8. 8.5]
3.numpy.linspace
(1)使用numpy模块的linspace模块创建数组
numpy.linspace通过起始值,结束值和元素个数创建一维数组
nums=np.linspace(0,1,12)
print('使用linspce创建的数组为:',nums)
使用linspce创建的数组为: [0. 0.09090909 0.18181818 0.27272727 0.36363636 0.45454545
0.54545455 0.63636364 0.72727273 0.81818182 0.90909091 1. ]
4.numpy.logspace
(1)使用numpy模块logspace函数创建数组
使用numpy.logspace创建的数组是等比数列(一维数组)
print('使用logspace创建的数组为:',np.logspace(0,2,20))
使用logspace创建的数组为: [ 1. 1.27427499 1.62377674 2.06913808 2.6366509
3.35981829 4.2813324 5.45559478 6.95192796 8.8586679
11.28837892 14.38449888 18.32980711 23.35721469 29.76351442
37.92690191 48.32930239 61.58482111 78.47599704 100. ]
5.numpy.zeros
(1)使用numpy模块zeros函数创建数组
使用numpy.zeros创建的数组元素值全为0
print('使用zeros创建的数组为:',np.zeros((2,3)))
使用zeros创建的数组为: [[0. 0. 0.]
[0. 0. 0.]]
6.numpy.eye
(1)使用numpy模块eye函数创建数组
使用numpy.eye创建的数组是类似单位矩阵,主对角线上的元素为1,其他元素为0的数组
print('使用eye创建的数组为:',np.eye(3))
使用eye创建的数组为: [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
7.numpy.diag
(1)使用numpy模块diag函数创建数组
使用numpy.diag创建的数组是类似对角的数组,除对角线以外的其他元素全为0,对角线上的元素可以为0或者其他值
print('使用diag创建的数组为:',np.diag([1,2,3,4]))
使用diag创建的数组为: [[1 0 0 0]
[0 2 0 0]
[0 0 3 0]
[0 0 0 4]]
8.numpy.ones
(1)使用numpy模块ones函数创建数组
使用numpy.ones创建的数组是元素全为1的数组
print('使用ones创建的数组为:',np.ones((5,3)))
使用ones创建的数组为: [[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
(二)数组数据类型
1.数组数据类型的转换
(1)整型转为浮点型
print("result is:",np.float64(42))#整型转为浮点型
result is: 42.0
(2)浮点型转换为整型
print("result is:",np.int8(50.00)) # 浮点型转为整型
result is: 50
(3)整型转为布尔型
print("result is:",np.bool(42))#整型转为布尔型
result is: True
<ipython-input-131-20f51aa17a36>:1: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
print("result is:",np.bool(42))#整型转为布尔型
(4)整型转为布尔型
print("result is:",np.bool(0)) # 浮点型转为布尔型
result is: False
<ipython-input-132-baa6e092f026>:1: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
print("result is:",np.bool(0)) # 浮点型转为布尔型
(5)布尔型转换为浮点型
print("result is:",np.float(True))#布尔型转换为浮点型
result is: 1.0
<ipython-input-133-1d83aab87f9b>:1: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
print("result is:",np.float(True))#布尔型转换为浮点型
(6)浮点型转换为整型
print("result is:",np.float(False)) # 布尔型转换为浮点型
result is: 0.0
<ipython-input-134-6cff5da744ee>:1: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
print("result is:",np.float(False)) # 布尔型转换为浮点型
(三)生成随机数
(1)random:常见的生成随机数的方法
print("random 生成的随机数为:\n",np.random.random(20))
random 生成的随机数为:
[0.55791026 0.63477055 0.54116354 0.83027174 0.42348607 0.79176684
0.64617388 0.15483727 0.26771988 0.40054346 0.06375048 0.87844409
0.74775519 0.30153283 0.58807044 0.40101532 0.02335887 0.48903779
0.53704195 0.21920781]
(2)rand:生成服从均匀分布的随机数
print("rand 生成的随机数为:\n",np.random.rand(10,5))
rand 生成的随机数为:
[[0.47925172 0.02799311 0.40786315 0.54069018 0.04411974]
[0.86318353 0.95417633 0.37817788 0.2896957 0.93821463]
[0.67268173 0.1380039 0.65746221 0.23188345 0.37880424]
[0.18452551 0.25816607 0.95759322 0.27727783 0.18721676]
[0.95292517 0.74291523 0.40309373 0.94374932 0.92228902]
[0.43309873 0.30446723 0.09329081 0.55081681 0.47916364]
[0.3584203 0.39172761 0.76052451 0.39024365 0.57812365]
[0.09857758 0.17557483 0.20864483 0.28781192 0.87004018]
[0.9512888 0.01955258 0.30916712 0.97176433 0.23897355]
[0.48093503 0.40555043 0.98634328 0.15895443 0.66770778]]
(3)randn:生成服从正态分布的随机数
print("randn 生成的随机数为:\n",np.random.randn(10,5))
randn 生成的随机数为:
[[-0.76112335 -1.29554727 0.20443226 0.28725719 0.45780882]
[ 1.15042934 0.71026017 2.4854007 0.53654559 -0.74665367]
[ 1.18328206 0.10513728 -1.03066039 -0.38489429 -0.82075365]
[-0.52447026 -0.36709368 -0.2101057 0.31681531 -2.9162681 ]
[ 0.42938188 0.83432469 0.12873553 1.13837947 -1.13441527]
[ 0.58330087 -1.55108377 0.55288945 -1.23808854 1.60961539]
[ 0.6795389 -0.00752145 -1.09122978 -0.37458747 -0.27252843]
[-0.48179827 0.83922752 -0.12928557 1.48130922 -1.76047751]
[ 1.39814554 -0.24733519 -0.61242604 -1.56736942 2.31962423]
[ 0.84477241 -1.18142687 0.3659049 -0.11059641 1.00624283]]
(4)randint:生成给定上下限范围的随机数
print("randint 生成的随机数为:\n",np.random.randint(2,10,size=[2,5]))
randint 生成的随机数为:
[[7 8 5 9 4]
[9 2 9 9 6]]
上面的代码,返回值为最小值不低于2,最大值不高于10的2行5列数组
函数 | 说明-这些都是random模块常用随机数生成函数 |
---|---|
seed | 确定随机数生成的种子 |
permutation | 返回一个序列的随机排列或返回一个随机排列的范围 |
Shuffle | 对一个序列进行随机排序 |
binomial | 产生二项分布的随机数 |
normal | 产生正态(高斯)分布的随机数 |
Beta | 产生beta分布的随机数 |
chisquare | 产生卡方分布的随机数 |
gamma | 产生gamma分布的随机数 |
uniform | 产生在[0,1]中均匀分布的随机数 |
| CSDN:萌狼蓝天 |
哔哩哔哩:萌狼蓝天 |
微信公众号:mllt9920 | 除此之外。该学习笔记若是出现在的其他博客、网站、则是未经授权的盗版。