numpy数组操作_numpy数组

numpy数组操作

什么是数组对象? (What is an array object?)

Simply put, an array is a data structure meant to hold data (generally but not necessarily of similar datatypes) in a particular scheme/format. Formal definition of an array object as per the numpy docs is as follows:

简而言之,数组是一种数据结构,用于以特定方案/格式保存数据(通常但不一定是相似的数据类型)。 根据numpy docs对数组对象的形式定义如下:

An array object represents a multidimensional, homogeneous array of fixed-size items.

数组对象表示固定大小项的多维同构数组。

What a recursive definition, or is it? Note that we’re trying to define an array object as defined in numpy and not an array (the definition of an array is already mentioned in the first line of this para: a data-structure holding data in a fixed format). For understanding the definition, we have to first understand all the terms mentioned here. So, let’s start with some intuition from geometry to understand the concept of what dimensions are.

什么是递归定义? 请注意,我们正在尝试定义一个以numpy而不是数组定义的数组对象(在本段的第一行中已经提到了数组的定义:以固定格式保存数据的数据结构)。 为了理解该定义,我们必须首先理解这里提到的所有术语。 因此,让我们从几何的一些直觉开始,了解什么是尺寸的概念。

In geometry, fundamental unit/building block is a point which is a zero-dimensional entity. When they’re arranged next to each other, we get a contiguous set of points making a line; this is a one-dimensional entity.

在几何中,基本单位/基本单元是一个零维实体。 当它们彼此并排布置时,我们得到了一组连续的点,形成一条直线。 这是一维实体。

Next, when we arrange a set of lines which are at some angle (other than 0 degrees) to one another, we get a plane which is a two dimensional entity and then sets of planes which define a parallelopiped become a three dimensional entity and so on. Since we’re three-dimensional beings, we can visualize 0, 1, 2 and 3-Dimensional structures but this logic of stacking dimensions can continue perpetually and we call the resulting space n-dimensional space.

接下来,当我们布置一组彼此成一定角度(不是0度)的线时,我们得到一个平面,它是一个二维实体,然后定义了平行视点的平面集变成了一个三维实体,因此上。 由于我们是三维生物,因此我们可以可视化0、1、2和3维结构,但是这种堆叠维的逻辑可以永久地继续,我们将结果空间称为n维空间。

Image for post
Photo by Dhruv on Unsplash
DhruvUnsplash上的 照片

The nd-array object in numpy can be thought of a generic object capable of representing data in n-dimensions. You can think of a python list as one dimension. When you nest multiple lists within one another, you create what is an n-dimensional array. (n-dimensional is just another way of saying multi-dimensional).

可以将numpy中的nd-array对象视为能够以n维表示数据的通用对象。 您可以将python列表视为一维。 当您将多个列表相互嵌套时,您将创建一个n维数组。 (n维只是多维的另一种说法)。

Now that we have some idea of what a dimension is, let’s tackle the second part. Homogeneous simply means that the lists which we nest to create this structure must all contain same kind of items.

现在我们对尺寸是什么有了一些了解,让我们解决第二部分。 同质只是意味着我们嵌套以创建此结构的列表必须都包含相同种类的项目。

Image for post
Photo by Denise Johnson on Unsplash
丹妮丝·约翰逊 ( Denise Johnson)Unsplash

Let’s say you have a box of oranges with oranges aligned neatly with their axes perpendicular to each other. This arrangement is a 3-Dimensional one. However if you got a capsicum sitting in one or more spots in this box, you can’t call this arrangement a 3-Dimensional array. Note that it cannot be called an array because you have dissimilar items in the arrangement. That’s what homogeneous means.

假设您有一盒橘子,橘子的轴线彼此垂直,整齐地对齐。 这种布置是3维的。 但是,如果您在此框中的一个或多个位置上放置了一个辣椒,则不能将此布置称为3维数组。 请注意,它不能称为数组,因为您的排列中有不同的项目。 这就是齐心的意思。

In terms of program, a nested list could be called an array if all elements of all lists are of the same datatype i.e. either int, float, bools, strings or anything. But since we’re doing numerical computation, these types are limited to ints and floats for numpy. And remember that we have to compulsorily wrap it in the np.array function to call it a numpy nd-array object.

就程序而言,如果所有列表的所有元素都具有相同的数据类型,即int,float,bool,字符串或任何其他内容,则嵌套列表可以称为数组。 但是由于我们正在进行数值计算,因此这些类型仅限于numpy的整数和浮点型。 请记住,我们必须将其强制包装在np.array函数中,以将其称为numpy nd-array对象。

