【漫漫转码路】Python Day 24

Numpy

NumPy(Numerical Python)是 Python 进行科学计算的一个扩展库,提供了大量的函数和操作,主要用于对多维数组执行计算,它比 Python 自身的嵌套列表结构要高效的多

1、创建一个数组

数组里面的元素都需要具备相同的数据类型
数组就是一个矩阵,因此必须是方的,不能说每一行的数据个数不一样
[[[[1, 2, 3], [4, 5]]]] # 会报错
数组不能进行伸缩, 没有列表的内存自动管理,他是一个固定的,

import numpy as np  # 导入模块

np.array(object, dtype = None):object传入一个类数组,根据类数组中的数据创建一个数组
dtype是数据类型,默认自动推断

# 例如
import numpy as np  # 导入numpy并另命名为np
from typing import Iterable  #导入Iterable

list1 = [1, 2, 3]
test1 = np.array(list1)
print(list1)
print(test1)  # 与列表形式不同
print(type(test1))  # 类型
print(isinstance(test1, Iterable))  # 可迭代对象
test2 = np.array(999)  # 一个数据,标量也可以
print(type(test2))  # 类型依然是numpy.ndarray
print(test2 == 999)  # 可以用来和数据比较
# 终端显示
[1, 2, 3]
[1 2 3]
<class 'numpy.ndarray'>  # 数组就是ndarray
True
<class 'numpy.ndarray'>
True

数组里面的元素都需要具备相同的数据类型

# 例如
import numpy as np
list1 = [1, 2., 3]  # 如果列表中有一个是浮点型
test1 = np.array(list1)
print(test1)
# 终端显示
[1. 2. 3.]  # 数组中会所有的都变成浮点型

2、dtype类型

numpy.dtype:判断数组的属性
dtype常用值描述

dtype常用值 描述
np.int8 字节(-128 to 127)
np.int16 整数(-32768 to 32767)
np.int32 整数(-2147483648 to 2147483647)
np.int64 整数(-9223372036854775808 to 9223372036854775807)
np.uint8 无符号整数(0 to 255)
np.uint16 无符号整数(0 to 65535)
np.uint32 无符号整数(0 to 4294967295)
np.uint64 无符号整数(0 to 18446744073709551615)
np.float16 半精度浮点数
np.float32 单精度浮点数
np.float64 双精度浮点数

一个字节是八个占位,可以表示256位数字

# 例如
import numpy as np
list1 = [1, 2, 3]
test1 = np.array(list1)
print(test1)
print(test1.dtype)

test2 = np.array(list1, dtype=np.int64)  # 手动设置int64
print(test2)
print(test2.dtype)

list2 = [1, 2, 2147483648]
test3 = np.array(list2)
print(test3)
print(test3.dtype)
# 终端显示
[1 2 3]
int32  # 自动推断,类型int32
[1 2 3]
int64  # 手动设置之后可以看到已经改变
[         1          2 2147483648]
int64  # 当数据改变之后,自动推断会根据数据做改变来满足要求

4、shape

numpy.shape:数组的形状,返回一个元组,里面是每个维度有多少个数据

# 例如
import numpy as np
list1 = [[[[1, 2, 3], [4, 5, 6]]]]
test1 = np.array(list1)
print(test1)
print(test1.shape)

test2 = np.array(999)
print(test2)
print(test2.shape)
# 终端显示
[[[[1 2 3]
   [4 5 6]]]]  # 这个数组
(1, 1, 2, 3)  # 有四个[],所以是四维,四维数组里面只有一个三维数组,三维数组里面只有一个二维数组,二维数组里面有2个一维数组,一维数组里面有三个标量,所以是(1, 1, 2, 3)
999  # 这是一个标量
()  # 标量是一个点,所以没有形状

3、ndim

numpy.ndim:返回数组的秩:轴的数量和维度的数量
其实也就是形状shape返回元组的长度

# 例如
import numpy as np
list1 = [[[[1, 2, 3], [4, 5, 6]]]]
test1 = np.array(list1)
print(test1)
print(test1.shape)
print(test1.ndim)

test2 = np.array(999)
print(test2)
print(test2.shape)
print(test2.ndim)
# 终端显示
[[[[1 2 3]
   [4 5 6]]]]
(1, 1, 2, 3)
4  # 四维
999
()
0  # 0维

4、size

numpy.size:数组中元素的个数,就是标量的个数

# 例如
list1 = [[[[1, 2, 3], [4, 5, 6]]]]
test1 = np.array(list1)
print(test1)
print(test1.size)

