Python numerical calculation ------ the creation and operation of arrays

The array is created by the array function in the numpy module. The elements that make up the array are all homogeneous, that is, each value in the array has the same data type.

Array creation
# 导入模块,并重命名为np
import numpy as np
# 单个列表创建一维数组
arr1 = np.array([3,10,8,7,34,11,28,72])
# 嵌套元组创建二维数组
arr2 = np.array(((8.5,6,4.1,2,0.7),(1.5,3,5.4,7.3,9),(3.2,3,3.8,3,3),(11.2,13.4,15.6,17.8,19)))
print('一维数组:\n',arr1)
print('二维数组:\n',arr2)

In the second array, the input elements contain two data types: integer and floating-point, but all array elements become floating-point when outputting. Next, learn how to get array elements:

# 一维数组元素的获取
print(arr1[[2,3,5,7]])

# 二维数组元素的获取
# 第2行第3列元素
print(arr2[1,2])
# 第3行所有元素
print(arr2[2,:])
# 第2列所有元素
print(arr2[:,1])
# 第2至4行,2至5行
print(arr2[1:4,1:5])

The above methods are used to fetch continuous rows or columns. Next, we will introduce examples of discontinuous fetching:

# 第一行、最后一行和第二列、第四列构成的数组
print(arr2[np.ix_([0,-1],[1,3])])
# 第一行、最后一行和第一列、第三列、第四列构成的数组
print(arr2[np.ix_([0,-1],[1,2,3])])
Common attributes of arrays

We often don't know the attributes of the data read from the outside. In the numpy module, we can read the data of external text files by following the genfromtxt function. The text files here are mainly csv files and txt files.

np.genfromtxt(fname,dtype=<class 'float'>,comments='#',delimiter=None,skip_header=0,skip_footer=0,converters=None,missing_values=None,filling_values=None,usecols=None,names=None,)

fname: Specify the file path of the data to be read.
dtype: Specify the data type of the data to be read. The default is floating point. If the original data set contains character data, the data type must be specified as "str".
comments: Specify the comment character, the default is #, if the original data line starts with #, the reading of these lines will be ignored.
delimiter: Specifies the column separator of the data set
skip_header: whether to skip the first row of the data set,
skip_footer: whether to skip the footnotes of the data set, default not to skip
converters: convert the data of the specified column into other values
miss_values : Specify the mark of the exact value. If the specified mark is contained in the original data set, such data will be a missing value after being read in.
filling_values: specify the filling value of missing values
usecols: specify which columns need to be read in
names: set the column names for the columns that read in data

# 读入数据
stu_score = np.genfromtxt(fname = r'stu_socre.txt',delimiter='\t',skip_header=1)
# 查看数据结构
print(type(stu_score))
# 查看数据维数
print(stu_score.ndim)
# 查看数据行列数
print(stu_score.shape)
# 查看数组元素的数据类型
print(stu_score.dtype)
# 查看数组元素个数
print(stu_score.size)
Array shape processing
arr3 = np.array([[1,5,7],[3,6,1],[2,4,8],[5,8,9],[1,5,9],[8,5,2]])
# 数组的行列数
print(arr3.shape)
# 使用reshape方法更改数组的形状
print(arr3.reshape(2,9))
# 打印数组arr3的行列数
print(arr3.shape)

# 使用resize方法更改数组的形状
print(arr3.resize(2,9))
# 打印数组arr3的行列数
print(arr3.shape)

Although both reshape and resize are methods used to change the shape of the array, the reshape method only returns a preview of the changed shape, but does not really change the shape of the array arr3; the resize method does not return the preview, but directly changes the shape of arr3 , And then learn dimensionality reduction:

arr4 = np.array([[1,10,100],[2,20,200],[3,30,300]])
print('原数组:\n',arr4)
# 默认排序降维
print('数组降维:\n',arr4.ravel())
print(arr4.flatten())
print(arr4.reshape(-1))
# 改变排序模式的降维
print(arr4.ravel(order = 'F'))
print(arr4.flatten(order = 'F'))
print(arr4.reshape(-1, order = 'F'))

Next, learn the differences between the above three methods:

# 更改预览值
arr4.flatten()[0] = 2000
print('flatten方法:\n',arr4)
arr4.ravel()[1] = 1000
print('ravel方法:\n',arr4)
arr4.reshape(-1)[2] = 3000
print('reshape方法:\n',arr4)

As a result
Insert picture description here
, the dimensionality reduction achieved by the flatten method returns a copy, because the modification of the dimensionality reduction element does not affect the result of the original array arr4. On the contrary, the ravel method and the reshape method return the view. By changing the view, Will affect the original array arr4.

arr5 = np.array([1,2,3])
print('vstack纵向合并数组:\n',np.vstack([arr4,arr5]))
print('row_stack纵向合并数组:\n',np.row_stack([arr4,arr5]))
arr6 = np.array([[5],[15],[25]])
print('hstack横向合并数组:\n',np.hstack([arr4,arr6]))
print('column_stack横向合并数组:\n',np.column_stack([arr4,arr6]))

Vertical stacking must ensure that the number of columns in each array is the same, and horizontal stacking must ensure that the number of rows in each array is the same.

Guess you like

Origin blog.csdn.net/m0_46445293/article/details/115002811
Recommended