The basic usage of Numpy (array, array operation, calculation of array and number, calculation of array and array, fancy index, slice, Boolean, exchange of row and column, copy) has examples

Functions that will appear in this article: array(), arange(), random(), randint(), zeros(), ones(), full(), eye(), reshape(), round(), T, transpose ()

Hi everyone, I’m Xiaobai, and I’m learning about data analysis at this stage. I gained some insights during the lecture and summarized a little note. I want to share it with you and discuss it with everyone. You can use it as a Just take a look at the information, if you feel that something is vague or not accurate enough, you can @我呵 in the comment area, hehe~

1. Usage of numpy

1.1Numpy is a python scientific computing library for fast processing of arrays of any dimension.

1.2Numpy provides an N-dimensional array type ndarray, which describes the same type of 'items' collection.

1.3 numpy.ndarray supports vectorized operations.

2. The array in numpy is different from the list in python

2.1 A list can store multiple data types, while an array can only store the same data type.

2.2 Arrays can be multi-dimensional. When all the data in a multi-dimensional array is of numeric type, it is equivalent to a matrix in linear algebra, and mutual operations can be performed.

3. Code usage

(1). Array

1. Library import

import numpy as np

2.array()

Generate a one-dimensional array of 1--4

a1=np.array([1,2,3,4])
a1

3.arange()

Generate a one-dimensional array from 2 to 22, with a step size of 2 (including the left and excluding the right)

a2=np.arange(2,22,2)
a2

 4. Generate random numbers: random()

Generate a two-dimensional random array with two rows and two columns

a3=np.random.random((2,2))    #两行两列,随机数
a3

Generate a two-dimensional random array of three rows and three columns (data between 0--10)

a4=np.random.randint(0,10,size=(3,3))#元素是从0--10的随机数,三行三列
a4

 

5.zeros()

Generate a two-dimensional array with two rows and two columns of 0

a5=np.zeros((2,2))     #两行两列都是0
a5

6. ones()

Generate a two-dimensional array with two rows and two columns of 1

a6=np.ones((2,2))    #两行两列都是1
a6

7.full()

Generate a two-dimensional array with two rows and two columns of other numbers

a7=np.full((2,2),6)  #两行两列都是6
a7

8.eye()

Generate a two-dimensional array of three rows and three columns with all 1s on the upper slope and 0s on the other elements

a8=np.eye(3)    #生成的是斜上方都是1,其他元素为0的3*3的矩阵
a8

(2). Array operation

1.reshape(): cutting

Transform array dimensions

data=np.arange(12).reshape(3,4)     #把一维数组变成三行四列的二维数组
data

2. it's me

View array dimensions

data.ndim

3. shape

View the array as several rows and columns

data.shape

 4.size

View the number of array elements

data.size

 5.dtype

View array type

data.dtype

(3) Calculation of arrays and numbers

1. Enlarge an array by a certain time

a3=np.random.random((2,2))    #两行两列,随机数
aa=a3*10        #给a3数组的元素乘以10
aa

 2.round()

Give arrays three decimal places

aaa=aa.round(3)     #保留小数点
aaa

Let's write this article here for the time being, and see you in the next article! bye~

If you think it is easy to use, please like, comment and collect, good people have a safe life! ! !

Guess you like

Origin blog.csdn.net/m0_50962679/article/details/123601000