# 第一天学习内容
- numpy数组的创建,相关概念
- 空值、多维数组的创建
- 注意括号形式,多维数组每行元素个数相等
>>> import numpy as np
>>> a=np.arange(15).reshape(3,5)
>>> a.shape
(3, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> a.ndim
2
>>> a.dtype.name
'int32'
>>> a.itemsize
4
>>> a.size
15
>>> a=np.array([1,2,3])
>>> a.dtype
dtype('int32')
>>> a=np.array([(1.0,3),(4,5)])
>>> a
array([[1., 3.],
[4., 5.]])
>>> a.dtype
dtype('float64')
>>> a=np.array([(1,2),(3,4,5)])
>>> a
array([(1, 2), (3, 4, 5)], dtype=object)
>>> np.zeros((3,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
>>> np.ones((2,3,4),dtype=np.int16)
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]], dtype=int16)
>>> np.arange(0.1,1.6,0.4)
array([0.1, 0.5, 0.9, 1.3])
>>> x=np.linspace(0,2*np.pi,10)
>>> x
array([0. , 0.6981317 , 1.3962634 , 2.0943951 , 2.7925268 ,
3.4906585 , 4.1887902 , 4.88692191, 5.58505361, 6.28318531])
>>> np.sin(x)
array([ 0.00000000e+00, 6.42787610e-01, 9.84807753e-01, 8.66025404e-01,
3.42020143e-01, -3.42020143e-01, -8.66025404e-01, -9.84807753e-01,
-6.42787610e-01, -2.44929360e-16])
## 等分数组
>>> np.linspace(0,3,3)
array([0. , 1.5, 3. ])
>>> np.arange(10000).reshape(100,100)
array([[ 0, 1, 2, ..., 97, 98, 99],
[ 100, 101, 102, ..., 197, 198, 199],
[ 200, 201, 202, ..., 297, 298, 299],
...,
[9700, 9701, 9702, ..., 9797, 9798, 9799],
[9800, 9801, 9802, ..., 9897, 9898, 9899],
[9900, 9901, 9902, ..., 9997, 9998, 9999]])
## 不省略打印数组
>>> import sys;np.set_printoptions(threshold=sys.maxsize)
>>> np.arange(100).reshape(10,10)
array([[ 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, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
## 注意函数参数是元组还是直接传参
>>> a=np.ones((2,3))
>>> a.dtype
dtype('float64')
>>> a.dtype=np.int16
>>> a.dtype
dtype('int16')
>>> a*=4
>>> a
array([[ 0, 0, 0, -64, 0, 0, 0, -64, 0, 0, 0, -64],
[ 0, 0, 0, -64, 0, 0, 0, -64, 0, 0, 0, -64]],
dtype=int16)
>>> a.size
24
>>> a=np.ones((2,3),dtype=int) # (2,3)只是一个参数
>>> a.dtype
dtype('int32')
## 数组直接乘法
>>> a*=3
>>> a
array([[3, 3, 3],
[3, 3, 3]])
>>> b=np.random.random((2,3))
>>> b.dtype
dtype('float64')
## 数组直接数学操作
>>> a+=b
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
a+=b
numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'add' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'
>>> a=np.ones(3,dtype=np.int32)
>>> b=np.linspace(0,np.pi,3)
>>> c=a+b
>>> c
array([1. , 2.57079633, 4.14159265])
>>> d=np.exp(c*1j)
>>> d.dtype
dtype('complex128')
## 数组数学方法
>>> a=np.random.random((2,3))
>>> a
array([[0.02704919, 0.38012895, 0.2666206 ],
[0.61326647, 0.35950685, 0.41460935]])
>>> a.sum()
2.061181408183111
>>> a.min()
0.027049190424122016
>>> a.max()
0.6132664662564865
>>> a=np.arange(12).reshape(3,4)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> a.sum(axis=0)
array([12, 15, 18, 21])
>>> a.sum(axis=1)
array([ 6, 22, 38])
>>> a.min(axis=0)
array([0, 1, 2, 3])
>>> a.min(axis=1)
array([0, 4, 8])
>>> b=np.arange(3)
>>> b
array([0, 1, 2])
>>> np.exp(b)
array([1. , 2.71828183, 7.3890561 ])
>>> np.sqrt(b)
array([0. , 1. , 1.41421356])
>>> np.add(b,np.array([2,4,6]))
array([2, 5, 8])
>>> a.mean()
5.0
>>> a.mean(1)
array([2., 5., 8.])
>>> a.mean(0)
array([4., 5., 6.])
>>> a=np.array(np.arange(1,9))
>>> a
array([1, 2, 3, 4, 5, 6, 7, 8])
>>> a=np.array(np.arange(1,10))
## 最值索引
>>> a.argmax()
8
>>> a.argmin()
0
## 限定并更改最大最小值
>>> a.clip(3,7)
array([3, 3, 3, 4, 5, 6, 7, 7, 7])
>>> a.conj()
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> b=np.array(np.linspace(1,5,4))
>>> b
array([1. , 2.33333333, 3.66666667, 5. ])
## 保留小数
>>> b.round(3)
array([1. , 2.333, 3.667, 5. ])
>>> a.reshape((3,3))
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> a=a.reshape((3,3))
## 求对角线的和
>>> a.trace()
15
## 按轴求和
>>> a.cumsum(0)
array([[ 1, 2, 3],
[ 5, 7, 9],
[12, 15, 18]], dtype=int32)
>>> a.cumsum(1)
array([[ 1, 3, 6],
[ 4, 9, 15],
[ 7, 15, 24]], dtype=int32)
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> a.any()
True
>>> a.all()
True
>>> a.std() # 标准差
2.581988897471611
>>> a=np.array((1,0,3))
>>> a.all()
False
## 加下划线的方法
- 比较:lt le gt ge eq ne bool
- 一元:neg pos abs invert
- 算术:add sub mul truediv floordiv mod divmod pow lshift rshift and or xor
- 就地运算:iadd isub itruediv ifloordiv imod ipow ilshift irshift ior ixor
- 特殊方法:copy deepcopy reduce setstate
>>> a=np.array((-1,0,3))
>>> a.__abs__()
array([1, 0, 3])
>>> a.__lt__(3)
array([ True, True, False])
>>> a.__ne__(0)
array([ True, False, True])
>>> a.__ge__(1)
array([False, False, True])
>>> b=np.array((1))
>>> b.__bool__()
True
>>> b=np.array((0))
>>> b.__bool__()
False
>>> a.__neg__()
array([ 1, 0, -3])
>>> a.__pos__()
array([-1, 0, 3])
>>> a.__invert__()
array([ 0, -1, -4], dtype=int32)
>>> a.__add__(4)
array([3, 4, 7])
>>> a.__sub__(3)
array([-4, -3, 0])
>>> a.__lshift__(2)
array([-4, 0, 12], dtype=int32)
>>> a.__rshift__(2)
array([-1, 0, 0], dtype=int32)
>>>
>>> a.__iadd__(3)
array([2, 3, 6])
>>> a.__isub__(1)
array([1, 2, 5])
>>> b=np.array((0,0,1,1))
>>> a=b=np.array((1,1,0,0))
>>> a.__ior__(b)
array([1, 1, 0, 0])
>>> a.__iand__(b)
array([1, 1, 0, 0])
>>> a.__ixor__(b)
array([0, 0, 0, 0])
>>> c=a.__copy__()
>>> c
array([0, 0, 0, 0])