## Happy journey of simple Python: numpy topic of Python basic syntax

Defonds 2020-11-13 04:49:16
happy journey simple python numpy

In the use of 2D Array or multidimensional array ,Python Of Numpy The library is very useful . such as , In the work of image processing , You have to store the pixel values in a two-dimensional or three-dimensional array .
Python Only one dimensional arrays are supported . It doesn't support multidimensional arrays .Numpy It's right Python An extension of array , It not only supports multidimensional arrays , It also provides mathematical operations based on multidimensional arrays .

# 1. Pycharm Import Numpy modular

Writing Numpy Before the program ,Pycharm You need to import Numpy modular , Otherwise, you may encounter No module named ‘numpy’ Error of . Please refer to the blog for details 《PyCharm To write Numpy Program times No module named ‘numpy‘ Wrong solution 》.

# 2. Python Numpy brief introduction

By default ,Python There is no concept of arrays , And there is no built-in support for multidimensional arrays .
Python Numpy Is an easy library for handling multidimensional arrays . It has a rich set of functions , You can easily handle arrays . Especially with Python Increased use in data analysis and scientific projects ,numpy Has become a processing array Python An integral part of .
Python Numpy Is an easy library for handling multidimensional arrays . It has a rich set of functions, which can easily handle arrays . Especially with Python Increased use in data analysis and scientific projects ,numpy Has become a processing array Python An integral part of .
Next we'll take a series of Numpy Examples to help you understand the use of numpy Kuhe Python programing language .

# 3. Numpy - Create a one-dimensional array

seeing the name of a thing one thinks of its function , A one-dimensional array contains only one-dimensional elements . Or say ,Numpy Arrays are shaped as tuples containing only one value .
To create a one-dimensional array , You can use Numpy Of array()、arange() or linspace() Any function of .

## 3.1. Use array() Function creation 1D Numpy Array

Numpy array() The function takes the element parameters of a list and returns a one-dimensional array .
In the following example, we will introduce numpy Library and use array() Function to create a one-dimensional array .

``````import numpy as np
# create numpy array
a = np.array([5, 8, 12])
print(a)
``````

Execution and output ： ## 3.2. Use arange() Function creation 1D Numpy Array

arange() Function takes parameters start、end As the value range and take the parameter interval To create a one-dimensional step numpy Array .
[start, start+interval, start+2*interval, … ]
Next we introduce numpy Library and use arange() Function to create a one-dimensional numpy Array .

``````import numpy as np
# create numpy array
a = np.arange(5, 14, 2)
print(a)
``````

Execution and output ： so , The array starts with 5, With 2 Step length , until 14 until .

## 3.3. Use linspace() Function creation 1D Numpy Array

Numpy Of linspace() Function takes parameters start、end As the beginning of an array 、 Final element , And with parameters number Create a one-dimensional, evenly spaced array of numbers as the total number of elements to create the array .
Next we introduce numpy Library and use linspace() Function to create a one-dimensional numpy Array .

``````import numpy as np
# create numpy array
a = np.linspace(5, 25, 4)
print(a)
``````

Execution and output ： ## 3.4. Summary

In this section , We learned from a simple, detailed example of using different built-in functions to create a numpy One dimensional array .

# 4. Create an array of random values

## 4.1. numpy in Of shape

shape Included in numpy library , It's an attribute of a matrix , You can get the shape of the matrix , The result is a tuple .
To create a specific... With random values shape Of numpy Array , Use numpy.random.rand(), And put the array of shape Pass as a parameter .
In this section , We'll learn about creating a random value of numpy An example of an array .

## 4.2. numpy.random.rand() The grammar of

rand() The syntax of the function is as follows ：

``````numpy.random.rand(d0,d1,d2,...,dN)
``````

among ,d0、d1、d2、… Is the size of each dimension in the array .
such as ,numpy.random.rand(2,4) It means a shape 2x4 Two dimensional array of , and numpy.random.rand(51,4,8,3) It means a shape by 51x4x8x3 The thinking array of .
so , This function returns a specified shape And use 0 To 1 Between random floating point numbers numpy Array .

## 4.3. Create a one-dimensional random value numpy Array

To create a one-dimensional random value numpy Array , Just pass the array size to rand() Function .
In this example , We will create a length of 7 One dimensional random value array of .

``````import numpy as np
# numpy array with random values
a = np.random.rand(7)
print(a)
``````

Execution and output ： ## 4.4. Create a two-dimensional random value numpy Array

To create a two-dimensional random value numpy Array , Just pass the size of the two dimensions to rand() Function .
In this example , We're going to create one dimension-0 The length is 2,dimension-1 The length is 4 Two dimensional random value array of .

``````import numpy as np
# numpy array with random values
a = np.random.rand(2, 4)
print(a)
``````

Execution and output ： ## 4.5. Create a three-dimensional random value numpy Array

To create a three-dimensional random value numpy Array , Just pass the respective sizes of the three dimensions to rand() Function .
In this example , We will create a dimension with a length of 4、2、3 Three dimensional random value array of .

``````import numpy as np
# numpy array with random values
a = np.random.rand(4, 2, 3)
print(a)
``````

Execution and output ： ## 4.6. Summary

In the example in this section , We learned how to use numpy.random.rand() To create arrays of random values of different dimensions .

# 5. take Numpy The array is stored in a file & Read from file Numpy Array

You can use numpy.save() take numpy The array is stored in a file , It can also be used at a later time numpy.load() Load the contents of the file into numpy Array .
Here is a code snippet to illustrate , First we use save() Function to write an array to a file , Then we use load() Function to load the file into a numpy Array .

``````# save array to file
numpy.save(file, array)
``````

## 5.1. Save array to file

In the next example , We will initialize an array , And then to write binary Pattern creates and opens a file , And finally we use numpy.save() Method to write the array to a file .

``````import numpy as np
# initialize an array
arr = np.array([[[11,11,9,9],[11,0,2,0]],
[[10,14,9,14],[0,1,11,11]]])
# open a binary file in write mode
file = open("arr", "wb")
# save array to the file
np.save(file, arr)
# close the file
file.close()
``````

Execution and output ： View the current program working directory ( The project directory ) It's called arr File generation for ： Please note that after saving the array to the file , Don't forget to close the file .

## 5.2. Load from file numpy Array

In this example , We're going to load an array from the file . We will save the array to a file based on the above , Continue to read the array from the file .

``````import numpy as np
# initialize an array
arr = np.array([[[11,11,9,9],[11,0,2,0]],
[[10,14,9,14],[0,1,11,11]]])
# open a binary file in write mode
file = open("arr", "wb")
# save array to the file
np.save(file, arr)
# close the file
file.close()
# open the file in read binary mode
file = open("arr", "rb")
# read the file to numpy array
print(arr1)
# close the file
file.close()
``````

Execution and output ： You can see , We have successfully read from the file numpy Array and use this array to generate an object .

## 5.3. Summary

In this section , We learned how to put a numpy Array saved to file , And how to load from a file numpy Array into an object in the program .