Numpy学习——创建数组及常规操作(数组创建、切片、维度变换、索引、筛选、判断、广播)

系列文章目录

Numpy学习——创建数组及常规操作(数组创建、切片、维度变换、索引、筛选、判断、广播)
Tensor学习——创建张量及常规操作(创建、切片、索引、转换、维度变换、拼接)
基础学习——numpy与tensor张量的转换
基础学习——关于list、numpy、torch在float和int等数据类型转换方面的总结



学习前言

近期学习了很多numpy的基础知识,在这里总结了一下,方便以后查看。


一、NumPy简介

我们都知道,Python本身是一门通用的编程语言,可用于诸多不同的场景,如Web开发、操作系统开发、数据分析、算法工程等等,而不同领域的应用,就要求额外定义一些更专业、同时也更高效的数据类型与函数。在上一节中,我们简单尝试了自己定义一类表示二维或多维的数组数据类型,并定义的数组之间的距离计算方法。N维数组是数据科学领域最基本的数据结构了,无论你是要进行数学计算,还是进行实际的表格数据处理,N维数组都是非常基础的一类对象。而在Python原生数据类型中,列表是最接近数组功能要求的对象,但列表受限于其数据结构,计算并不高效,且没有相关科学计算函数支持,因此在进行数据科学计算过程中,我们往往会适用第三方库NumPy,调用其中数组(Array)对象,来满足数据科学运算需求。也正因如此,NumPy可以说是Python数据技术中最通用的第三方库,其核心优势在它提供了可供各类复杂数据运算的N维数组数据对象,以及各类相关的函数方法,在上一节中我们也初步看到了NumPy的强大之处,而本节开始,我们将详细介绍NumPy库中常用的对象和基本方法。

二、使用array创建数组

#导入numpy包,以下操作都基于这个包
import numpy as np 

1、普通数组

#直接对数组里赋值。
a= np.array(
[[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[86, 85, 83, 67, 80]])

array([[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[86, 85, 83, 67, 80]])

a = np.array((1.1, 3.2, 2))  # 将tuple转为array
b = np.array([1, 1.1], dtype = 'int')        # dtype参数
a,b

array([1.1, 3.2, 2. ])
array([1, 1])

2、 序列数组

# 利用range生成array
a = np.array(range(5))  
b = np.arange(5)
a,b

array([0, 1, 2, 3, 4])
array([0, 1, 2, 3, 4])

#生成序列数组
a = np.arange(5)  
b = np.arange(10,100,2)
a,b

array([0, 1, 2, 3, 4])
array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28])

3、 0与1数组

a = np.ones([4,8])   #生成数值为1的矩阵
a

array([[1., 1., 1.],
[1., 1., 1.]])

a = np.zeros([2,3])   #生成数值为0的矩阵
a
b= np.zeros_like(a)

array([[0., 0., 0.],
[0., 0., 0.]])

4、 正态分布数组

# 创建符合正态分布的4行5列数据
a = np.random.normal(0, 1, (4, 5))

array([[-0.38083747, -0.36443134, -0.35581484, -1.28505148, 0.42154171],
[-0.85404262, 0.24371723, 0.15531367, 0.00353427, -0.58411297],
[ 0.90002949, -0.1033983 , 0.03296337, 2.80171916, -1.14690091],
[ 0.21078579, 0.40770893, 1.13163909, -0.04951451, 0.17209715]])

5、随机数数组

b= np.random.randint(40, 100, (3, 4))

array([[69, 64, 74, 41],
[63, 61, 46, 48],
[81, 90, 99, 79]])

三.数组操作

1、reshape操作

b.reshape([2, 6]) #改变形状

array([[65, 75, 74, 51, 81, 92],
[77, 52, 77, 60, 44, 45]])

2、深浅拷贝

a1 = np.array(a) #深拷贝
a2 = np.asarray(a)#浅拷贝
a[0,0]=1000
a,a1,a2

3、 max、min、mean

a = np.random.normal(0, 1, (4, 5))
np.max(temp,axis=0) #按列的最大值 
np.max(temp) #最大值
np.mean(temp) #平均值
np.min(temp)  # 最小值
np.argmax(temp)  #最大值下标
np.argmax(temp,axis=0)  #按列最大值

