📜  NumPy Ndarray

📅  最后修改于: 2020-10-27 02:13:34             🧑  作者: Mango

NumPy Ndarray

Ndarray是在numpy中定义的n维数组对象,该对象存储相似类型的元素的集合。换句话说,我们可以将ndarray定义为数据类型(dtype)对象的集合。

可以使用基于0的索引来访问ndarray对象。 Array对象的每个元素在内存中包含相同的大小。

创建一个ndarray对象

可以使用numpy模块的数组例程创建ndarray对象。为此,我们需要导入numpy。

>>> a = numpy.array

考虑下图。

我们还可以将集合对象传递到数组例程中,以创建等效的n维数组。语法如下。

>>> numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)

下表描述了这些参数。

SN Parameter Description
1 object It represents the collection object. It can be a list, tuple, dictionary, set, etc.
2 dtype We can change the data type of the array elements by changing this option to the specified type. The default is none.
3 copy It is optional. By default, it is true which means the object is copied.
4 order There can be 3 possible values assigned to this option. It can be C (column order), R (row order), or A (any)
5 subok The returned array will be base class array by default. We can change this to make the subclasses passes through by setting this option to true.
6 ndmin It represents the minimum dimensions of the resultant array.

要使用列表创建数组,请使用以下语法。

>>> a = numpy.array([1, 2, 3])

若要创建多维数组对象,请使用以下语法。

>>> a = numpy.array([[1, 2, 3], [4, 5, 6]])

要更改数组元素的数据类型,请在集合中提及数据类型的名称。

>>> a = numpy.array([1, 3, 5, 7], complex)

查找数组的尺寸

ndim函数可用于查找数组的尺寸。

>>> import numpy as np
>>> arr = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [9, 10, 11, 23]])

>>> print(arr.ndim)

查找每个数组元素的大小

itemssize函数用于获取每个数组项目的大小。它返回每个数组元素占用的字节数。

考虑以下示例。

#finding the size of each item in the array
import numpy as np
a = np.array([[1,2,3]])
print("Each item contains",a.itemsize,"bytes")

输出:

Each item contains 8 bytes.

查找每个数组项的数据类型

要检查每个数组项的数据类型,请使用dtype函数。考虑以下示例,检查数组项的数据类型。

#finding the data type of each array item
import numpy as np
a = np.array([[1,2,3]])
print("Each item is of the type",a.dtype)

输出:

Each item is of the type int64

查找数组的形状和大小

为了获得数组的形状和大小,使用了与numpy数组关联的大小和形状函数。

考虑以下示例。

import numpy as np
a = np.array([[1,2,3,4,5,6,7]])
print("Array Size:",a.size)
print("Shape:",a.shape)

输出:

Array Size: 7
Shape: (1, 7)

重塑数组对象

所谓数组的形状,是指多维数组的行数和列数。但是,numpy模块为我们提供了一种通过更改多维数组的行数和列数来重塑数组形状的方法。

与ndarray对象关联的reshape()函数用于对数组进行整形。它接受两个参数,这些参数指示数组新形状的行和列。

让我们重塑下图中给出的数组。

import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
print("printing the original array..")
print(a)
a=a.reshape(2,3)
print("printing the reshaped array..")
print(a)

输出:

printing the original array..
[[1 2]
 [3 4]
 [5 6]]
printing the reshaped array..
[[1 2 3]
 [4 5 6]]

在数组中切片

在NumPy数组中切片是从数组中提取一系列元素的方法。数组中的切片与Python列表中的切片相同。

考虑以下示例以print数组的特定元素。

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

输出:

2
5

上面的程序从数组的第0个索引打印第2个元素,从数组的第2个索引打印第0个元素。

Linspace

linspace()函数返回给定间隔内均匀间隔的值。下面的示例在给定的间隔5-15中返回10个均分的值

import numpy as np
a=np.linspace(5,15,10) #prints 10 values which are evenly spaced over the given interval 5-15
print(a)

输出:

[ 5.          6.11111111  7.22222222  8.33333333  9.44444444 10.55555556
 11.66666667 12.77777778 13.88888889 15.        ]

查找数组元素的最大,最小和总和

NumPy提供max(),min()和sum()函数,分别用于查找数组元素的最大值,最小值和总和。

考虑以下示例。

import numpy as np
a = np.array([1,2,3,10,15,4])
print("The array:",a)
print("The maximum element:",a.max())
print("The minimum element:",a.min())
print("The sum of the elements:",a.sum())

输出:

The array: [ 1  2  3 10 15  4]
The maximum element: 15
The minimum element: 1
The sum of the elements: 35

NumPy阵列轴

NumPy多维数组由轴表示,其中轴0表示列,轴1表示行。我们可以提到轴来执行行级或列级计算,例如添加行或列元素。

要计算每列中的最大元素,每行中的最小元素以及所有行元素的加法,请考虑以下示例。

import numpy as np
a = np.array([[1,2,30],[10,15,4]])
print("The array:",a)
print("The maximum elements of columns:",a.max(axis = 0)) 
print("The minimum element of rows",a.min(axis = 1))
print("The sum of all rows",a.sum(axis = 1))

输出:

The array: [[1  2  30]
           [10  15  4]]
The maximum elements of columns: [10 15 30]
The minimum element of rows [1 4]
The sum of all rows [33 29]

求平方根和标准偏差

与numpy数组关联的sqrt()和std()函数分别用于查找数组元素的平方根和标准偏差。

标准偏差表示数组中每个元素与numpy数组的平均值相差多少。

考虑以下示例。

import numpy as np
a = np.array([[1,2,30],[10,15,4]])
print(np.sqrt(a))
print(np.std(a))

输出:

[[1.         1.41421356 5.47722558]
 [3.16227766 3.87298335 2.        ]]
10.044346115546242

阵列上的算术运算

numpy模块允许我们直接在多维数组上执行算术运算。

在以下示例中,对两个多维数组a和b执行算术运算。

import numpy as np
a = np.array([[1,2,30],[10,15,4]])
b = np.array([[1,2,3],[12, 19, 29]])
print("Sum of array a and b\n",a+b)
print("Product of array a and b\n",a*b)
print("Division of array a and b\n",a/b)

数组串联

numpy为我们提供了垂直堆叠和水平堆叠,这使我们可以垂直或水平连接两个多维数组。

考虑以下示例。

import numpy as np
a = np.array([[1,2,30],[10,15,4]])
b = np.array([[1,2,3],[12, 19, 29]])
print("Arrays vertically concatenated\n",np.vstack((a,b)));
print("Arrays horizontally concatenated\n",np.hstack((a,b)))

输出:

Arrays vertically concatenated
 [[ 1  2 30]
 [10 15  4]
 [ 1  2  3]
 [12 19 29]]
Arrays horizontally concatenated
 [[ 1  2 30  1  2  3]
 [10 15  4 12 19 29]]