**
**
In [1]:
# 安装:pip install numpy
# ndarray结构要求数据类型一致,不一致就会自动向下转换
import numpy as np
In [2]:
# 输出帮助信息
print(help(np.info(np.add)))
In [3]:
# 将列表数据变成numpy类型
array = np.array([1,2,3,4])
array
Out[3]:
array([1, 2, 3, 4])
In [4]:
# 查看类型
type(array)
Out[4]:
numpy.ndarray
In [5]:
# 根据索引值获取
array[1]
Out[5]:
2
In [6]:
# 获取值,切片的时候前闭后开,所以会获取下标为1,2的值
array[1:3]
Out[6]:
array([2, 3])
In [7]:
# 负数表示从后往前数
array[0:-2]
Out[7]:
array([1, 2])
In [8]:
# 给数组中每一个数加1
array += 1
array
Out[8]:
array([2, 3, 4, 5])
In [9]:
array1 = array + 1
array1
Out[9]:
array([3, 4, 5, 6])
In [10]:
array + array1
Out[10]:
array([ 5, 7, 9, 11])
In [11]:
array * array1
Out[11]:
array([ 6, 12, 20, 30])
In [12]:
# 查看数据形状
array.shape
Out[12]:
(4,)
In [13]:
# 查看数据类型
array.dtype
Out[13]:
dtype('int32')
In [14]:
# 元素占几个字节
array.itemsize
Out[14]:
4
In [15]:
# 查看数据的个数
array.size
Out[15]:
4
In [16]:
# 维度
array.ndim
Out[16]:
1
In [17]:
# 填充,参数表示用哪个数字进行填充
array.fill(0)
array
Out[17]:
array([0, 0, 0, 0])
In [18]:
# 将列表数据变成numpy类型
array2 = np.array([[1,2,3],[4,5,6],[7,8,9]])
array2
Out[18]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [19]:
array2.shape
Out[19]:
(3, 3)
In [20]:
array2.size
Out[20]:
9
In [21]:
array2.ndim
Out[21]:
2
In [22]:
array2[1,1]
Out[22]:
5
In [23]:
array2[1,1] = 10
array2
Out[23]:
array([[ 1, 2, 3],
[ 4, 10, 6],
[ 7, 8, 9]])
In [24]:
# 获取第2行的数据
array2[1]
Out[24]:
array([ 4, 10, 6])
In [25]:
# 第一个参数表示全部的行,第二个参数表示第2列,即第2列的全部数据
array2[:,1]
Out[25]:
array([ 2, 10, 8])
In [26]:
# 第一个参数表示第1行,第二个参数表示第1,2列
array2[0,1:3]
Out[26]:
array([2, 3])
In [27]:
# 复制数据,这2个变量指向同一个值,一个变量修改了值,另外一个变量查看的时候值也会变
array3 = array2
array3
Out[27]:
array([[ 1, 2, 3],
[ 4, 10, 6],
[ 7, 8, 9]])
In [28]:
array3[1,1] = 100
array3
Out[28]:
array([[ 1, 2, 3],
[ 4, 100, 6],
[ 7, 8, 9]])
In [29]:
array2
Out[29]:
array([[ 1, 2, 3],
[ 4, 100, 6],
[ 7, 8, 9]])
In [30]:
# 复制数据,2个变量之间的值互不影响
array3 = array2.copy()
array3
Out[30]:
array([[ 1, 2, 3],
[ 4, 100, 6],
[ 7, 8, 9]])
In [31]:
array3[1,1] = 1000
array3
Out[31]:
array([[ 1, 2, 3],
[ 4, 1000, 6],
[ 7, 8, 9]])
In [32]:
array2
Out[32]:
array([[ 1, 2, 3],
[ 4, 100, 6],
[ 7, 8, 9]])
In [33]:
# 生成一维数据,在[0,100)中,每隔10个选取一个
a = np.arange(0,100,10)
a
Out[33]:
array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
In [34]:
m = np.array([0,2,0,0,1,5,0,0,7,0],dtype = bool)
m
Out[34]:
array([False, True, False, False, True, True, False, False, True,
False])
In [35]:
# 下标是False表示不显示这个值,True表示显示这个值
a[m]
Out[35]:
array([10, 40, 50, 80])
In [36]:
# 参数10表示生成10个数
r = np.random.rand(10)
r
Out[36]:
array([0.73174095, 0.25660127, 0.14302333, 0.19748141, 0.91501771,
0.0737786 , 0.42826993, 0.85637025, 0.9121782 , 0.150054 ])
In [37]:
m = r >0.5
m
Out[37]:
array([ True, False, False, False, True, False, False, True, True,
False])
In [38]:
r[m]
Out[38]:
array([0.73174095, 0.91501771, 0.85637025, 0.9121782 ])
In [39]:
# 找到r中大于0.5的下标
np.where(r > 0.5)
Out[39]:
(array([0, 4, 7, 8], dtype=int64),)
In [40]:
r[np.where(r > 0.5)]
Out[40]:
array([0.73174095, 0.91501771, 0.85637025, 0.9121782 ])
In [41]:
a = np.array([1,2,3],dtype = np.float32)
a
Out[41]:
array([1., 2., 3.], dtype=float32)
In [42]:
# 数据的个数
a.size
Out[42]:
3
In [43]:
# 每个元素的字节
a.itemsize
Out[43]:
4
In [44]:
# 所有元素的总字节
a.nbytes
Out[44]:
12
In [45]:
a = np.array([1,2.5,'str'],dtype = np.object)
a
c:\users\一\appdata\local\programs\python\python37\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
"""Entry point for launching an IPython kernel.
Out[45]:
array([1, 2.5, 'str'], dtype=object)
In [46]:
a = np.array([1,2,3,4])
a
Out[46]:
array([1, 2, 3, 4])
In [47]:
np.asarray(a,dtype = np.float32)
Out[47]:
array([1., 2., 3., 4.], dtype=float32)
In [48]:
a
Out[48]:
array([1, 2, 3, 4])
In [49]:
a2 = np.asarray(a,dtype = np.float32)
a2
Out[49]:
array([1., 2., 3., 4.], dtype=float32)
In [50]:
a
Out[50]:
array([1, 2, 3, 4])
In [51]:
a.astype(np.float32)
Out[51]:
array([1., 2., 3., 4.], dtype=float32)
In [52]:
a
Out[52]:
array([1, 2, 3, 4])
In [53]:
array2 = np.array([[1,2,3],[4,5,6]])
array2
Out[53]:
array([[1, 2, 3],
[4, 5, 6]])
In [54]:
# 求和
array2.sum()
Out[54]:
21
In [55]:
np.sum(array2)
Out[55]:
21
In [56]:
# 按维度求和,负数表示从后面维度开始数
np.sum(array2,axis=0)
Out[56]:
array([5, 7, 9])
In [57]:
np.sum(array2,axis=1)
Out[57]:
array([ 6, 15])
In [58]:
np.sum(array2,axis=-1)
Out[58]:
array([ 6, 15])
In [59]:
array2.sum(axis=0)
Out[59]:
array([5, 7, 9])
In [60]:
# 所有数相乘
array2.prod()
Out[60]:
720
In [61]:
# 按维度相乘
array2.prod(axis=1)
Out[61]:
array([ 6, 120])
In [62]:
# 找到最小值
array2.min()
Out[62]:
1
In [63]:
# 找到最大值
array2.max()
Out[63]:
6
In [64]:
# 按维度寻找
array2.max(axis=1)
Out[64]:
array([3, 6])
In [65]:
# 索引位置
array2.argmin()
Out[65]:
0
In [66]:
array2.argmax()
Out[66]:
5
In [67]:
array2.argmin(axis=0)
Out[67]:
array([0, 0, 0], dtype=int64)
In [68]:
array2.argmax(axis=1)
Out[68]:
array([2, 2], dtype=int64)
In [69]:
# 平均值
array2.mean()
Out[69]:
3.5
In [70]:
array2.mean(axis=1)
Out[70]:
array([2., 5.])
In [71]:
# 标准差
array2.std()
Out[71]:
1.707825127659933
In [72]:
# 标准差
array2.std(axis=1)
Out[72]:
array([0.81649658, 0.81649658])
In [73]:
# 方差
array2.var()
Out[73]:
2.9166666666666665
In [74]:
array2.var(axis=1)
Out[74]:
array([0.66666667, 0.66666667])
In [75]:
array2
Out[75]:
array([[1, 2, 3],
[4, 5, 6]])
In [76]:
# 小于2的值都变成2,大于4的值都变成4
array2.clip(2,4)
Out[76]:
array([[2, 2, 3],
[4, 4, 4]])
In [77]:
a = np.array([[1.1,2.258,3.3],[4.4,5.5,6.672]])
a
Out[77]:
array([[1.1 , 2.258, 3.3 ],
[4.4 , 5.5 , 6.672]])
In [78]:
# 四舍五入
a.round()
Out[78]:
array([[1., 2., 3.],
[4., 6., 7.]])
In [79]:
# 设置精度
a.round(decimals=1)
Out[79]:
array([[1.1, 2.3, 3.3],
[4.4, 5.5, 6.7]])
In [80]:
a = np.array([[2,1,4],[1,9,6]])
a
Out[80]:
array([[2, 1, 4],
[1, 9, 6]])
In [81]:
# 排序
np.sort(a)
Out[81]:
array([[1, 2, 4],
[1, 6, 9]])
In [82]:
# 按维度排序
np.sort(a,axis=0)
Out[82]:
array([[1, 1, 4],
[2, 9, 6]])
In [83]:
# 返回原来的索引值
np.argsort(a)
Out[83]:
array([[1, 0, 2],
[0, 2, 1]], dtype=int64)
In [84]:
# 等分取值
a = np.linspace(0,10,10)
a
Out[84]:
array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444,
5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ])
In [85]:
v = np.array([3,6,9])
# 排序好的数组才可以使用
np.searchsorted(a,v)
Out[85]:
array([3, 6, 9], dtype=int64)
In [86]:
a = np.array([[3,6,1],[5,7,6],[2,9,4]])
a
Out[86]:
array([[3, 6, 1],
[5, 7, 6],
[2, 9, 4]])
In [87]:
# 第一列降序,第三列升序
i = np.lexsort([-1*a[:,0],a[:,2]])
i
Out[87]:
array([0, 2, 1], dtype=int64)
In [88]:
a[i]
Out[88]:
array([[3, 6, 1],
[2, 9, 4],
[5, 7, 6]])
In [89]:
a = np.arange(10)
a
Out[89]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [90]:
a.shape
Out[90]:
(10,)
In [91]:
# 修改数据形状
a.shape = 2,5
In [92]:
a
Out[92]:
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
In [93]:
a.reshape(1,10)
Out[93]:
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
In [94]:
a = np.arange(10)
a
Out[94]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [95]:
a.shape
Out[95]:
(10,)
In [96]:
a = a[np.newaxis,:]
a.shape
Out[96]:
(1, 10)
In [97]:
a = np.arange(10)
a
Out[97]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [98]:
a.shape
Out[98]:
(10,)
In [99]:
a = a[:,np.newaxis]
a.shape
Out[99]:
(10, 1)
In [100]:
a = a[:,np.newaxis,np.newaxis]
a.shape
Out[100]:
(10, 1, 1, 1)
In [101]:
# 压缩数据形状
a = a.squeeze()
a.shape
Out[101]:
(10,)
In [102]:
a.shape = 2,5
a
Out[102]:
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
In [103]:
# 转制
a.transpose()
Out[103]:
array([[0, 5],
[1, 6],
[2, 7],
[3, 8],
[4, 9]])
In [104]:
a = np.arange(10)
a
Out[104]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [105]:
a.shape = 2,5
a
Out[105]:
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
In [106]:
# 转置,不会改变原来数据形状
a.T
Out[106]:
array([[0, 5],
[1, 6],
[2, 7],
[3, 8],
[4, 9]])
In [107]:
a
Out[107]:
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
In [108]:
a = np.array([[1,2,3,2],[4,5,4,6],[7,8,4,9]])
a
Out[108]:
array([[1, 2, 3, 2],
[4, 5, 4, 6],
[7, 8, 4, 9]])
In [109]:
b = np.array([[1,12,13,2],[45,5,44,6],[17,8,4,9]])
b
Out[109]:
array([[ 1, 12, 13, 2],
[45, 5, 44, 6],
[17, 8, 4, 9]])
In [110]:
# 拼接数据
c = np.concatenate((a,b))
c
Out[110]:
array([[ 1, 2, 3, 2],
[ 4, 5, 4, 6],
[ 7, 8, 4, 9],
[ 1, 12, 13, 2],
[45, 5, 44, 6],
[17, 8, 4, 9]])
In [111]:
# 拼接数据,指定在哪个维度
c = np.concatenate((a,b),axis=1)
c
Out[111]:
array([[ 1, 2, 3, 2, 1, 12, 13, 2],
[ 4, 5, 4, 6, 45, 5, 44, 6],
[ 7, 8, 4, 9, 17, 8, 4, 9]])
In [112]:
np.vstack((a,b))
Out[112]:
array([[ 1, 2, 3, 2],
[ 4, 5, 4, 6],
[ 7, 8, 4, 9],
[ 1, 12, 13, 2],
[45, 5, 44, 6],
[17, 8, 4, 9]])
In [113]:
np.hstack((a,b))
Out[113]:
array([[ 1, 2, 3, 2, 1, 12, 13, 2],
[ 4, 5, 4, 6, 45, 5, 44, 6],
[ 7, 8, 4, 9, 17, 8, 4, 9]])
In [114]:
a
Out[114]:
array([[1, 2, 3, 2],
[4, 5, 4, 6],
[7, 8, 4, 9]])
In [115]:
# 数据变成一维的,不会改变原数据
a.flatten()
Out[115]:
array([1, 2, 3, 2, 4, 5, 4, 6, 7, 8, 4, 9])
In [116]:
a
Out[116]:
array([[1, 2, 3, 2],
[4, 5, 4, 6],
[7, 8, 4, 9]])
In [117]:
# 数据变成一维的
a.ravel()
Out[117]:
array([1, 2, 3, 2, 4, 5, 4, 6, 7, 8, 4, 9])
In [118]:
# 获取10个数
np.arange(10)
Out[118]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [119]:
# 在[3,10)上每隔2个显示一个
np.arange(3,10,2)
Out[119]:
array([3, 5, 7, 9])
In [120]:
# 同时设置数据类型
np.arange(3,10,2,dtype=np.float32)
Out[120]:
array([3., 5., 7., 9.], dtype=float32)
In [121]:
# 等分显示数据
np.linspace(1,20,5)
Out[121]:
array([ 1. , 5.75, 10.5 , 15.25, 20. ])
In [122]:
# 以10为底
np.logspace(0,1,5)
Out[122]:
array([ 1. , 1.77827941, 3.16227766, 5.62341325, 10. ])
In [123]:
x = np.linspace(-10,10,5)
x
Out[123]:
array([-10., -5., 0., 5., 10.])
In [124]:
y = np.linspace(-10,10,5)
y
Out[124]:
array([-10., -5., 0., 5., 10.])
In [125]:
# 网格显示
x,y = np.meshgrid(x,y)
In [126]:
x
Out[126]:
array([[-10., -5., 0., 5., 10.],
[-10., -5., 0., 5., 10.],
[-10., -5., 0., 5., 10.],
[-10., -5., 0., 5., 10.],
[-10., -5., 0., 5., 10.]])
In [127]:
y
Out[127]:
array([[-10., -10., -10., -10., -10.],
[ -5., -5., -5., -5., -5.],
[ 0., 0., 0., 0., 0.],
[ 5., 5., 5., 5., 5.],
[ 10., 10., 10., 10., 10.]])
In [128]:
# 按行
np.r_[1:4:1]
Out[128]:
array([1, 2, 3])
In [129]:
# 按列
np.c_[1:8:2]
Out[129]:
array([[1],
[3],
[5],
[7]])
In [130]:
# 构建全0的
np.zeros(4)
Out[130]:
array([0., 0., 0., 0.])
In [131]:
np.zeros((3,4))
Out[131]:
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
In [132]:
# 构建全1的
np.ones((2,3))
Out[132]:
array([[1., 1., 1.],
[1., 1., 1.]])
In [133]:
np.ones((2,3)) *5
Out[133]:
array([[5., 5., 5.],
[5., 5., 5.]])
In [134]:
np.ones((2,3),dtype=np.int32)
Out[134]:
array([[1, 1, 1],
[1, 1, 1]])
In [135]:
# 构建空的,会随机填充一些东西
a = np.empty(5)
a
Out[135]:
array([-10., -5., 0., 5., 10.])
In [136]:
# 用3填充
a.fill(3)
a
Out[136]:
array([3., 3., 3., 3., 3.])
In [137]:
a = np.array([1,2,3,4])
a
Out[137]:
array([1, 2, 3, 4])
In [138]:
# 根据某个数据来构建给他形状一样的东西
np.zeros_like(a)
Out[138]:
array([0, 0, 0, 0])
In [139]:
np.ones_like(a)
Out[139]:
array([1, 1, 1, 1])
In [140]:
# 单位矩阵
np.identity(4)
Out[140]:
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
In [141]:
x = np.array([5,5])
y = np.array([2,2])
In [142]:
x.shape
Out[142]:
(2,)
In [143]:
y.shape
Out[143]:
(2,)
In [144]:
# 向量乘法
np.multiply(x,y)
Out[144]:
array([10, 10])
In [145]:
# 矩阵乘法
np.dot(x,y)
Out[145]:
20
In [146]:
x.shape = 2,1
In [147]:
x
Out[147]:
array([[5],
[5]])
In [148]:
# 形状不一样不能
np.dot(x,y)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_16616\1585282532.py in
1 # 形状不一样不能
----> 2 np.dot(x,y)
<__array_function__ internals> in dot(*args, **kwargs)
ValueError: shapes (2,1) and (2,) not aligned: 1 (dim 1) != 2 (dim 0)
In [149]:
x
Out[149]:
array([[5],
[5]])
In [150]:
y.shape = 1,2
In [151]:
y
Out[151]:
array([[2, 2]])
In [152]:
print(x.shape)
print(y.shape)
(2, 1)
(1, 2)
In [153]:
np.dot(x,y)
Out[153]:
array([[10, 10],
[10, 10]])
In [154]:
np.dot(y,x)
Out[154]:
array([[20]])
In [155]:
# 默认会自动补全
x = np.array([1,1,1,1])
y = np.array([[1,2,3,4],[8,9,5,6]])
x * y
Out[155]:
array([[1, 2, 3, 4],
[8, 9, 5, 6]])
In [156]:
x = np.array([1,1,1,1])
y = np.array([1,2,3,4])
x == y
Out[156]:
array([ True, False, False, False])
In [157]:
x = np.array([1,1,1])
y = np.array([1,2,3,4])
x == y
c:\users\一\appdata\local\programs\python\python37\lib\site-packages\ipykernel_launcher.py:3: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
This is separate from the ipykernel package so we can avoid doing imports until
Out[157]:
False
In [158]:
x = np.array([1,1,0])
y = np.array([1,2,3])
np.logical_and(x,y)
Out[158]:
array([ True, True, False])
In [159]:
np.logical_or(x,y)
Out[159]:
array([ True, True, True])
In [160]:
np.logical_not(x,y)
Out[160]:
array([0, 0, 1])
In [161]:
# 3行2列的随机数
np.random.rand(3,2)
Out[161]:
array([[0.60028871, 0.51878556],
[0.01137581, 0.57996631],
[0.89317575, 0.42156693]])
In [162]:
# 产生一个随机数
np.random.rand()
Out[162]:
0.9120995891601261
In [163]:
np.random.random_sample()
Out[163]:
0.23406011017882655
In [164]:
# 产生10以下的2行3列的数据
np.random.randint(10,size = (2,3))
Out[164]:
array([[0, 1, 1],
[2, 8, 5]])
In [165]:
# 产生一个随机整数
np.random.randint(10)
Out[165]:
3
In [166]:
# 在0-10产生4个随机数
np.random.randint(0,10,4)
Out[166]:
array([7, 3, 8, 9])
In [167]:
# 均值,方差 高斯分布
m,s = 0,0.2
np.random.normal(m,s,5)
Out[167]:
array([-0.25670771, 0.14098329, -0.02476364, -0.29993815, -0.01253749])
In [168]:
# 设置输出的精度
np.set_printoptions(precision = 2)
# 均值,方差 高斯分布
m,s = 0,0.2
np.random.normal(m,s,5)
Out[168]:
array([-0.19, 0.03, 0.08, 0.37, 0.19])
In [169]:
a = np.arange(10)
a
Out[169]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [170]:
# 打乱数据
np.random.shuffle(a)
a
Out[170]:
array([9, 7, 5, 1, 0, 8, 4, 6, 2, 3])
In [171]:
# 随机种子,设置之后执行产生的随机数都一样,不会变化
# 用在对比实验中,观察某一参数的时候,需要保证其他的数据(包括随机数)都是不变的
np.random.seed(5)
m,s = 0,0.2
np.random.normal(m,s,5)
Out[171]:
array([ 0.09, -0.07, 0.49, -0.05, 0.02])
In [172]:
# 创建文件,把下面的数据加进去
%%writefile a.txt
1 2 3 4 5 6
7 8 9 2 3 1
File "C:\Users\一\AppData\Local\Temp\ipykernel_16616\1079126928.py", line 3
1 2 3 4 5 6
^
SyntaxError: invalid syntax
In [173]:
# 读取数据
data = []
with open('a.txt') as f:
for line in f:
fileds = line.split()
c_data = [float(x) for x in fileds]
data.append(c_data)
data = np.array(data)
data
Out[173]:
array([[1., 2., 3., 4., 5., 6.],
[7., 8., 9., 2., 3., 1.]])
In [174]:
# 读取数据
data = np.loadtxt('a.txt')
data
Out[174]:
array([[1., 2., 3., 4., 5., 6.],
[7., 8., 9., 2., 3., 1.]])
In [175]:
%%writefile b.txt
1,2,3,4,5,6
7,8,9,2,3,1
Overwriting b.txt
In [176]:
# 读取数据,指定分隔符,默认是以空格为分隔符
data = np.loadtxt('b.txt',delimiter = ',')
data
Out[176]:
array([[1., 2., 3., 4., 5., 6.],
[7., 8., 9., 2., 3., 1.]])
In [177]:
%%writefile c.txt
a,b,c,d,e,f
1,2,3,4,5,6
7,8,9,2,3,1
Overwriting c.txt
In [178]:
# 跳过某些行
data = np.loadtxt('c.txt',delimiter = ',',skiprows = 1)
data
Out[178]:
array([[1., 2., 3., 4., 5., 6.],
[7., 8., 9., 2., 3., 1.]])
In [179]:
# 指定输出的列
data = np.loadtxt('c.txt',delimiter = ',',skiprows = 1,usecols = (1,3,5))
data
Out[179]:
array([[2., 4., 6.],
[8., 2., 1.]])
In [180]:
# 保存数据
a = np.array([[1,2,3],[4,5,6]])
np.savetxt('d.txt',a,fmt='%d',delimiter=',')
In [181]:
# 保留2位小数进行保存
a = np.array([[1,2,3],[4,5,6]])
np.savetxt('d.txt',a,fmt='%.2f',delimiter=',')
In [182]:
# 读写array结构
a = np.array([[1,2,3],[4,5,6]])
np.save('a.npy',a)
In [183]:
# 读取数据
a = np.load('a.npy')
a
Out[183]:
array([[1, 2, 3],
[4, 5, 6]])
In [184]:
b = np.arange(10)
b
Out[184]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [185]:
# 保存为压缩文件
np.savez('a.npz',a=a,b=b)
In [186]:
data = np.load('a.npz')
In [187]:
# 输出关键字
[i for i in data.keys()]
Out[187]:
['a', 'b']
In [188]:
# 根据关键字获取值
data['a']
Out[188]:
array([[1, 2, 3],
[4, 5, 6]])
In [189]:
data['b']
Out[189]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [190]:
a = np.arange(10,20)
a
Out[190]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
In [191]:
# 逆序
a = a[::-1]
a
Out[191]:
array([19, 18, 17, 16, 15, 14, 13, 12, 11, 10])
In [192]:
# 值不为0的索引
a = np.array([1,2,0,5,0,8,0])
np.nonzero(a)
Out[192]:
(array([0, 1, 3, 5], dtype=int64),)
In [193]:
a = np.ones((4,4))
a
Out[193]:
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
In [194]:
# 在外圈填充0
a = np.pad(a,pad_width=1,mode='constant',constant_values=0)
a
Out[194]:
array([[0., 0., 0., 0., 0., 0.],
[0., 1., 1., 1., 1., 0.],
[0., 1., 1., 1., 1., 0.],
[0., 1., 1., 1., 1., 0.],
[0., 1., 1., 1., 1., 0.],
[0., 0., 0., 0., 0., 0.]])
In [195]:
# 找到shape(6,7,8)为,找到第100个元素的的索引值
np.unravel_index(100,(6,7,8))
Out[195]:
(1, 5, 4)
In [196]:
# 输入都有的数
a = np.random.randint(0,10,10)
b = np.random.randint(0,10,10)
print(a)
print(b)
print(np.intersect1d(a,b)) # 数字1d,不是字母l
[7 1 5 7 0 1 4 6 2 9]
[9 9 9 1 2 7 0 5 0 0]
[0 1 2 5 7 9]
In [197]:
# 日期
y = np.datetime64('today','D') - np.timedelta64(1,'D')
n = np.datetime64('today','D')
t = np.datetime64('today','D') + np.timedelta64(1,'D')
print(y)
print(n)
print(t)
2022-12-16
2022-12-17
2022-12-18
In [198]:
# 一定范围的日期
np.arange('2022-11','2022-12',dtype='datetime64[D]')
Out[198]:
array(['2022-11-01', '2022-11-02', '2022-11-03', '2022-11-04',
'2022-11-05', '2022-11-06', '2022-11-07', '2022-11-08',
'2022-11-09', '2022-11-10', '2022-11-11', '2022-11-12',
'2022-11-13', '2022-11-14', '2022-11-15', '2022-11-16',
'2022-11-17', '2022-11-18', '2022-11-19', '2022-11-20',
'2022-11-21', '2022-11-22', '2022-11-23', '2022-11-24',
'2022-11-25', '2022-11-26', '2022-11-27', '2022-11-28',
'2022-11-29', '2022-11-30'], dtype='datetime64[D]')
In [199]:
a = np.random.uniform(0,10,10)
a
Out[199]:
array([1.44, 1.66, 9.64, 9.6 , 1.88, 0.24, 2.05, 7. , 7.8 , 0.23])
In [200]:
# 向下取整
np.floor(a)
Out[200]:
array([1., 1., 9., 9., 1., 0., 2., 6., 7., 0.])
In [201]:
# 设置只读
a = np.array([1,2,3])
a.flags.writeable = False
a[0]=5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_16616\4169285836.py in
2 a = np.array([1,2,3])
3 a.flags.writeable = False
----> 4 a[0]=5
ValueError: assignment destination is read-only
In [202]:
# 修改类型
a = np.array([1,2,3])
print(a.dtype)
a = a.astype(np.float32)
print(a.dtype)
int32
float32
In [203]:
# 索引和值
a = np.arange(9).reshape(3,3)
for i,v in np.ndenumerate(a):
print(i,v)
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
In [204]:
# 按某一列进行排序
a = np.random.randint(0,10,(3,3))
print(a)
print(a[a[:,1].argsort()])
[[3 1 7]
[3 1 9]
[5 7 0]]
[[3 1 7]
[3 1 9]
[5 7 0]]
In [205]:
# 数字出现的个数,[0出现的个数,1出现的个数...]
a = np.array([1,2,1,2,5,4,3,8])
np.bincount(a)
Out[205]:
array([0, 2, 2, 1, 1, 1, 0, 0, 1], dtype=int64)
In [206]:
a = np.random.randint(0,10,(4,4,4,4))
r = a.sum(axis=(-2,-1))
print(r)
[[71 75 56 89]
[52 76 48 78]
[66 89 69 59]
[94 83 56 72]]
In [207]:
a = np.arange(25).reshape(5,5)
print(a)
a[[0,1]]=a[[1,0]]
print(a)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
[[ 5 6 7 8 9]
[ 0 1 2 3 4]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
In [208]:
# 查找最大的数,速度较快
a = np.arange(10000)
np.random.shuffle(a)
n = 5
print(a[np.argpartition(-a,n)[:n]])
[9998 9997 9996 9995 9999]
In [209]:
a = np.array([1,2,4])
b = np.array([1,2,3])
np.all(a == b)
Out[209]:
False
In [210]:
np.any(a == b)
Out[210]:
True
In [211]:
a = np.random.randint(0,2,(5,3))
a
Out[211]:
array([[0, 0, 1],
[1, 1, 1],
[1, 0, 1],
[1, 1, 1],
[1, 0, 1]])
In [212]:
np.all(a[:,1:] == a[:,:-1],axis = 1)
Out[212]:
array([False, True, False, True, False])