numpy基本知识_NumPy-基本知识!

这篇博客介绍了NumPy的基础知识,包括其在数据分析和科学计算中的重要性。通过翻译自Analytics Vidhya的文章,读者将了解到如何使用Python库NumPy进行数组操作和基本计算。
摘要由CSDN通过智能技术生成

numpy基本知识

This article is for people who have zero knowledge of NumPy so that they can get a little hang of it to kick start.

本文适用于对NumPy知识为零的人们,因此他们可以从中掌握一些点子来开始。

NumPy is the package for scientific and mathematical computing in Python. While NumPy is widely used in an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms basic linear algebra, basic statistical operations, random simulation and much more we will, however, today scratch the very basics of NumPy.

NumPy是使用Python进行科学和数学计算的软件包。 尽管NumPy在各种例程中广泛用于数组的快速操作,包括数学,逻辑,形状处理,排序,选择,I / O,离散傅里叶变换,基本线性代数,基本统计运算,随机模拟等,我们将做更多但是,今天,NumPy的基础知识已经开始。

Entire article I will use code from Jupyter Notebook and assuming you already know basics of Python and installed NumPy.

整篇文章中,我将使用Jupyter Notebook中的代码,并假设您已经了解Python的基础知识并安装了NumPy。

We will start by importing NumPy.

我们将从导入NumPy开始。

In[1]: import numpy as np

NumPy数组的尺寸 (Dimensions of NumPy array)

Dimensions of NumPy array
https://fgnt.github.io/python_crashkurs_doc/include/numpy.html : //fgnt.github.io/python_crashkurs_doc/include/numpy.html

一维数组 (One dimensional array)

Let us create an array using a python list. ( Not a recommended way )

让我们使用python列表创建一个数组。 (不推荐)

In[2]: np.array([1,2,3,4,5])
Out[2]: array([1,2,3,4,5])

One very important property of NumPy which more like a constraint is, NumPy array can have only a single data type. Meaning, one cannot have an array of mixed data types. The moment you try to achieve that NumPy will implicitly try to upcast where possible. Below code, we can see that even though we have integers they are automatically upcasted to string my NumPy.

NumPy的一个非常重要的属性(更像是约束)是,NumPy数组只能具有单个数据类型。 这意味着,不能有一组混合数据类型。 您尝试实现NumPy的那一刻,将在可能的情况下隐式尝试向上转换。 在下面的代码中,我们可以看到,即使我们有整数,它们也会被自动转换为字符串NumPy。

In[3]: print(np.array([1,2,’Hello’,3,4])Out[3]: ‘1’,’2’,’Hello’,’3’,’4’

二维数组 (Two-dimensional array)

NumPy array can be multidimensional also. Here is an example of a 2x5 matrix.

NumPy数组也可以是多维的。 这是2x5矩阵的示例。

In[4]: np.array([[1,2,3,4,5], [1,2,3,4,5]])Out[4]: array([[1,2,3,4,5],
[1,2,3,4,5]])

使用内置例程创建数组 (Creating arrays using built-in routines)

Numpy basics

NumPy属性 (NumPy Attributes)

  • 1. ndim - Number of dimensions.

    1. ndim-尺寸数。
  • 2. shape - The size of each dimension.

    2.形状-每个尺寸的大小。
  • 3. size - The total size of the array.

    3. size-数组的总大小。
  • 4. dtype - The data type of array elements.

    4. dtype-数组元素的数据类型。
  • 5. itemsize - Byte size of each array element.

    5. itemsize-每个数组元素的字节大小。
  • 6. nbytes - Total size of the array. It is equal to itemsize times size.

    6. nbytes-数组的总大小。 它等于项目大小乘以大小。

Let’s create a 3-dimensional array of shape (3,4,5) using random variables and examine each attribute.

让我们使用随机变量创建一个形状为(3,4,5)的3维数组,并检查每个属性。

In[5]: a=np.random.randint(10, (3,4,5))       print(a.ndim)       print(a.nshape)       print(a.size) #Multiplication of shape values       print(a.dtype)       print(a.itemsize)       print(a.nbyte)Out[5]: 3       (3,4,5)       60       Int64       8 bytes       480 bytes

数组索引 (Array Indexing)

If you have come this far, I’m sure you are well aware of lists and it’s indexing. Array indexing is quite familiar. Let’s dive into examples.

如果您走了这么远,我敢肯定您对列表及其索引很了解。 数组索引非常熟悉。 让我们深入研究示例。

In[6]: a=np.array([4,8,3,0,1,5])       a[0]Out[6]: 5In[7]: a[4]Out[7]: 1

We use negative indices to index from the end of the array.

我们使用负索引从数组末尾开始索引。

In[8]: a[-1]Out[8]: 5In[8]: a[-2]Out[8]: 1

Negative indexing might be confusing some times. So, I have a visual representation below to give a clear idea. Another point to be noted while negative indexing is it starts from 1 rather than 0.

负索引可能会使您感到困惑。 因此,我在下面有一个直观的表述来给出清晰的想法。 负索引时要注意的另一点是它从1开始而不是0。

Image for post

In a multidimensional array, you access items using comma-separated indices.

在多维数组中 ,您使用逗号分隔的索引访问项目。

In[9]: a=np.array([[2,3,5,8,7],                   [4,1,0,9,6],                   [6,3,4,0,6]])       # 1st element of 1st list or element at position 0,0       a[0 , 0]Out[9]: 2In[10]: a[2, -1]   #Last element of last listOut[10]: 6

数组切片 (Array slicing)

As we use of square brackets for extracting elements of the array we can use it for extracting subarray also with a combination of the colon(:). The syntax is similar to range function except we use square brackets here.

当我们使用方括号来提取数组的元素时,我们也可以将其用于冒号(:)的组合来提取子数组。 除了在这里使用方括号外,语法与范围函数相似。

array_variable[start : stop: step]

array_variable [开始:停止:步骤]

The default values for start is 0, stop is size of dimension and step is 1

开始的默认值为0,停止的默认值为尺寸,步长为1

In[11]: x = np.arange(10)        xOut[11]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])In[12]: x[:5] #first five elementsOut[12]: array([0, 1, 2, 3, 4])In[13]: x[5:] # elements from index 5Out[13]: array([5, 6, 7, 8, 9])In[14]: x[4:7] # subarrayOut[14]: array([4, 5, 6])In[15]: x[::2] # every other elementOut[15]: array([0, 2, 4, 6, 8])In[16]: x[1::2] # every other element, starting at index 1Out[16]: array([1, 3, 5, 7, 9])In[17]: x[::-1] # all elements, reversedOut[17]: array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])In[18]: x[5::-2] # reversed every other from index 5Out[18]: array([5, 3, 1])

