纪录27个NumPy操作

介绍

在上一篇有关21 Pandas操作的文章中,我讨论了一些重要的操作,可以帮助新手开始进行数据分析。本文应该为NumPy提供类似的目的。简要介绍一下,NumPy是一个非常强大的库,可用于执行各种操作,从查找数组的均值到快速傅立叶变换和信号分析。从这个意义上讲,它与MATLAB非常相似。

您将需要将NumPy导入为'np',然后使用它来执行操作。

操作:

  1. 将列表转换为n维NumPy数组
numpy_array = np.array(list_to_convert)

2.使用np.newaxis和np.reshape

np.newaxis用于创建大小为1的新尺寸。

a = [1,2,3,4,5] is a list
a_numpy = np.array(a)

如果打印a_numpy.shape,您会得到(5,)。为了使它成为行向量或列向量,可以

row_vector = a_numpy[:,np.newaxis] ####shape is (5,1) now
col_vector = a_numpy[np.newaxis,:] ####shape is (1,5) now

类似地,np.reshape可以用来对任何数组进行整形。例如:

a = range(0,15) ####list of numbers from 0 to 14
b = a.reshape(3,5) 
b would become:
[[0,1,2,3,4],
 [5,6,7,8,9],
 [10,11,12,13,14],
 [15,16,17,18,19]]

3.将任何数据类型转换为NumPy数组

使用np.asarray。例如

a = [(1,2), [3,4,(5)], (6,7,8)]
b = np.asarray(a)
b::
array([(1, 2), list([3, 4, (5, 6)]), (6, 7, 8)], dtype=object)

4.获得零的n维数组。

a = np.zeros(shape,dtype=type_of_zeros)
type of zeros can be int or float as it is required
eg.
a = np.zeros((3,4), dtype = np.float16)

5.获得一个n维数组。

类似于np.zeros:

a = np.ones((3,4), dtype=np.int32)

6. np.full和np.empty

np.full用于获取由一个特定值填充的数组,而np.empty通过使用随机值初始化数组来帮助创建数组。例如。

1. np.full(shape_as_tuple,value_to_fill,dtype=type_you_want)
a = np.full((2,3),1,dtype=np.float16)
a would be:
array([[1., 1., 1.],
       [1., 1., 1.]], dtype=float16)
2. np.empty(shape_as_tuple,dtype=int)
a = np.empty((2,2),dtype=np.int16)
a would be:
array([[25824, 25701],
       [ 2606,  8224]], dtype=int16) 
The integers here are random.

7.使用np.arrange和np.linspace获取均匀间隔的值的数组

两者都可以用来创建具有均匀间隔的元素的数组。

linspace:

np.linspace(start,stop,num=50,endpoint=bool_value,retstep=bool_value)
endpoint specifies if you want the stop value to be included and retstep tells if you would like to know the step-value.'num' is the number of integer to be returned where 50 is default Eg,
np.linspace(1,2,num=5,endpoint=False,retstep=True)
This means return 5 values starting at 1 and ending befor 2 and returning the step-size.
output would be:
(array([1. , 1.2, 1.4, 1.6, 1.8]), 0.2) 
##### Tuple of numpy array and step-size

人气指数:

np.arange(start=where_to_start,stop=where_to_stop,step=step_size)

如果仅提供一个数字作为参数,则将其视为停止,如果提供2,则将其视为开始和停止。注意这里的拼写。

8.找到NumPy数组的形状

array.shape

9.了解NumPy数组的尺寸

x = np.array([1,2,3])
x.ndim will produce 1

10.查找NumPy数组中的元素数

x = np.ones((3,2,4),dtype=np.int16)
x.size will produce 24

11.获取n维数组占用的内存空间

x.nbytes
output will be 24*memory occupied by 16 bit integer = 24*2 = 48

12.在NumPy数组中查找元素的数据类型

x = np.ones((2,3), dtype=np.int16)
x.dtype will produce
dtype('int16')
It works better when elements in the array are of one type otherwise typecasting happens and result may be difficult to interpret.

13.如何创建NumPy数组的副本

使用np.copy

y = np.array([[1,3],[5,6]])
x = np.copy(y)
If,
x[0][0] = 1000
Then,
x is
100 3
5 6
y is
1 3
5 6

14.获取n-d数组的转置

使用array_name.T

x = [[1,2],[3,4]]
x
1 2
3 4
x.T is
1 3
2 4

