numpy学习指南_2020年终极NumPy初学者指南。

numpy学习指南

NumPy is a python library used for working with arrays. It also has functions for working in the domain of linear algebra, fourier transform, and matrices. In this article, I hope to teach you the fundamentals of NumPy.

NumPy是用于处理数组的python库。 它还具有用于线性代数,傅立叶变换和矩阵领域的功能。 在本文中,我希望教您NumPy的基础知识。

If you want to learn about Python first you can check out my beginners guide.

如果您想先学习Python,可以查阅我的初学者指南

入门 (Getting Started)

You need to have Python installed and PIP. If you have already done this you can start working with NumPy. Before you can use NumPy we have to install with the following command line:

您需要安装Python和PIP。 如果您已经完成此操作,则可以开始使用NumPy。 在使用NumPy之前,我们必须使用以下命令行进行安装:

C:\Users\Your Name>pip install numpy

Then open up a python script or file and import the library:

然后打开python脚本或文件并导入库:

import numpy

Now, NumPy is imported and ready for you to use. NumPy usually is important as np instead of NumPy:

现在,NumPy已导入,可供您使用。 通常,NumPy作为np而不是NumPy很重要:

import numpy as np

We can check if NumPy works by adding two simple lines of code:

我们可以通过添加两行简单的代码来检查NumPy是否起作用:

arr = np.array([1, 2, 3, 4, 5])
print(arr)#output: [1 2 3 4 5]

创建一个数组 (Creating an Array)

NumPy was developed to work with Arrays, so let’s create one with NumPy. An important thing to know is that NumPy uses the ‘ndarray’ object to create an array. You can create one as follows:

NumPy是为与数组一起使用而开发的,因此让我们使用NumPy创建一个。 要知道的重要一点是NumPy使用'ndarray'对象创建一个数组。 您可以如下创建一个:

import numpy as np #import the library
arr = np.array([1, 2, 3, 4, 5]) #initializing an ndarray
print(arr) #print the array
print(type(arr)) #output: <class 'numpy.ndarray'>

多维数组 (Multidimensional Arrays)

A dimension in arrays is one level of array depth.

数组中的维度是数组深度的一级。

0-D Arrays

0维数组

Create a 0-D array with value 30

创建一个值为30的0-D数组

import numpy as np
arr = np.array(42)
print(arr)

一维阵列 (1-D Arrays)

These arrays are the most common arrays used in programming and python.

这些数组是编程和python中最常用的数组。

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)

二维阵列 (2-D Arrays)

An array that has 1-D arrays as its elements is called a 2-D array.

以一维数组为元素的数组称为二维数组。

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

3-D阵列 (3-D arrays)

An array that has 2-D arrays (matrices) as its elements is called a 3-D array.

以2维数组(矩阵)为元素的数组称为3维数组。

import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)

Within NumPy you can check how many dimensions an array actually has:

在NumPy中,您可以检查数组实际具有的维数:

import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)#output:0
1
2
3

访问元素 (Accessing Elements)

Array indexing is the same as accessing an array element. Ou can access an array element by referring to its index number.

数组索引与访问数组元素相同。 Ou可以通过引用其索引号来访问数组元素。

Example

import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[0])#output: 1

阵列切片 (Array Slicing)

Slicing in python means taking elements from one given index to another given index.

python中的切片意味着将元素从一个给定的索引带到另一个给定的索引。

Example:

例:

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])#output:[2 3 4 5]

数组迭代 (Array Iterating)

Iterating means going through elements one by one. An example below:

迭代意味着一步一步地遍历元素。 下面的例子:

import numpy as np
arr = np.array([1, 2, 3])
for x in arr:
print(x)#output: 1
2
3

阵列联接 (Array Joins)

Joining means putting contents of two or more arrays in a single array.

连接意味着将两个或多个数组的内容放在单个数组中。

Join two arrays

连接两个数组

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)#output:[1 2 3 4 5 6]

分割阵列 (Split Array)

Splitting is the reverse operation of Joining.

拆分是联接的相反操作。

Example

In the example below, you see one array been split up in 3 separate arrays.

在下面的示例中,您看到一个阵列被分成3个单独的阵列。

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr)#output:[array([1, 2]), array([3, 4]), array([5, 6])]

排序数组 (Sorting Arrays)

Sorting means putting elements in an ordered sequence.

排序是指将元素按有序顺序排列。

Example

Sort the array:

排序数组:

import numpy as np
arr = np.array([3, 2, 0, 1])
print(np.sort(arr))#output: [0 1 2 3]

随机数 (Random Numbers)

We can also use NumPy to generate a random number:

我们还可以使用NumPy生成一个随机数:

from numpy import random
x = random.randint(100)
print(x)#output: 10

结论 (Conclusion)

After this article, I hope you know the fundamentals of NumPy and explore yourself to become a Python expert!

读完本文后,希望您了解NumPy的基础知识,并探索成为一名Python专家!

翻译自: https://levelup.gitconnected.com/the-ultimate-numpy-guide-for-beginners-in-2020-411c9b1821d3

numpy学习指南

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值