And a brief description of different ways to create an array: Shredded numpy (a)

↑  focus + star - interesting not like technology No.

Nine at night, we meet on time  


Hello everyone, I am a student yellow

Recently updated basics for everyone to wave python, this time bring the Shredded numpy series.

1. Introduction of numpy

numpy is "Numerical Python" for short.

numpy provides a high-performance multi-dimensional array object ndarray (N Dimension Array), and a number of library functions and operations, can help programmers numerical calculation easily.

Native language python performs slowly, C executed 500 times, almost python can only be executed once, but python syntax is much more simpler than other programming languages. So we not only want to execute a particular programming language fast, but also simple to program. The final python by integrating C and C ++, the ultimate solution to this problem, that is to say: the bottom are running C and C ++ code, but the top is used to write the python language. This is the reason why we like to use "numpy Library".

As many numpy used for data mining, data analysis, artificial intelligence related to the underlying technology component implemented. Like SciPy, Matplotlib, Scikit-learn some extent, need to rely on numpy.

2, learning the routines numpy

Learn how to use numpy organizational data (how to create, you want different dimensions, the array of different shapes): numpy provides a high-performance multi-dimensional array object: ndarray.

Numpy learning function to deal with this group provided good data: numpy provides a lot of library functions that help us deal with these data.

3, description of the structure of data objects numpy in ndarray

numpy most important data structures are referred to as n-dimensional array ndarray the object, the object consists of two parts:

  • Metadata part: description of some of the stored information of the current object ndarray;

  • Real data section: storage is the current object of this ndarray real data.

1) What is the description of ndarray array of objects it?

Note: The above figures shape, size, dtype, ndim, etc. are the x ndarray object description information, this information is stored in a metadata area. The real data is 1,2,3,4,5,6 this object.

4, a simple comparison ndarray arrays and list files

① ndarray in an array or list data types

list file can be stored in different data types, such as: x = [1,2.3, True, "China"]. All of the elements ndarray stored in the array must be consistent.

② use numpy to create and use an array of native Efficiency Comparison list

③ create an array of benefits of using ndarray

Since ndarray, the type of each element since it is consistent, then you only need a whole ndaray metadata information on it, rather than like a list, each object needs to store a metadata information.

ndarray advantage is: 1, due to metadata only need to store a copy, so you can save more space. 2, since each element of the same type, as evidenced by the size of the memory for each element is the same, then such data can be stored in a more compact, more efficient operation.

5. What are the dimensions?

An example will be described ①

② a figure illustrates ndarray written one-dimensional array, two-dimensional arrays, three dimensional arrays

③ are exemplified for the above-described knowledge

6, create an array of different ways

1) using the array () function to create an array;

Follows

import numpy as np

array1 = [1,2,3]
m = np.array(array1)
display(m)

array2 = [[1,2,3],[4,5,6]]
n = np.array(array2)
display(n)

The results are as follows:

conclusion as below:

  • np.array (parameters) function, the data parameters to what style, what you build ndarray array of styles; you give me a one-dimensional list, I would build a one-dimensional array; you give me a two-dimensional list, I Construction of a two-dimensional array;

  • What is a two-dimensional list? List each element is a one-dimensional list, it is a two-dimensional list;

  • If I built a two-dimensional list, then the two-dimensional list each element is just a one-dimensional list;

  • In numpy, a one-dimensional array is also called "vectors"; two-dimensional array is also called "matrix";

2) using the arange () function to create an array: contrast range of the list () function learning;

① the same point: the usage is the same

# 语法如下:
range(start,end,step)
arange(start,end,step)
# 举例如下:
list1 = list(range(1,10,2))
display(list1)
array1 = np.arange(1,10,2)
display(array1)

The results are as follows:

② different: arange () function step, can be floating, but the range () function step size, can not float

list1 = list(range(1,10,2))
display(list1)
list2 = list(range(1,10,0.5))
display(list2)
list3 = list(range(10,1,-2))
display(list3)

array1 = np.arange(1,10,2)
display(array1)
array2 = np.arange(1,10,0.5)
display(array2)
array3 = np.arange(10,1,-2)
display(array4)

The results are as follows:

3) with the specified value to generate an array of a predetermined shape;

① commonly used functions as follows

  • np.zeros ((x, y)): generate a column y x row, a two-dimensional array of elements are 0;

  • np.ones ((x, y)): generate a column y x row, a two-dimensional array of elements are;

  • np.full ((x, y), value): generating a column y x row, a two-dimensional array of elements are of value, wherein this value may be an integer value (a positive integer, 0, negative integer) or decimal;