test2 = np.array(999)
print(test2)
print(test2.size)
#终端显示
[[[[1 2 3]
   [4 5 6]]]]
6  # 6个标量
999
1  # 1个标量

可以发现,size等于shape的乘积

5、itemsize

numpy.itemsize:数组中每个元素的大小,以字节为单位,int32就是4个字节

# 例如
import numpy as np
list1 = [[[[1, 2, 3], [4, 5, 6]]]]
test1 = np.array(list1)
print(test1)
print(test1.dtype)
print(test1.itemsize)

test2 = np.array(999)
print(test2)
print(test2.dtype)
print(test2.itemsize)
# 终端显示
[[[[1 2 3]
   [4 5 6]]]]
int32  # 类型是int32
4  # 四个字节
999
int32
4

6、numpy.asarray():

numpy.asarray(a, dtype=None):当a是ndarray(数组)且dtype匹配时,np.asarray不执行复制操作,而np.arraya仍然会复制出一个副本,占用新的内存
意思是说:当a是ndarray且dtype匹配时,类似于视图,可以直接反映原数据的情况

当a传类数组的时候(列表为例)

# 例如
import numpy as np
list1 = [[1, 2, 3], [1, 2, 3]]
np1 = np.array(list1)
print(np1)
np2 = np.asarray(list1)
print(np2)
list1.append([7, 8, 9])
print(np1)
print(np2)
print(list1)
# 终端显示
[[1 2 3]
 [1 2 3]]
[[1 2 3]
 [1 2 3]]
[[1 2 3]
 [1 2 3]]
[[1 2 3]  # 两者一样,没有变化
 [1 2 3]]
[[1, 2, 3], [1, 2, 3], [7, 8, 9]]

当a传数组的时候

# 例如
import numpy as np
test1 = np.array([[1, 2, 3], [1, 2, 3]])
np1 = np.array(test1)
print(np1)
np2 = np.asarray(test1)
print(np2)
test1[0][1] = 7
print(np1)
print(np2)
print(test1)
# 终端显示
[[1 2 3]
 [1 2 3]]
[[1 2 3]
 [1 2 3]]
[[1 2 3]
 [1 2 3]]
[[1 7 3]  # asarray的结果和视图一样发生了变化,实时反馈
 [1 2 3]]
[[1 7 3]
 [1 2 3]]

但如果改变了dtype的类型

# 例如
import numpy as np
test1 = np.array([[1, 2, 3], [1, 2, 3]])
np1 = np.array(test1)
print(np1)
np2 = np.asarray(test1, dtype=np.int64)
print(np2)
test1[0][1] = 7
print(np1)
print(np2)
print(test1)
# 终端显示
[[1 2 3]
 [1 2 3]]
[[1 2 3]
 [1 2 3]]
[[1 2 3]
 [1 2 3]]
[[1 2 3]  # 你会发现没有像视图一样发生变化,因为是新建了一个,跟原数据没有关系了
 [1 2 3]]
[[1 7 3]
 [1 2 3]]

总结:
像视图一样直接改变的两个因素,一个是传入数组,第二个是dtype没有改变

7、numpy.copy()

numpy.copy(array like):传一个类数组,然后会新建一个数组

# 例如
import numpy as np
list1 = [[1, 2, 3], [1, 2, 3]]
np1 = np.copy(list1)
print(np1)
# 终端显示
[[1 2 3]
 [1 2 3]]  # 变成了一个数组
# 例如
import numpy as np
list1 = [[1, 2, 3], [1, 2, 3]]
np1 = np.copy(list1)  # 用numpy调用
print(np1)

np2 = np1.copy()  # 用np1调用对象方法
print(np2)
# 终端显示
[[1 2 3]
 [1 2 3]]
[[1 2 3]
 [1 2 3]]

8、numpy.fromiter()

numpy.fromiter(iterable, dtype, count=-1):将可迭代对象iterable转成一维数组,dtype为类型,count是选取其中元素个数,默认-1是全部选取
只能形成一维数组

# 例如
import numpy as np
list1 = [1, 2, 3, 4]
np1 = np.fromiter(list1, np.int64)
print(np1)

np2 = np.fromiter(list1, np.int64, count=3)
print(np2)
# 终端显示
[1 2 3 4]
[1 2 3]  # 选取三个元素形成数组

你可能感兴趣的:(转码,python,numpy,开发语言,人工智能,改行学it)