python基础整理二(numpy)

通过指定url安装指定包

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy

numpy[数据处理]

数组可以存储一种类型的数据,列表可以存储任意类型的数据

import array
array.array('i',range(10)) #i代表整数类型
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

a =array.array('i',range(10))
a[0]
0
a[0]=10
a
array('i', [10, 1, 2, 3, 4, 5, 6, 7, 8, 9])
1.数组转换成列表
a = array.array('i',range(10))
list1=list(a)
2.列表转换成数组
list2=list(range(10))
b = np.array(list2)
3.生成数组
#一维数组
a = np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
type(a)
<class 'numpy.ndarray'>
a.dtype
dtype('float64')


np.zeros(10,int)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
b = np.zeros(10,int)
b.dtype
dtype('int32')
#多维数组,比如矩阵
c=np.zeros((4,4),int)
c
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

d=np.ones((4,4),int)
d
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])

e=np.full((4,4),3.14)
e
array([[3.14, 3.14, 3.14, 3.14],
       [3.14, 3.14, 3.14, 3.14],
       [3.14, 3.14, 3.14, 3.14],
       [3.14, 3.14, 3.14, 3.14]])


f=np.zeros_like(c)
h=np.ones_like(c)
f
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])
h
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])

i=np.full_like(c,3)
i
array([[3, 3, 3, 3],
       [3, 3, 3, 3],
       [3, 3, 3, 3],
       [3, 3, 3, 3]])

4.随机数random
#生成随机数
random.randint(1,10)
random.random()

#numpy.random
a= np.random.random((3,3))
a
array([[0.29769585, 0.87180424, 0.44915006],
       [0.65561664, 0.15132184, 0.61796125],
       [0.22334261, 0.5531065 , 0.26790481]])
b= np.random.randint(1,10,(3,3))
b
array([[6, 9, 5],
       [9, 8, 5],
       [9, 5, 3]])
5.范围取值range
range(1,10)
range(1, 10)
list(range(1,10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

#numpy.arange
a=np.arange(0,10,2)
a
array([0, 2, 4, 6, 8])
#numpy.linespace 步长取数
a=np.linspace(0,5,10)
a
array([0.        , 0.55555556, 1.11111111, 1.66666667, 2.22222222, 2.77777778, 3.33333333, 3.88888889, 4.44444444, 5.     ])
#numpy.eye 单位矩阵
a=np.eye(5)
a
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])

取值范围

Data typeDescription
boolBoolean(True or False)stored as a byte
int_Default integer type(same as C long, normally either int64 or int32
intcIdentical to C int(normally int64 or int32)
intpInteger used for indexing(same as C ssize_t, either int64 or int32)
int8Byte(-128 to 127)
int16Integer(-32758, 32767)
int32Integer(-2147483648, 2147483647)
int64Integer(-9223372036854775808, 9223372036854775807)
uint8Unsigned Integer (0, 255)
uint16Unsigned Integer (0, 65535)
uint32Unsigned Integer (0,4294967295)
uint64Unsigned Integer (0, 18446744073709551615)
float_shorthand for float64
float16半精度浮点数:符号位,5位整数位,10位小数位
float32单精度浮点数:符号位,8位整数位,23位小数位
float64双精度浮点数:符号位,11位整数位,32位小数位
complex_shorthand for complex64
complex64实部和虚部都是 float32
complex128实部和虚部都是 float64
6.访问数组中的元素
嵌套列表的访问
list1=[[1,2,3],[3,4,5],[5,6,7]]
list1[0][0]
1
list1[0,0]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: list indices must be integers or slices, not tuple
数组中元素的访问
a=np.full((3,3),3.14,dtype=float)
a[0]
array([3.14, 3.14, 3.14])
a[0][0]
3.14
a=np.array(list1)
a
array([[1, 2, 3],
       [3, 4, 5],
       [5, 6, 7]])
a[-1][0]
5
a[0,0]
1
a[-1,0]
5
等价访问a[0][0],a[0,0]
取前两列,用列表访问的方式
a[:3,:2]
array([[1, 2],
       [3, 4],
       [5, 6]])


a[:2][:2] 不等价与a[:2,:2]
array([[1, 2, 3],
       [3, 4, 5]])
7.数组属性
a
array([[1, 2, 3],
       [3, 4, 5],
       [5, 6, 7]])
#维度
a.ndim
2
#形状
a.shape
(3, 3)
#数量
a.size
9
#类型
a.dtype
dtype('int32')
#每个元素占用字节数
a.itemsize
4
#占用总字节数
a.nbytes
36

7.数组的运算
一维数组 (+-*/)
a=np.array(range(10)) 等价于
a=np.array(list(range(10)))
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a+10
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
二维数组(+-*/)
a=np.full((3,3),1.0)
a
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])
a+2
array([[3., 3., 3.],
       [3., 3., 3.],
       [3., 3., 3.]])
