展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

NumPy基本使用(一)

  • 安装库
# 打开cmd
pip install Numpy

# 使用
import numpy as np
  • 创建列表
lst = np.arange(1,11)
print(lst)
a = lst * 3
print(a)
a = lst + 3
print(a)

# 输出打印
[ 1  2  3  4  5  6  7  8  9 10]
[ 3  6  9 12 15 18 21 24 27 30]
[ 4  5  6  7  8  9 10 11 12 13]
  • 计算1
lst = np.arange(21,25)
lst1 = np.arange(11,15)
print(lst)
print(lst1)
lst2 = lst + lst1
print(lst2)
lst2 = lst - lst1  
print(lst2)
lst2 = lst > lst1
print(lst2)

# 输出打印
[21 22 23 24]
[11 12 13 14]
[32 34 36 38]
[10 10 10 10] 
[ True  True  True  True]
  • 计算2
a = np.arange(1,10) ** 2
print(a)
b = np.arange(1,10) ** 3
print(b)
c = a + b
print(c)

# 输出打印
[ 1  4  9 16 25 36 49 64 81]
[  1   8  27  64 125 216 343 512 729]
[  2  12  36  80 150 252 392 576 810]
  • 指定步长
lst = np.arange(1,10,2)
print(lst)

# 输出打印
[1 3 5 7 9]
  • 列表作为参数
plst = [1,10,20,5,7]
lst = np.array(plst)
print(lst)

# 输出打印
[ 1 10 20  5  7]
  • 维度
lst = np.arange(1,13)
print(lst)
# 查看数组的维度
print(lst.shape)  
# 改变数组的维度
lst.shape = (4,3) 
print(lst)
print(lst.shape)
# 查看长度
a = np.arange(1,10).reshape(3,3)
print(a.size)
print(len(a))

# 输出打印
[ 1  2  3  4  5  6  7  8  9 10 11 12]
(12,)

[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
(4, 3)

9
3
  • 复制维度
a = np.arange(1,10).reshape(3,3)
print(a)
# 创建默认值为0,维度和a一致的数组
b = np.zeros_like(a)  
print(b)
# 创建默认值为1,维度和a一致的数组
c = np.ones_like(a) 
print(c)

# 输出打印
[[1 2 3]
 [4 5 6]
 [7 8 9]]

[[0 0 0]
 [0 0 0]
 [0 0 0]]

[[1 1 1]
 [1 1 1]
 [1 1 1]]
  • 案例
# 传入列表
lst = np.array([[1,2,3],[4,5,6]])
print(lst)
print(lst.shape)

# 输出打印
[[1 2 3]
 [4 5 6]]
(2, 3)

# 直接创建
lst = np.arange(1,13).reshape(4,3)
print(lst)
lst.shape=(3,4)
print(lst)

# 输出
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
  • 指定类型
# 创建5个默认值为0的元素,默认是float类型
lst = np.zeros(5)  
print(lst)

# output
[0. 0. 0. 0. 0.]

# 查看lst对象的类型
print(type(lst))  

# output
<class 'numpy.ndarray'>

# 查看数组中的元素的数据类型
print(lst.dtype)  

# output
float64

# 创建默认值的时候,指定类型
lst = np.zeros(5,dtype='int')  
print(lst)
print(lst.dtype)

# output
[0 0 0 0 0]
int32

# 创建默认值的时候,指定类型
lst = np.zeros(5,dtype='bool')  
print(lst)
print(lst.dtype)

# output
[False False False False False]
bool

# 创建默认值为0的数组,指定维度为2,3
lst = np.zeros((2,3),dtype='int')
print(lst)

# output
[[0 0 0]
 [0 0 0]]

# 创建默认值为1的数组
lst = np.ones(5) 
print(lst)

# output
[1. 1. 1. 1. 1.]

# 创建2行3列的默认值为1的数组
lst = np.ones((2,3)) 
print(lst)

# output
[[1. 1. 1.]
 [1. 1. 1.]]

# 创建一个数组,默认值为0.2
lst = np.ones(5) / 5
print(lst)
# 创建3个值为520的数组
a = np.full(3,520)
print(a)
# 3行2列
a = np.full((3,2),520)
print(a)

# 输出打印
[0.2 0.2 0.2 0.2 0.2]

[520 520 520]

[[520 520]
 [520 520]
 [520 520]]
  • 类型转换
a = np.arange(1,10)
print(a)
# 数组元素的类型
print(a.dtype)
# 将int类型转换为float,生成新的数据组
b =  a.astype("float")
print(b)
print(b.dtype)

# 输出打印
[1 2 3 4 5 6 7 8 9]
int32
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
float64
  • 获取数据
a = np.arange(1,19)
print(a)
print(a[3]) # 索引为3的值
a.shape = (3,6)
print(a)
b = a[0][3]
print(b)    # 行索引0,列索引3
a.shape= (3,2,3)
print(a)
c = a[0][1][0]
print(c)    # 第0个列表,行索引1,列索引0

# 输出打印
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18]
4
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [13 14 15 16 17 18]]
4
[[[ 1  2  3]
  [ 4  5  6]]
 [[ 7  8  9]
  [10 11 12]]
 [[13 14 15]
  [16 17 18]]]
4
  • 遍历
a.shape = (18,)
# 通过遍历获取元素
for b in a:
    print(b)

# output
1-18

# 通过下标获取元素
for i in range(18):
    print(a[i])

# output
1-18

a.shape = (3,6)
# 使用遍历的方式
for b in a:
    for c in b:
        print(c)

# output
1-18

# 使用下标获取元素
for i in range(a.shape[0]):
    for j in range(a.shape[1]):
        print(a[i][j])

# output
1-18
posted @ 2023-11-10 15:13  DogLeftover  阅读(10)  评论(0)    收藏  举报