python numpy的

它是什么 ? (What is it ?)

If you work or are learning python, sooner or later you will bump into Numpy arrays, I’d venture that numpy, along with pandas dataframes are the workhorses of data as far as python is concerned. In layman terms Numpy arrays are data containers that can represent multiple dimensions and be queried and operated on, or if you prefer the official definition from the docs:

如果您正在工作或正在学习python,则迟早会碰到Numpy数组,我敢冒险说numpy和pandas数据帧是python的主要数据源。 用外行术语来说,Numpy数组是可以表示多个维度的数据容器,可以对其进行查询和操作,或者如果您更喜欢docs的官方定义:

NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes.

从列表到Numpy数组 (From lists to Numpy Arrays)

You might be familiar with python lists :

您可能熟悉python列表:

list = [1,2,3,4,5,6]
print(list)>>>
[1, 2, 3, 4, 5, 6]

Well, a Numpy array at first glance is a very similar concept:

好吧,乍看之下,Numpy数组是一个非常相似的概念:

import numpy as npnp_array = np.array([1, 2, 3, 4, 5, 6])
print(np_array)>>>
[1 2 3 4 5 6]
Do note the lack of commas which tell us we are dealing with something different than a regular list.

SideNote: Why use Numpy ? : Because it is faster than a regular list and because its use is widespread ? ¯\_(ツ)_/¯

SideNote:为什么要使用Numpy? :是因为它比常规列表要快, 并且使用范围很广? ¯\ _(ツ)_ /¯

创建简单的数组: (Creating simple arrays:)

Peruse the following for simple Numpy array creation:

请仔细阅读以下内容以创建简单的Numpy数组:

类型和类型转换 (Type and type conversion)

You might have noticed that some numbers above are floats 0. and others are integers (ints) 1,2,3 , this begs the question, what types are supported and how to change them :

您可能已经注意到上面的一些数字是浮点数0.而另一些数字是整数1,2,3 (整数),这就引出了一个问题,支持哪些类型以及如何更改它们:

You can find all the types and more information here:Numpy data Types

外形尺寸 (Dimensions and Shape)

So far we’ve been covering very basic Numpy Arrays but the utility and complexity goes up once we start using multiple dimensions…

到目前为止,我们已经涵盖了非常基本的Numpy数组,但是一旦我们开始使用多个维度,实用性和复杂性就会增加……

Dimensions make more sense when paired with actual values or use cases, so for instance a list of grocery items can be expressed as a one dimensional Numpy array:

将尺寸与实际值或用例配对时,尺寸更有意义,例如,杂货项目列表可以表示为一维Numpy数组:

grocery_list = np.array(['eggs', 'milk', 'cereal', 'bacon'])print("Dimensions:", grocery_list.ndim)
>>> Dimensions: 1
print("Shape:", grocery_list.shape)
Dimensions: 1
>>> Shape: (4,)

You can check the number of dimensions (or axes) with ndim ;shape will give you the size of the array in each dimension, here 4 is the number of elements in our only dimension, the length of shape in this case 1 is also the number of dimensions.

您可以使用ndim检查尺寸(或轴)的ndim shape 将为您提供每个维度中数组的大小,这里4是我们唯一维度中元素的数量,即shape的长度 在这种情况下,1也是维数。

An X,Y chart consists of 2 pairs of data points and so it makes sense it can be expressed as a 2 dimensional array:

X,Y图表由两对数据点组成,因此可以将其表示为二维数组是有意义的:

chartData = X,Y = np.array([[1, 2, 3, 4],[2, 4, 6, 8]])Dimensions: 2Shape: (2, 4) length: 2 =  2 dimensions, first axe has 2 elements [1,2,3,4,] and [2,4,6,8], the second axe has 4 elements, also note we are assigning X and Y values upon creation which is a common way to pass values to a chart...print(X):
>>> [1 2 3 4]print(Y):
>>> [2 4 6 8]

A point in space X,Y,Z could be expressed in you guessed it 3 dimensions:

您可以将空间X,Y,Z中的点表示为3维:

point_in_space = X,Y,Z = np.array([[[1]],[[2]],[[3]]])Dimensions: 3Shape: (3, 1, 1)

If we add a 4th dimension ( a comment ) to the point we would have:

如果将第四维( 注释 )添加到该点,我们将具有:

point_in_spaceWithComment = X, Y, Z, comment = np.array([
[[[1]]],
[[[2]]],
[[[3]]],
[[['My Favorite Point']]]
])Dimensions: 4
Shape: (4, 1, 1, 1)

These are just examples to show you how to write from scratch dimensions and how to figure out how many dimensions you are dealing with, we could for instance rewrite the last example as something more easy on the eyes and practical:

这些只是示例,向您展示如何从头开始编写尺寸以及如何确定要处理的尺寸,例如,我们可以重写最后一个示例,使之看起来更容易且实用:

points_in_spaceWithComments = np.array([
[2, 4, 2, 'Comment1'],
[4, 6, 8, 'Comment2'],
[1, 4, 3, 'Comment3']
])Dimensions: 2
Shape: (3, 4)

数据I / O和索引 (Data I/O and Indexing)

By now I hope you have a rough idea of what a Numpy array is, to continue this overview let’s look at selecting, adding and deleting elements:

到目前为止,我希望您对Numpy数组有一个大概的了解,继续此概述,让我们看一下选择,添加和删除元素:

If you’ve done any work with lists on python this should look familiar, although it you look at the second example there is an axis argument and we haven’t talked about selecting things in a multidimensional Numpy array, for that we’ll need indexing and slicing

如果您已经完成了有关python列表的任何工作,则应该看起来很熟悉,尽管您在第二个示例中看到了一个axis参数,并且我们没有谈论在多维Numpy数组中选择内容,为此,我们需要索引和切片

处理多个维度: (Dealing with multiple dimensions:)

Selecting, Indexing, Slicing in multiple dimensions is surprisingly simple once you know the syntax, basically add a comma to get to the next dimension, everything we covered previously still applies:

一旦您知道语法,就可以在多个维度中进行选择,索引和切片,这非常简单,基本上是添加逗号以到达下一个维度,我们之前介绍的所有内容仍然适用:

重塑 (Reshaping)

One last Numpy thing you might encounter when starting is reshaping, which as its name implies modifies an existing array into a different shape:

重新启动时,您可能会遇到的最后一件Numpy事情是reshaping ,其名称暗示将现有数组修改为另一种形状:

还有什么 ? (What else ?)

Some places I’d steer you if you want to learn more advanced topics:

如果您想学习更多高级主题,我会指导您一些地方:

Meshgrids complex grid like arrays

复杂 网格状数组

Complex array manipulations

复杂的数组操作

Broadcasting (advanced array operations)

广播 (高级数组操作)

In general Numpy is another tool in your programmers bag, whenever you encounter complex numerical data (and other types) you need to store, manipulate and operate with ( and do it fast) Numpy is a good candidate to use.

通常,Numpy是程序员的另一种工具,每当遇到复杂的数值数据(和其他类型)时,您都需要存储,操纵和操作(并快速完成)Numpy是一个很好的选择。

Hope this helps you get started.

希望这可以帮助您入门。

Thanks for reading !

谢谢阅读 !

翻译自: https://medium.com/@k3no/python-numpy-aac458ea6134

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值