操作符numpy函数描述
+np.add加法(1+1=2)
-np.subtract减法(3-1=2)
*np.multiply乘法(2*3=6)
/np.divide除法(3/2=1.5)
//np.floor_divide取整除(3/2=1)
**np.power幂方(3**2=9)
%np.mod取余(5%3=2)
-np.negative负数(-2)
a=np.linspace(0,np.pi,5)
a
array([0.        , 0.78539816, 1.57079633, 2.35619449, 3.14159265])
b = np.sin(a)
b
array([0.00000000e+00, 7.07106781e-01, 1.00000000e+00, 7.07106781e-01, 1.22464680e-16])
8.统计运算
列表的运算
list1=[[1,2,3],[3,4,5],[5,6,7]]
sum(list1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'list'
list2=[1,2,3,4,5,6]
sum(list2)
21
数组运算
a=np.full((3,3),1.0)
sum(a) 每一列求和
array([3., 3., 3.])
sum(sum(a))
9.0
a=np.array([[1,2],[3,4],[5,6]])
a
array([[1, 2],
       [3, 4],
       [5, 6]])
sum(a)
array([ 9, 12])
np.sum(a, axis=0)
array([ 9, 12])
按照行求和
np.sum(a, axis=1)
array([ 3,  7, 11])
求整个数组的和
np.sum(a)
21
np.max,np.minsum类似可以求整个数组也可以求行或者列

np.sum效率高
在这里插入图片描述

9.比较
a=np.array(range(10))
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a>3
array([False, False, False, False,  True,  True,  True,  True,  True, True])

np.all是指数组中所有的值都是True的时候返回True
np.all(a>-1)
True
np.all(a>0)
False

np.any任何一个为True就是True
操作符函数
==np.equal
<np.less
>np.greater
!=np.not_equal
<=np.less_equal
>=np.greater_equal
9.变形
a=np.full((2,10),1,dtype=float)
a
a.reshape(10,2)
a.reshape(4,5)


a=np.random.randint(1,20,(3,4))
a
array([[ 8, 18,  2, 10],
       [19,  6, 15, 17],
       [17, 18, 16,  9]])
a.reshape(4,3)
array([[ 8, 18,  2],
       [10, 19,  6],
       [15, 17, 17],
       [18, 16,  9]])
a.reshape(2,6)
array([[ 8, 18,  2, 10, 19,  6],
       [15, 17, 17, 18, 16,  9]])
a.reshape(2,3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-76-d3d8e5b27339> in <module>
----> 1 a.reshape(2,3)

ValueError: cannot reshape array of size 12 into shape (2,3)
10.排序
list1=[[5,3,2],[1,4,8],[2,7,6]]
a=np.array(list1)
a
array([[5, 3, 2],
       [1, 4, 8],
       [2, 7, 6]])

np.sort(a)
array([[2, 3, 5],
       [1, 4, 8],
       [2, 6, 7]])

a.sort(axis=1)
11.拼接
a=np.array([1,2,3])
b=np.array([[1,2,3],[4,5,6]])
np.concatenate([a,a,a])
array([1, 2, 3, 1, 2, 3, 1, 2, 3])
np.concatenate([b,b])
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3],
       [4, 5, 6]])
按照列去链接
array([[1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6]])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值