15.展平n-d数组以获得一维数组

使用np.reshape和np.ravel:

np.reshape:这确实是一个不错的选择。在重塑时,如果您提供-1作为尺寸之一,则从no推断出来。的元素。例如。对于尺寸数组,(1,3,4)如果将其调整为,(-1,2,2),则第一维的长度计算为3。所以,

If x is:
1 2 3
4 5 9
Then x.reshape(-1) produces:
array([1, 2, 3, 4, 5, 9])

np.ravel

x = np.array([[1, 2, 3], [4, 5, 6]])
x.ravel() produces
array([1, 2, 3, 4, 5, 6])

16.更改n-d数组的轴或交换尺寸

使用np.moveaxis和np.swapaxes。

x = np.ones((3,4,5))
np.moveaxis(x,axes_to_move_as_list, destination_axes_as_list)
For eg.
x.moveaxis([1,2],[0,-2])
This means you want to move 1st axis to 0th axes and 2nd axes to 2nd last axis. So,the new shape would be.
(4,5,3)

转换没有到位,所以不要忘记将其存储在另一个变量中。

np.swapaxes。

x = np.array([[1,2],[3,4]])
x.shape is (2,2) and x is
1 2
3 4
np.swapaxes(x,0,1) will produce
1 3
2 4
If x = np.ones((3,4,5)), and
y = np.swapaxes(0,2)
y.shape will be
(5,4,3)

17.将NumPy数组转换为列表

x = np.array([[3,4,5,9],[2,6,8,0]])
y = x.tolist()
y will be
[[3, 4, 5, 9], [2, 6, 8, 0]]

NumPy文档提到,list(x)如果x是一维数组,使用也将起作用。

18.更改NumPy数组中元素的数据类型。

使用ndarray.astype

x = np.array([0,1,2.0,3.0,4.2],dtype=np.float32)
x.astype(np.int16) will produce
array([0, 1, 2, 3, 4], dtype=int16)
x.astype(np.bool) will produce 
array([False,  True,  True,  True,  True])

19.获取非零元素的索引

使用n-dim_array.nonzero()

x = np.array([0,1,2.0,3.0,4.2],dtype=np.float32)
x.nonzero() will produce
(array([1, 2, 3, 4]),) 
It's important to note that x has shape (5,) so only 1st indices are returned. If x were say,
x = np.array([[0,1],[3,5]])
x.nonzero() would produce 
(array([0, 1, 1]), array([1, 0, 1]))
So, the indices are actually (0,1), (1,0), (1,1). 

20.对NumPy数组进行排序

使用np.ndarray.sort(axis = axis_you_want_to_sort_by)