阵列术语 (Array Terminologies)

数组对象的等级 (Rank of an array object)

The rank of an array is the number of dimensions present within the array. It’s interesting to note that some arrays have also got special names.

数组的等级是数组中存在的维数。 有趣的是,有些数组也有特殊名称。

A rank one array is also called a vector, just an array. (Colloquial understanding is that array = 1 dimension).

排名第一的数组也称为向量,只是一个数组。 (口语理解是数组= 1维)。

A rank two array is also called a matrix.

秩为2的数组也称为矩阵。

All other arrays are referred to by prepending the rank followed by a D. eg. 3D-array, 4D-array, nD-array etc.

所有其他数组都通过在排名之前加上D来引用。 3D阵列,4D阵列,nD阵列等

数组对象的轴 (Axis of an array object)

The axis of an array is a specific dimension along that array. The elements of an array are said to run along/across axis of an array. The length of an axis is the number of elements running along that axis.

数组的轴是沿着该数组的特定尺寸。 数组的元素被称为沿数组的轴/交叉轴延伸。 轴的长度是沿该轴延伸的元素数。

Image for post
Basic array terminologies defined in a diagram
图中定义的基本阵列术语

数组对象的形状 (Shape of an array object)

The lengths of all the axis together correspond to the shape of an array. If we know the shape of an array, we automatically know the rank and axes as well.

所有轴的长度一起对应于阵列的形状。 如果我们知道数组的形状,那么我们也会自动知道等级和轴。

For eg. a 3 X 4 array has rank 2 and the there are two axes which run for a length of 3 and 4 elements respectively.

例如。 一个3 X 4数组的等级为2,并且有两个轴分别延伸3个元素和4个元素。

数组对象的使用 (Uses of Array Objects)

Now that we know about what arrays are, let’s first understand how & where they’re useful and then go on to study them in more depth.

现在我们了解了什么是数组,让我们首先了解它们的用途和用途,然后继续更深入地研究它们。

To name a few uses of arrays, they’re as follows:

列举数组的几种用法,它们如下:

  • Any Machine Learning problem involves expressing the output Y as a function of the input X. These inputs and outputs are expressed typically as a 1-Dimensional array or more colloquially referred to as vectors.

    任何机器学习问题都涉及将输出Y表示为输入X的函数。这些输入和输出通常表示为一维数组,或更通俗地称为矢量。
  • Applications involving checking the states of machines/elements/programs use flags or flag variables which are convenient to be represented as vectors.

    涉及检查机器/元件/程序状态的应用程序使用标志或标志变量,这些标志或标志变量可以方便地表示为矢量。
  • In any natural language processing problem, sentences are represented as sequence of words. A sequence of words is nothing but a 1-D array/vector. Also in time-series analysis, the dependent variable is studied with respect to it’s past values which are also represented as a 1-Dimensional array/vector.

    在任何自然语言处理问题中,句子都表示为单词序列。 单词序列不过是一维数组/向量。 同样在时间序列分析中,将针对因变量的过去值进行研究,这些过去值也表示为一维数组/向量。
  • Images are represented as either 2-Dimensional (monochromatic) or 3-Dimensional (colored) or 4-Dimensional (colored with transparency) arrays of numbers. Any Deep Learning application needs you to specify the image in one of these formats. What you’re viewing on your screen right now as you read this is a 3-Dimensional/4-Dimensional array being rendered as an image in real-time; that’s how important and underappreciated this data-structure called as an array is!

    图像表示为2维(单色)或3维(彩色)或4维(彩色透明)的数字数组。 任何深度学习应用程序都需要您以以下一种格式指定图像。 当您阅读此书时,您正在屏幕上查看的是一个3维/ 4维数组,实时将其渲染为图像。 这就是所谓的数组数据结构的重要性和欠缺之处!

Now that we know why we need arrays, let’s get our hands dirty and understand how we can use them along with numpy.

现在我们知道了为什么需要数组,让我们开始动手,了解如何将它们与numpy一起使用。

使用numpy创建数组 (Creating arrays using numpy)

It’s pretty simple to create an array in numpy. You create a list and wrap numpy’s array function around it as follows

在numpy中创建数组非常简单。 您创建一个列表并将numpy的数组函数包装在其周围,如下所示

import numpy as np
x = [1, 2, 3, 10, 11]
y = np.array(x)

