Numpy Array Creation Tips and Application Guide
Numpy is a powerful library in Python, widely used in scientific computing, data analysis, machine learning and other fields. In Numpy, the most basic data structure is a multidimensional array, also called an ndarray. This article will introduce some techniques for creating Numpy arrays and provide specific code examples to help readers better understand and apply Numpy arrays.
1. Creation of Numpy arrays
The simplest way to create Numpy arrays is to use Python lists. A list can be converted to a Numpy array by passing it to the numpy.array()
function.
import numpy as np # 创建一维数组 arr1 = np.array([1, 2, 3, 4, 5]) print(arr1) # 输出:[1 2 3 4 5] # 创建二维数组 arr2 = np.array([[1, 2, 3], [4, 5, 6]]) print(arr2) # 输出: # [[1 2 3] # [4 5 6]]
Numpy provides a series of range functions that can easily create Numpy arrays with specific ranges and intervals.
import numpy as np # 创建一维等差数列数组 arr3 = np.arange(0, 10, 2) print(arr3) # 输出:[0 2 4 6 8] # 创建一维等间隔数列数组 arr4 = np.linspace(0, 1, 5) print(arr4) # 输出:[0. 0.25 0.5 0.75 1. ]
When you need to create a Numpy array with random numbers, you can use Numpy's random function.
import numpy as np # 创建具有随机整数的一维数组 arr5 = np.random.randint(0, 10, 5) print(arr5) # 输出:[8 6 3 9 1] # 创建具有随机浮点数的二维数组 arr6 = np.random.rand(2, 3) print(arr6) # 输出: # [[0.61723063 0.25061847 0.76613935] # [0.96519743 0.45027448 0.62479021]]
2. Application of Numpy array
Numpy array provides several functions for adjusting the shape of the array, including transformation Operations such as array dimensions, transposing arrays, and reshaping arrays.
import numpy as np # 变换数组形状 arr7 = np.arange(12).reshape(3, 4) print(arr7) # 输出: # [[ 0 1 2 3] # [ 4 5 6 7] # [ 8 9 10 11]] # 转置数组 arr8 = arr7.T print(arr8) # 输出: # [[ 0 4 8] # [ 1 5 9] # [ 2 6 10] # [ 3 7 11]] # 重塑数组形状 arr9 = np.arange(12).reshape(2, 2, 3) print(arr9) # 输出: # [[[ 0 1 2] # [ 3 4 5]] # [[ 6 7 8] # [ 9 10 11]]]
Numpy array supports operations on array elements one by one, such as positional access, slicing, dimensionality reduction, splicing, etc.
import numpy as np # 访问单个数组元素 arr10 = np.array([1, 2, 3, 4, 5]) print(arr10[2]) # 输出:3 # 对数组进行切片操作 arr11 = np.array([1, 2, 3, 4, 5]) print(arr11[1:4]) # 输出:[2 3 4] # 降维数组 arr12 = np.array([[1, 2, 3], [4, 5, 6]]) print(arr12.flatten()) # 输出:[1 2 3 4 5 6] # 数组拼接 arr13 = np.array([1, 2, 3]) arr14 = np.array([4, 5, 6]) print(np.concatenate((arr13, arr14))) # 输出:[1 2 3 4 5 6]
The above are just some tips and application examples for Numpy array creation. There are more operations and functions to choose from in actual applications. Proficiency in the creation and operation of Numpy arrays will be of great benefit to data processing and analysis tasks. I hope that the introduction of this article can provide readers with some help and guidance.
Summary:
The above is the detailed content of Numpy array creation techniques and practical guides for application. For more information, please follow other related articles on the PHP Chinese website!