4、数组相乘、点乘、广播

a=[1,2,3]  #列表
a*3

[1, 2, 3, 1, 2, 3, 1, 2, 3]

arr1 = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])
arr2 = np.array([[1], [3]])
arr1+arr2

array([[2, 3, 4, 3, 2, 5],
[8, 9, 4, 5, 6, 4]])

a = np.array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
b = np.array([[0.7], [0.3]])
np.matmul(a, b)  #矩阵相乘
np.dot(a,b)  #点乘

array([[81.8],
[81.4],
[82.9],
[90. ],
[84.8],
[84.4],
[78.6],
[92.6]])

5、扩展、收缩维度、填充边界

x = np.arange(8).reshape(2, 4)
print(x2.shape)
x2 = np.expand_dims(x, axis=1)  

(2, 1, 4)
array([[[0, 1, 2, 3]],

   [[4, 5, 6, 7]]])
n = np.zeros((1,2,1,3))
print(n.shape,'\n')
 
n1 = np.squeeze(n)
print(n1.shape,'\n')
 
n2 = np.squeeze(n,axis = 0)
print(n2.shape,'\n')

(1, 2, 1, 3)

(2, 3)

(2, 1, 3)

a = np.ones((2,2))
b = np.pad(a,3,'constant')    #对a,上下左右各扩充3行,constant缺省,默认为0
print(a.shape)
print(b.shape)
print(b)

(2, 2)
(8, 8)
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]

另一种填充方法

a = np.array([[[1,2],[4,3]],[[5,6],[8,7]]])
print("a.shape:",a.shape) # [2,2,2] 
print(a)
# 在做padding时,优先从最里面的维度开始扩展,对于本样例的三维数据a,也就是先padding dim=2,之后dim=1, 最后是 dim=0
print("padding dim=2:"),
a1 = np.pad(a,((0,0),(0,0),(1,1)),'constant')
print("a1.shape:",a1.shape,"\na1:\n",a1)

a.shape: (2, 2, 2)
[[[1 2]
[4 3]]
[[5 6]
[8 7]]]
padding dim=2:
a1.shape: (2, 2, 4)
a1:
[[[0 1 2 0]
[0 4 3 0]]

[[0 5 6 0]
[0 8 7 0]]]

6、数组切片、转置、去重

a= np.random.normal(0, 1, (4, 5))
#数组的索引和切片
b = a[0, 0:3]
c = a.T
print(a.shape,b,c.shape)
#数组去重
temp = np.array([[1, 2, 3, 4],[3, 4, 5, 6]])
np.unique(temp)

7、数组筛选、判断

a= np.random.randint(40, 100, (5, 5))
a=a[2:,0:4]
a

array([[70, 72, 46, 42],
[93, 81, 42, 73],
[51, 72, 48, 91]])

b>60

array([[ True, True, False, False],
[ True, True, False, True],
[False, True, False, True]])

#通用判断函数
np.all(a[0:2,:]>60)
np.any(score[0:2,:]>90)
np.where(a>60,1,0)  #判断
np.where(np.logical_and(a>60,a<90),1,0)   #判断 and

array([[0, 0, 0, 1, 0],
[1, 0, 1, 1, 1],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 1],
[1, 0, 1, 1, 0]])
array([[0, 0, 0, 1, 0],
[1, 0, 1, 1, 1],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0]])

8、 数组去重

numpy.unique(arr, return_index, return_inverse, return_counts)

arr:输入数组,如果不是一维数组则会展开
return_index:如果为 true,返回新列表元素在旧列表中的位置(下标),并以列表形式存储。
return_inverse:如果为true,返回旧列表元素在新列表中的位置(下标),并以列表形式存储。
return_counts:如果为 true,返回去重数组中的元素在原数组中的出现次数。

四.查看数组属性及扩展

查看数组形状

a.shape 

查看数组维度

a.ndim

查看数组大小

a.size

查看数组数据类型

a.dtype

np数组属性操作
数据类型
数据类型

总结

以上内容并不全面,后期还会更新,有错误的地方还请大家多多指教。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chaoy6565

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值