Optionally you can specify the type of data you want your numpy array to be. It could be one of np.int8, np.int16, np.int32, np.int64, np.float16, np.float32, np.float64. The numbers specify the number of bits used to store every element in that array. If you don’t specify, numpy will automatically assign one of np.int64 or np.float64 based on the data you have in the array.

(可选)您可以指定希望numpy数组成为的数据类型。 它可以是np.int8,np.int16,np.int32,np.int64,np.float16,np.float32,np.float64之一。 数字指定用于存储该数组中每个元素的位数。 如果未指定,则numpy将根据数组中的数据自动分配np.int64或np.float64之一。

You can see the shape of an array using

您可以使用来查看数组的形状

print(np.shape(x))

This prints the shape of the array x to the console. Also you can see the rank of an array using

这会将数组x的形状打印到控制台。 您也可以使用

print(len(np.shape(x))

since shape is basically a tuple with the lengths of axes. Apart from this, there are special functions which can create arrays of particular types for eg. An Identity matrix, a diagonal matrix, zeros matrix etc. I will discuss that in a future post.

因为形状基本上是具有轴长的元组。 除此之外,还有一些特殊功能可以为例如创建特定类型的数组。 单位矩阵,对角矩阵,零矩阵等。我将在以后的文章中进行讨论。

基本阵列运算(元素运算) (Basic Array Operations (Elementwise Operations))

Let us discuss some simple fundamental operations which we can do with arrays.

让我们讨论一些可以对数组进行操作的简单基本操作。

All the basic arithmetic operations like addition, subtraction, multiplication and division can be performed on two arrays having same shape. These operations are carried out in an elementwise manner i.e. corresponding elements in the same position across the two arrays are operated upon.

所有基本的算术运算(如加,减,乘和除)都可以在具有相同形状的两个数组上执行。 这些操作以元素方式执行,即在两个数组中位于相同位置的相应元素被操作。

Unary operations of negation and reciprocal are also done elementwise. Let’s see them in action in code.

否定和倒数的一元运算也是按元素进行的。 让我们看看它们在代码中的作用。

Image for post
Defining array in numpy
在numpy中定义数组

Let us start by defining two arrays, each of shape 2 x 2. Data could be any numbers of your choice.

让我们从定义两个形状为2 x 2的数组开始。数据可以是您选择的任意数量。

Image for post
Adding numpy arrays
添加numpy数组

We can simply use the addition symbol for adding two numpy arrays together. Alternatively, you can use np.add(x, y)

我们可以简单地使用加号将两个numpy数组加在一起。 或者,您可以使用np.add(x,y)

Image for post
Subtracting one array from another
从另一个数组中减去一个数组

We can subtract one array from another using minus symbol or alternatively by using np.subtract(x, y)

我们可以使用减号或使用np.subtract(x,y)从另一个数组中减去一个数组

Image for post
Multiplying two numpy arrays
将两个numpy数组相乘

Two arrays can be multiplied using the asterisk symbol as shown or by inbuilt function np.multiply(x, y)

可以使用所示的星号符号或内置函数np.multiply(x,y)将两个数组相乘

Image for post
Dividing numpy arrays
划分numpy数组

Two arrays could be divided elementwise by using either the forward slash or by using np.divide(x, y)

可以使用正斜杠或使用np.divide(x,y)将两个数组按元素划分

Image for post
Reciprocal of an array
数组的倒数

The unary operation of taking a reciprocal can simply be done by performing 1 / arrayname thanks to broadcasting in numpy. I will cover broadcasting in another post, but it simply allows you to do operations without having to particularly bother a ton about the datatypes or shapes of arrays under operation.

由于在numpy中进行广播,只需执行1 / arrayname即可简单地进行一元对数的一元运算。 我将在另一篇文章中介绍广播,但是它只允许您执行操作,而不必特别担心正在操作的数组的数据类型或形状。

Image for post
Negating an array
取反数组

Similarly, the unary operation of negation can simply be performed by adding a minus sign in front of the array variable.

类似地,可以通过在数组变量前面添加减号来简单地执行一元求反运算。

The code snippets above can be viewed on my github in this repository Numpy Explained.

可以在我的github中的Numpy Explained存储库中查看上面的代码段

In the next post, we will look at some special arrays and functions to create the same. You can view that post by clicking on the following link

在下一篇文章中,我们将介绍一些特殊的数组和函数来创建它们。 您可以通过单击以下链接查看该帖子

Numpy中的特殊数组 (Special Arrays in Numpy)

翻译自: https://medium.com/analytics-vidhya/numpy-arrays-35b171a18cae

numpy数组操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值