Numerical Python (Numerical Python) is an open-source numerical computing library for Python, which provides efficient multidimensional array objects and functions for processing arrays. This tutorial will introduce the basic concepts and common operations of numpy.
Install numpy:
Before you start, you need to install numpy first. Numpy can be installed via pip using the following command
pip install numpy
Import numpy:
Once the installation is complete, you can use the import statement to import numpy:
import numpy as np
Create arrays: With numpy, you can create multidimensional array objects. You can use the array() function to create an array from a list or tuple. For example:
import numpy as np
Create a one-dimensional array.
arr1=np.array([1,2,3,4,5])
print(arr1) output: [1 2 3 4 5].
Crawler IP acquisition;
Create a two-dimensional array.
arr2=np.array([[1,2,3],[4,5,6]])
print(arr2) output:
Array properties:
Numpy arrays have some commonly used properties such as shape, dimension, and size. You can use these properties to understand the structure of an array. For example:
import numpy as np
arr=np.array([[1,2,3],[4,5,6]])
print(arr.shape) output: (2,3).
print(arr.ndim) output: 2
print(arr.size) Output: 6
Array manipulation: Numpy provides a number of functions and methods for manipulating arrays. You can use these functions and methods to compute, slice, reshape, and so on on arrays. For example:
import numpy as np
arr=np.array([1,2,3,4,5])
Calculate the sum, maximum, and minimum values of the array.
print(np.sum(arr)) output: 15
print(np.max(arr)) output: 5
print(np.min(arr)) output: 1
Slicing operation.
print(arr[1:4]) output: [2 3 4].
Reshape the array.
arr2=arr.reshape((5,1))
print(arr2) output:
Numppy provides many functions for nump, such as addition, subtraction, multiplication, division, etc. These functions can perform element-by-element operations on arrays. For example:
import numpy as np
arr1=np.array([1,2,3])
arr2=np.array([4,5,6])
Addition. print(np.add(arr1,arr2)) output: [5 7 9].
Subtraction. print(np.subtract(arr1,arr2)) output: [-3-3-3].
Multiplication. print(np.multiply(arr1,arr2)) output: [4 10 18].
Division. print(np.divide(arr1,arr2)) output: [0.25 0.4 0.5]
This is just a small part of Numpy's features, there are many other features and methods that can be used for numerical calculations and data processing. With these basic concepts and operations mastered, you can begin to use numpy for more complex numerical calculations and data analysis. You can refer to Numpy's official documentation and other tutorials to learn Xi in depth.