Introduction
A very important function of NumPy is to operate multi-dimensional arrays. Multi-dimensional array objects are also called ndarray. We can perform a series of complex mathematical operations on the basis of ndarray.
This article will introduce some basic and common ndarray operations, which you can use in data analysis.
Create ndarray
There are many ways to create an ndarray, we can use np.random to randomly generate data:
import numpy as np
# Generate some random data
data = np.random.randn(2, 3)
data
array([[ 0.0929, 0.2817, 0.769 ],
[ 1.2464, 1.0072, -1.2962]])
In addition to random creation, you can also create from the list:
data1 = [6, 7.5, 8, 0, 1]
arr1 = np.array(data1)
array([6. , 7.5, 8. , 0. , 1. ])
Create a multidimensional array from the list:
data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
arr2 = np.array(data2)
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
Use np.zeros to create an array with an initial value of 0:
np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
Create a 2-dimensional array:
np.zeros((3, 6))
array([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
Use empty to create a 3-dimensional array:
np.empty((2, 3, 2))
array([[[0., 0.],
[0., 0.],
[0., 0.]],
[[0., 0.],
[0., 0.],
[0., 0.]]])
Note that here we see that the value of the array created by empty is 0, which is not necessarily true. Empty will randomly select spaces from the memory to return, and there is no guarantee that there are no values in these spaces. So after we use empty to create the array, before using it, we must remember to initialize them.
Use arange to create an array of range classes:
np.arange(15)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
Specify the dtype of the elements in the array:
arr1 = np.array([1, 2, 3], dtype=np.float64)
arr2 = np.array([1, 2, 3], dtype=np.int32)
Properties of ndarray
The shape of the array can be obtained through data.shape.
data.shape
(2, 3)
Get dimensional information through ndim:
arr2.ndim
2
The specific data type can be obtained through data.dtype.
data.dtype
dtype('float64')
Type conversion of elements in ndarray
After creating a type of ndarray, you can also convert it:
arr = np.array([1, 2, 3, 4, 5])
arr.dtype
dtype('int64')
float_arr = arr.astype(np.float64)
float_arr.dtype
dtype('float64')
Above, we used astype to convert an ndarray of int64 type to float64.
If the range of the conversion type does not match, the truncation operation is automatically performed:
arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
arr.astype(np.int32)
array([ 3, -1, -2, 0, 12, 10], dtype=int32)
Note that this is the truncation of the decimals, and there is no rounding up or down.
Mathematical operations of ndarray
Arrays can be operated on with constants or on arrays:
arr = np.array([[1., 2., 3.], [4., 5., 6.]])
arr * arr
array([[ 1., 4., 9.],
[16., 25., 36.]])
arr + 10
array([[11., 12., 13.],
[14., 15., 16.]])
arr - arr
array([[0., 0., 0.],
[0., 0., 0.]])
1 / arr
array([[1. , 0.5 , 0.3333],
[0.25 , 0.2 , 0.1667]])
arr ** 0.5
array([[1. , 1.4142, 1.7321],
[2. , 2.2361, 2.4495]])
Arrays can also be compared, the comparison is the size of each element in the array:
arr2 = np.array([[0., 4., 1.], [7., 2., 12.]])
arr2 > arr
array([[False, True, False],
[ True, False, True]])
index and slice
Basic use
First look at the basic use of index and slice. Index is basically used in the same way as an ordinary array, which is used to access an element in the array.
When slicing, note that the elements in the returned array after slicing are references to the elements in the original array. Modifying the sliced array will affect the original array.
# 构建一维数组
arr = np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# index访问
arr[5]
5
# 切片访问
arr[5:8]
array([5, 6, 7])
# 切片修改
arr[5:8] = 12
array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])
# 切片可以修改原数组的值
arr_slice = arr[5:8]
arr_slice[1] = 12345
arr
array([ 0, 1, 2, 3, 4, 12, 12345, 12, 8,
9])
# 构建二维数组
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr2d[2]
array([7, 8, 9])
# index 二维数组
arr2d[0][2]
3
# index二维数组
arr2d[0, 2]
3
# 构建三维数组
arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
arr3d
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
# index三维数组
arr3d[0]
array([[1, 2, 3],
[4, 5, 6]])
# copy是硬拷贝,和原数组的值相互不影响
old_values = arr3d[0].copy()
arr3d[0] = 42
arr3d
array([[[42, 42, 42],
[42, 42, 42]],
[[ 7, 8, 9],
[10, 11, 12]]])
arr3d[0] = old_values
arr3d
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
# index 三维数组
arr3d[1, 0]
array([7, 8, 9])
x = arr3d[1]
x
array([[ 7, 8, 9],
[10, 11, 12]])
x[0]
array([7, 8, 9])
index with slice
A slice can also be used as an index. As an index, it means an index range value.
The slice represented by the index can have many forms.
There is a head and a tail, which means that the index starts from 1 to the end of 6-1:
arr[1:6]
array([ 1, 2, 3, 4, 64])
Without a head and a tail, it means that the index starts at 0 and ends at -1 at the end:
arr2d[:2]
array([[1, 2, 3],
[4, 5, 6]])
If there is a head and no tail, it means starting from the beginning to the end of all the data:
arr2d[:2, 1:]
array([[2, 3],
[5, 6]])
arr2d[1, :2]
array([4, 5])
boolean index
The index can also use a boolean value to indicate whether to select the data of this index.
Let's first look at how to construct an array of boolean type:
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
names == 'Bob'
array([ True, False, False, True, False, False, False])
Above we returned an array containing only True and False by way of comparison.
This array can be used as an index value to access the array:
# 构建一个7 * 4 的数组
data = np.random.randn(7, 4)
array([[ 0.275 , 0.2289, 1.3529, 0.8864],
[-2.0016, -0.3718, 1.669 , -0.4386],
[-0.5397, 0.477 , 3.2489, -1.0212],
[-0.5771, 0.1241, 0.3026, 0.5238],
[ 0.0009, 1.3438, -0.7135, -0.8312],
[-2.3702, -1.8608, -0.8608, 0.5601],
[-1.2659, 0.1198, -1.0635, 0.3329]])
# 通过boolean数组来访问:
data[names == 'Bob']
array([[ 0.275 , 0.2289, 1.3529, 0.8864],
[-0.5771, 0.1241, 0.3026, 0.5238]])
When indexing rows, you can also index columns:
data[names == 'Bob', 3]
array([0.8864, 0.5238])
You can use the ~
symbol to reverse:
data[~(names == 'Bob')]
array([[-2.0016, -0.3718, 1.669 , -0.4386],
[-0.5397, 0.477 , 3.2489, -1.0212],
[ 0.0009, 1.3438, -0.7135, -0.8312],
[-2.3702, -1.8608, -0.8608, 0.5601],
[-1.2659, 0.1198, -1.0635, 0.3329]])
We can set the value through a boolean array, which is very useful in actual projects:
data[data < 0] = 0
array([[0.275 , 0.2289, 1.3529, 0.8864],
[0. , 0. , 1.669 , 0. ],
[0. , 0.477 , 3.2489, 0. ],
[0. , 0.1241, 0.3026, 0.5238],
[0.0009, 1.3438, 0. , 0. ],
[0. , 0. , 0. , 0.5601],
[0. , 0.1198, 0. , 0.3329]])
data[names != 'Joe'] = 7
array([[7. , 7. , 7. , 7. ],
[0. , 0. , 1.669 , 0. ],
[7. , 7. , 7. , 7. ],
[7. , 7. , 7. , 7. ],
[7. , 7. , 7. , 7. ],
[0. , 0. , 0. , 0.5601],
[0. , 0.1198, 0. , 0.3329]])
Fancy indexing
Fancy indexing is also called fancy indexing, which refers to the use of an integer array for indexing.
For example, we first create an 8 * 4 array:
arr = np.empty((8, 4))
for i in range(8):
arr[i] = i
arr
array([[0., 0., 0., 0.],
[1., 1., 1., 1.],
[2., 2., 2., 2.],
[3., 3., 3., 3.],
[4., 4., 4., 4.],
[5., 5., 5., 5.],
[6., 6., 6., 6.],
[7., 7., 7., 7.]])
Then use an integer array to index, then the rows will be selected in the specified order:
arr[[4, 3, 0, 6]]
array([[4., 4., 4., 4.],
[3., 3., 3., 3.],
[0., 0., 0., 0.],
[6., 6., 6., 6.]])
You can also use negative values to index:
arr[[-3, -5, -7]]
array([[5., 5., 5., 5.],
[3., 3., 3., 3.],
[1., 1., 1., 1.]])
Fancy indexes can also be used in combination:
arr = np.arange(32).reshape((8, 4))
arr
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31]])
Above we have constructed an 8 * 4 array.
arr[[1, 5, 7, 2], [0, 3, 1, 2]]
array([ 4, 23, 29, 10])
Then take their first value in column 2, the third value in column 6, and so on. Finally, a 1-dimensional array is obtained.
Array transformation
We can transform between arrays of different dimensions, and we can also transform the axis of the array.
The reshape method can convert an array into any shape:
arr = np.arange(15).reshape((3, 5))
arr
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
The array also provides a T command, which can reverse the axis of the array:
arr.T
array([[ 0, 5, 10],
[ 1, 6, 11],
[ 2, 7, 12],
[ 3, 8, 13],
[ 4, 9, 14]])
For high-dimensional arrays, you can use transpose to transpose the axis:
arr = np.arange(16).reshape((2, 2, 4))
arr
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]])
arr.transpose((1, 0, 2))
array([[[ 0, 1, 2, 3],
[ 8, 9, 10, 11]],
[[ 4, 5, 6, 7],
[12, 13, 14, 15]]])
How do you understand the above transpose((1, 0, 2))?
Its meaning is to reverse the x and y axis, and keep the z axis unchanged.
Above we created a 3-dimensional array with 3 axes by using the reshape((2, 2, 4)) method. Its shape is 2 2 4.
First look at the corresponding relationship:
(0,0)-》 [ 0, 1, 2, 3]
(0,1)-》 [ 4, 5, 6, 7]
(1,0)-》 [ 8, 9, 10, 11]
(1,1)-》 [12, 13, 14, 15]
After conversion:
(0,0)-》 [ 0, 1, 2, 3]
(0,1)-》 [ 8, 9, 10, 11]
(1,0)-》[ 4, 5, 6, 7]
(1,1)-》 [12, 13, 14, 15]
So we got the result above.
The axis conversion of a multi-dimensional array may be more complicated, so please understand.
You can also use swapaxes to exchange two axes. The above example can be rewritten as:
arr.swapaxes(0,1)
This article has been included in http://www.flydean.com/09-python-numpy-ndarray/The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "Program those things", know technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。