x = np.array([[4,3],[3,2])
x is
4 3
3 2
x.sort(axis=1) #sort each row
3 4
2 3
x.sort(axis=0) #sort each col
3 2
4 3

21.比较NumPy数组和值

比较将产生布尔类型的NumPy n维数组。例如

x = np.array([[0,1],[2,3]])
x==1 will produce
array([[False,  True],
       [False, False]])

如果您想计算x中的数字,您可以这样做

(x==1).astype(np.int16).sum()

它应该输出 1

22.乘以两个NumPy矩阵

使用numpy.matmul来获取二维矩阵的矩阵乘积:

a = np.eye(2) #identity matrix of size 2
a
1 0
0 1
b = np.array([[1,2],[3,4]])
b
1 2
3 4
np.matmul(a,b) will give
1 2
3 4

如果我们提供一维阵列,则输出将非常不同,因为将使用广播。我们在下面讨论。此外,还有另一个函数称为np.multiply执行元素到元素的乘法。对于前两个矩阵,输出为np.multiply(a,b)

1 0
0 4

23.两个阵列的点积

np.dot(矩阵1,矩阵2)

a = np.array([[1,2,3],[4,8,16]])
a:
1 2 3
4 8 16
b = np.array([5,6,11]).reshape(-1,1)
b:
5
6
11
np.dot(a,b) produces
38
160
Just like any dot product of a matrix with a column vector would produce.

行向量与列向量的点积将产生:

if a is array([[1, 2, 3, 4]])
and b is:
 
array([[4],
       [5],
       [6],
       [7]])
np.dot(a,b) gives:
array([[60]])
a's shape was (1,4) and b's shape was (4,1) so the result will have shape (1,1)

24.获得两个numpy向量的叉积

回想一下物理学中的矢量叉积。这是大约一个点的扭矩方向。

x = [1,2,3]
y = [4,5,6]
z = np.cross(x, y)
z is:
array([-3,  6, -3]) 

25.获取数组的梯度

使用np.gradient。NumPy使用泰勒级数和中心差法计算梯度。您可以在这篇文章中阅读有关它的更多信息

x = np.array([5, 10, 14, 17, 19, 26], dtype=np.float16)
np.gradient(x) will be:
array([5. , 4.5, 3.5, 2.5, 4.5, 7. ], dtype=float16)

26.如何切片NumPy数组?

For single element:
x[r][c] where r,c are row and col number of the element.
For slicing more than one element.
x:
2 4 9
3 1 5
7 8 0
and you want 2,4 and 7,8 then do
x[list_of_rows,list_of_cols] which would be
x[[0,0,2,2],[0,1,0,1]] produces
array([2, 4, 7, 8])
If one of the rows or cols are continuous, it's easier to do it:
x[[0,2],0:2] produces
array([[2, 4],
       [7, 8]])

27. broadcasting

如果不包括broadcasting,则有关NumPy的任何文章都将是不完整的。这是一个重要的概念,可帮助NumPy对操作进行向量化,从而使计算速度更快。理解一些规则将有助于更好地剖析广播。

来自NumPy文档:

在两个数组上进行操作时,NumPy逐元素比较其形状。它从尾随尺寸开始,一直向前发展。两种尺寸兼容

1.他们是平等的,或

2.其中之一是1

要记住的另一件事是,

如果尺寸匹配,则输出将在每个尺寸中具有最大长度。如果其中一个维度的长度为1,则将重复该维度中的值

假设有两个数组A和维度说明B **(3,4,5)****(4,1) **分别,你想添加的两个数组。由于它们的形状不同,因此NumPy将尝试广播这些值。它开始比较两个尺寸的最后一个维度的长度:5和1,这些值不相等,但是由于其中一个值为1,因此将重复该值,最终输出在最后一个维度中的长度为5 。

两者的倒数第二个长度相同4

A中的最后3维或1维具有长度,3而B没有任何长度。当其中一个向量缺少维度时,NumPy在向量前加1。因此,B变为**(1,4,1)**现在,长度匹配为3和1,并且在B中将值重复3次。最终输出将具有shape **(3,4,5)**

a :
3 5 8
4 5 6
9 7 2
b :
b = [2,3,4]
a's shape: (3,3)
b's shape: (3,)
Last dimension has same length 3 for the two and then 1 is prepended to the the dimension of b as it doesn't have anything in that dimension. So, b becomes [[2,3,4]] having shape (1,3). Now, 1st dimension match and values are repeated for b. 
Finally, b can be seen as
2 3 4
2 3 4
2 3 4
So, a+b produces
5 8 12
6 8 10
11 10 6

查看broadcasting中的这些帖子以了解更多信息:第一第二

总结

感谢您的阅读。我希望本文能为需要NumPy入门的任何人提供帮助。我发现这些操作非常有帮助,将它们放在我们的提示上总是一件好事。这些操作非常基础,NumPy中可能有不同的方法来实现相同的目标。除了提供了链接的其他几篇文章外,我主要使用NumPy文档作为参考。

翻译自:https://towardsdatascience.com/27-things-that-a-beginner-needs-to-know-about-numpy-edda217fb662

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容

  • 基础篇NumPy的主要对象是同种元素的多维数组。这是一个所有的元素都是一种类型、通过一个正整数元组索引的元素表格(...
    oyan99阅读 5,124评论 0 18
  • 前言 numpy是支持 Python语言的数值计算扩充库,其拥有强大的高维度数组处理与矩阵运算能力。除此之外,nu...
    TensorFlow开发者阅读 3,212评论 0 35
  • Numpy的组成与功能 Numpy(Numeric Python)可以被理解为一个用python实现的科学计算包,...
    不做大哥好多年阅读 4,285评论 0 10
  • 先决条件 在阅读这个教程之前,你多少需要知道点python。如果你想从新回忆下,请看看Python Tutoria...
    舒map阅读 2,577评论 1 13
  • NumPy是Python中关于科学计算的一个类库,在这里简单介绍一下。 来源:https://docs.scipy...
    灰太狼_black阅读 1,228评论 0 5