"Data Science Technology and Application" (Electrical Engineering Edition) Learning Summary: 1.1 NumPy library and multi-dimensional array ndarry

前言:笔者所用教材为电子工业的《数据科学技术与应用》,基本知识在课本中都有涉及,此处仅仅是记录个人学习历程中个人觉得有必要记录的知识点,主要用于今后考试复习。如有错误,欢迎提出

Table of contents

1.1.1 One-dimensional ndarray object
1.1.2 Two-dimensional ndarray object
1.1.3 Common methods for creating multi-dimensional arrays

1.1.1 One-dimensional ndarray object

1.Array attributes

Commonly used array attributes are as follows:

Attributes Comment
size Check the number of array elements. For one-dimensional arrays, the usage of size is similar to the len function.
it's me The full name is probably ndarray dimension. Check the array dimensions.
dtype Check the array data type. So far, it has no practical use for me.
2. Element access and slicing

The access and slicing of one-dimensional arrays are the same as the access and slicing of Python sequences. They all use square brackets [ ] and will not be described in detail here.

3. Filter array elements based on conditions

Give conclusion first

>>>num = np.array([1,2,3,4])
>>>num[ (num == 1) | (num = 2)]
array(['1','2'], dtype='int32')

Give the process another try

<omitted> (dog head saves life)

1.1.2 Two-dimensional ndarray object

1.Array attributes

Commonly used attributes are basically the same as one-dimensional arrays, except that there is an additional shape attribute:

Attributes Comment
size 元素总数 = 行数 * 列数
shape View the number of rows and columns of an array

shape这个词比较重要,跟后面创建多维数组的一个方法有点关系

2. Element access and slicing
The basic format of two-dimensional array access or slicing operation: arr[ row(list) , column(list) ]

• The row serial number and the column serial number are separated by ',' in the middle. •
The representation of row and column slices is the same as that of a one-dimensional array.
• Use ":" instead of rows or columns to indicate that all corresponding rows or columns are selected.

Among the various ways of slicing, one situation is more special:
>>> scores= np.array([[70,85,77,90,82,84,89],
[60,64,80,75,80,92,90],
[90,93,88,87,86,90,91][80,82,91,88,83,86,80],
[88,72,78,90,91,73,80]])
>>> scores[[1,3],[0,1]]   #抽取scores[1,0],scores[3,1]
array([60, 82])

When arr[rowlist, columnlist] appears, the slicing method of ndarray is slightly different from the slicing method of the subsequent DataFrame:

ndarray DataFrame
Extract scores[1, 0],scores[3, 1] Extract scores[1, [0, 1] ],scores[3, [0, 1] ]
3. Filter array elements based on conditions

(Same as conditional filtering for one-dimensional arrays)

1.1.3 Common methods for creating multidimensional arrays

1.arange() function

Note that it is arange not arrange

The arange function is used to create an arithmetic array. It is similar to the range function, with two differences:
1. Before use, arange needs to introduce numpy.
2. arange returns a data instead of a list.

np.arange(start, stop, step, dtype) start: starting value, can be ignored and not written, the default starts from 0;
stop: end value, the generated elements do not include the end value;
step: step size, can be ignored and not written , the default is 1;
dtype: Set the data type of the display element, the default is None.

The following are some basic uses of the arrange function:

  1. When one parameter is used, the parameter value is the end point, the starting point is the default value 0, and the step length is the default value 1.

  2. When there are two parameters, the first parameter is the starting point, the second parameter is the end point, and the step size takes the default value 1

  3. When there are three parameters, the first parameter is the starting point, the second parameter is the end point, and the third parameter is the step size. The step size supports decimals

>>> a=np.arange(5)
[0 1 2 3 4]
>>> b1=np.arange(1,5)
[1 2 3 4]
>>> c=np.arange(1,10,2)
[1 3 5 7 9]

In addition, the three parameters of the arange() function can also be floating point numbers.

>>> np.arange(0.3,1.5,0,3)
array([ 0.3, 0.6, 0.9, 1.2])
2.reshape() function

Changes the shape of the array without changing the original data. However, the parameters in the reshape() function need to satisfy that the product is equal to the total number of data in the array.
For example: when we rearrange 8 numbers using (2,3), python will report an error

However, the shape() function is used to read the length of the matrix. For example, shape[0] is to read the length of the first dimension of the matrix, which is equivalent to the number of rows. Its input parameter can be an integer representing the dimension, or it can be a matrix. The shape function returns a tuple, representing the dimensions/shape of the array (matrix). Examples are as follows:

  • w.shape[0] returns the number of rows of w
  • w.shape[1] returns the number of columns of w
  • df.shape(): View the number of rows and columns
  1. When the array (matrix) has only one dimension, the shape is only shape[0]. What is returned is the number of elements in the one-dimensional array (matrix). In layman's terms, it returns the number of columns, because the one-dimensional array has only one row. In the case of one-dimensional The array created in the array can be regarded as a list (or one-dimensional array). You can use () or [ ] when creating it. If it is multi-dimensional, use [ ]
>>> a=np.array([1,2])  #也可以写成 a=np.array((1,2)) 
>>> a
array([1, 2])
>>> a.shape
(2L,)
>>> a.shape[0]
2L
>>> a.shape[1]
IndexError: tuple index out of range   #最后报错是因为一维数组只有一个维度,可以用a.shape或a.shape[0]来访问
 
#这个使用的是两个()包裹,得到的数组和前面的一样
>>> a=np.array((1,2))
>>> a
array([1, 2]) 
  1. When the array has two dimensions (i.e., rows and columns), the tuple returned by a.shape represents the number of rows and columns of the array.

The arange function and the reshape function can also be used together:

In [8]: np.arange(1,12,2).reshape(3,2)
Out[8]: array([[ 1,  3],
       [ 5,  7],
       [ 9, 11]])
3.zeros() and ones() functions

Generate an array of all 0s and 1s of the specified size

Guess you like

Origin blog.csdn.net/A_No2Tang/article/details/114947201