Multidimensional array slicing also works the same way the only difference is we need to use comma(,) to separate rows and columns.

多维数组切片也以相同的方式工作,唯一的区别是我们需要使用逗号(,)分隔行和列。

In[19]: a=np.array( [[2,3,5,8,7],                    [4,1,0,9,6],                    [6,3,4,0,6]] )       # 2 rows 3 columns       a[ :2, :3]Out[19]: array( [[2,3,5,8,7],                 [4,1,0,9,6]] )In[20]: a[ :3, :2] # 3 rows 2 columnsOut[20]: array( [[2,3],                [4,1],                [6,3]] )

The next one is a bit tricky. We will reverse the element from both rows and columns. Compare the original array with the below output.

下一个有点棘手。 我们将从行和列中反转元素。 将原始数组与以下输出进行比较。

In[21]: a[ ::-1, ::-1]Out[21]: array([[6, 0, 4, 3, 6],                [6, 9, 0, 1, 4],                [7, 8, 5, 3, 2]] )

One way where array slicing is different from list slicing is, the array slicing returns a view of the original array unlike in the list it returns the copy. So, any modification in the sliced subarray also reflects in the original array.

数组切片与列表切片不同的一种方式是,数组切片返回原始数组的视图,与列表中返回副本的列表不同。 因此,切片后的子数组中的任何修改也会反映在原始数组中。

数组串联和拆分 (Array concatenation and splitting)

Joining or concatenating array in NumPy is done by using np.concatenate, np.vstack, np.hstack, np.dstack. Don’t be worried by the sophisticated names “stack”, it's very simple operations here.

通过使用np.concatenate,np.vstack,np.hstack,np.dstack在NumPy中连接或连接数组。 不必担心复杂的名称“堆栈”,这里的操作非常简单。

In[22]: a= np.array([ 1,2,3,4 ])        b=np.arrar([5,6,7,8])        c=np.array([[9,8,7,6],                   [5,4,3,2]])        d=np.array[([5,6],                    [4,7]])        np.concatenate([a,b])Out[22]: array([1,2,3,4,5,6,7,8])In[23]: np.vstack([a,c])Out[23]: array([[1,2,3,4 ],                [9,8,7,6],                [5,4,3,2]])In[24]: np.hstack([c,d])Out[24]: array([[9,8,7,6,5,6],               [5,4,3,2,4,7]])
#np.dstack is used on three-dimensional array

The same goes for array splitting. We use np.split, np.vsplit and np.hsplit. All these work exactly opposite of concatenate.

数组拆分也是如此 。 我们使用np.split,np.vsplit和np.hsplit。 所有这些工作与连接完全相反。

In[25]: x = [1,2,3,99,99,3,2,1]       x1, x2, x3 = np.split(x,[3,5])       print(x1, x2, x3)Out[25]: [1 2 3] [99 99] [3 2 1]In[26]: a=np.arange(16).reshape((4,4))        aOut[26]: array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In[27]: upper,lower = np.vsplit(a,[2]) print(upper) print(lower)Out[27]: [0 1 2 3] [4 5 6 7]In[28]: left, right= np.hsplit(a,[2]) print(left) print(right)Out[28]: [[0 1]
[4 5]
[8 9]
[12 13]]
[[2 3]
[6 7]
[10 11]
[14 15]]

So that's it for basics of NumPy. Again this is for people with zero knowledge of NumPy. I will be posting more advanced concepts of NumPy.

这就是NumPy的基础知识。 同样,这适用于对NumPy零知识的人。 我将发布NumPy的更高级的概念。

Continue reading

继续阅读

Let’s connect

让我们连接

翻译自: https://medium.com/analytics-vidhya/numpy-the-very-basics-6ce19206ee22

numpy基本知识

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值