② code is as follows

array1 = np.zeros((3,4))
display(array1)

array2 = np.ones((3,4))
display(array2)

array1 = np.full((3,4),1.2)
display(array3)

The results are as follows:

Note: I am here to create a two-dimensional array, for example, you pass a number, you can create one-dimensional arrays; you pass three figures, you can create a three-dimensional array, you can go on your own try.

4) The shape of the existing ndarray array, creating identical but the shape of the specified array element ndarray;

① commonly used functions as follows

  • np.zeros_like()

  • np.ones_like()

  • np.full_like()

② operation is as follows

## 1)先创建一个一维数组和一个二维数组;
n1 = np.array([1,2,3]);
n2 = np.array([[1,2,3],[4,5,6]])

## 2)使用np.zeros_like()
array1 = np.zeros_like(n1)
array2 = np.zeros_like(n2)
display(array1)
display(array2)

## 3)np.ones_like()
array3 = np.ones_like(n1)
array4 = np.ones_like(n2)
display(array3)
display(array4)

## 4)np.full_like()
array5 = np.full_like(n1)
array6 = np.full_like(n2)
display(array5)
display(array6)

The results are as follows:

5) Create a matrix and a diagonal matrix;

① create a matrix

## 1)np.eye(x)和np.identity(x)
array1 = np.eye(3)
display(array1)

array2 = np.identity(4)
display(array2)

The results are as follows:

② create a diagonal matrix

## 1)np.diag()
array1 = np.diag([10,20,30])
display(array1)

The results are as follows:

6) Creating arithmetic array columns: Comparison arange () function and linspace () function learning;

① np.arange () and np.linspace () to create a difference arithmetic progression

  • np.arange (start, stop, step) The third parameter refers to a step element;

  • np.linspace (start, stop, num) the third parameter refers to the number of elements;

  • np.arange () takes a value that is less than the maximum stop, np.linspace () can take the maximum value by default STOP;

② used as follows

array1 = np.arange(1,15,3)
display(array1)
display(array1.dtype)

array2 = np.linspace(1,15,3)
display(array2)
display(array2.dtype)

The results are as follows:

③ np.linspace () are two commonly used parameters: endpoint and dtype

  • comprising represents endpoint = True end value (default), endpoint = False value indicating that no termination;

  • Dtype = np.float64 specified data type to create an array of default is float64, you can also set dtype = np.np.int32;

array3 = np.linspace(1,20,num=5,endpoint=False,dtype=np.float64)
display(array3)

array4 = np.linspace(1,20,num=5,endpoint=False,dtype=np.int32)
display(array4)

array5 = np.linspace(1,20,num=5,endpoint=True,dtype=np.float64)
display(array5)

array6 = np.linspace(1,20,num=5,endpoint=True,dtype=np.int32)
display(array6)

The results are as follows:

7) Create a geometric series array;

An example will be described ①

log_array = np.logspace(1, 7, num=4, endpoint=True, base=2)
display(log_array)

The results are as follows:

Note: The above code represented between the primary side to side 2 2 seven, the number of generation 4, which constitutes a further four digits geometric series.

② look at another case

log_array1 = np.logspace(1, 5, 3)
display(log_array1)

The results are as follows:

Note: The above code represents the default base = 10, that is, between the first side 10 of the quintic 10 generates three numbers, the number of which 3 also constitutes a geometric sequence.

8) by creating an array of custom functions;

## 1)np.fromfunction()
## 定义一个函数
def f(x, y):
    return x * 2 + 1 + y

## 从一个函数生成一个二维数组;
b = np.fromfunction(f, (3,3), dtype=np.int32)
display(b)

## 在这个函数,f函数要接收的参数,就是当前元素的坐标;
## 0行0列,传入的就是x=0,y=0;
## 0行1列,传入的就是x=0,y=1;
## 3行2列,传入的就是x=3,y=2;

The results are as follows:

numpy series continuously updated in ~

If you feel article helpful to you, welcome to scan two-dimensional code yellow concern students CSDN blog

A recent article, click on the picture to see

Keywords reply back " into the group ", immediately joined the Readers group ~

Fives

numpy series continuously updated in ~

ZHU Xiao five

Published 42 original articles · won praise 373 · views 30000 +

Guess you like

Origin blog.csdn.net/zhuxiao5/article/details/104890566
Recommended