Numpy from entry to proficiency - storing and reading matrices and reading data in matrices

This column is called " Numpy from entry to proficiency ". As the name suggests, it is to record the learning process of learning numpy, and it is also convenient for you to review later! Lay the foundation for further study in deep learning! I hope it can help everyone, and I wish you a happy life if you love to sleep! This article introduces " Numpy from entry to proficiency - storing and reading matrices and reading data in matrices "

insert image description here

1. Use savetxt and loadtxt to store and read the matrix

np also provides a function that can directly store a matrix into a txt file, and a function loadtxt that reads a txt file into a matrix. Below we will introduce these two functions in detail:

function illustrate
savetxt Store np.ndarray in txt file
loadtxt The matrix in the txt file is read into np.ndarray

Store the matrix through savetxt and read the matrix through loadtxt:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :numpy学习 
@File    :task_8.py
@IDE     :PyCharm 
@Author  :咋
@Date    :2023/4/17 16:45 
"""
import numpy as np
array_1 = np.random.random([5,5]) # 创建一个5x5的矩阵
print(array_1)
# 保存矩阵
np.savetxt(X=array_1,fname="test.txt")
array_2= np.loadtxt("test.txt")
print(array_2)

The output is:

[[0.55288088 0.77184344 0.8309328  0.55396012 0.62182447]
 [0.76018218 0.75927817 0.6210175  0.35243527 0.23502823]
 [0.80056504 0.33167949 0.21276266 0.28230738 0.93320109]
 [0.28639089 0.78890919 0.42325923 0.3815833  0.71751376]
 [0.72839721 0.45384038 0.4945789  0.36863601 0.21875081]]
[[0.55288088 0.77184344 0.8309328  0.55396012 0.62182447]
 [0.76018218 0.75927817 0.6210175  0.35243527 0.23502823]
 [0.80056504 0.33167949 0.21276266 0.28230738 0.93320109]
 [0.28639089 0.78890919 0.42325923 0.3815833  0.71751376]
 [0.72839721 0.45384038 0.4945789  0.36863601 0.21875081]]

The files in txt are:

insert image description here

It can be seen that np can successfully store the matrix in a txt file, and can also read the file in txt into the program.

2. Read matrix data with dimension 1

In the previous article, we mainly introduced the np generation matrix. After the matrix is ​​generated, how should we read the data we need? Next, through a piece of code, I will introduce several commonly used methods of obtaining data in ndarray:
Create a matrix with a dimension of 1 for later experiments:

@Project :numpy学习 
@File    :task_9.py
@IDE     :PyCharm 
@Author  :咋
@Date2023/4/17 17:04 
"""
import numpy as np
np.random.seed(2023)
array_1 = np.random.random([10])
print(array_1)

output:

[0.3219883  0.89042245 0.58805226 0.12659609 0.14134122 0.46789559
 0.02208966 0.72727471 0.52438734 0.54493524]

2.1 Get the data at the specified location

#获取指定位置的数据,获取第4个元素
print(array_1[3])

output:

0.12659609350429124

2.2 Intercept a piece of data

print(array_1[3:6])

output:

[0.12659609 0.14134122 0.46789559]

2.3 Take data at intervals

#截取固定间隔数据
print(array_1[1:6:2])

output:

[0.89042245 0.12659609 0.46789559]

2.4 Fetch in reverse order

#倒序取数
print(array_1[::-2])

output:

[0.54493524 0.72727471 0.46789559 0.12659609 0.89042245]

3. Read multidimensional matrix data

Let's take a matrix with a dimension of 2 as an example. Other high-dimensional matrices are similar. First, create a matrix with a dimension of 2 for later experiments:

array_2=np.arange(25).reshape([5, 5])
print(array_2)

output:

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]

3.1 Intercept data in a region of a multidimensional array

print(array_2[1:3, 1:3])

output:

[[ 6  7]
 [11 12]]

3.2 Intercept data in a multidimensional array whose value is within a value range

print(array_2[(array_2 > 3) & (array_2 < 10)])

output:

[4 5 6 7 8 9]

3.3 The specified line intercepts the multidimensional array

print(array_2[[1, 2]])

output:

[[ 5  6  7  8  9]
 [10 11 12 13 14]]

3.4 The specified column intercepts the multidimensional array

print(array_2[:, 1:3])

output:

[[ 1  2]
 [ 6  7]
 [11 12]
 [16 17]
 [21 22]]

This index is very similar to the range in python, consisting of start, end and step, note that end is not included .

Fourth, the choice function extracts data

In addition to using the above method to extract elements by index, we can also use the choice function to randomly extract data from the target matrix:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :numpy学习 
@File    :task_10.py
@IDE     :PyCharm 
@Author  :咋
@Date    :2023/4/17 17:29 
"""
import numpy as np
from numpy import random as nr
a = np.arange(1, 25, dtype=float)
c1 = nr.choice(a, size=(3, 4))  # size指定输出数组形状
c2 = nr.choice(a, size=(3, 4), replace=False)  # replace默认为True,即可重复抽取。
# 下式中参数p指定每个元素对应的抽取概率,默认为每个元素被抽取的概率相同。
c3 = nr.choice(a, size=(3, 4), p=a / np.sum(a))
print("随机可重复抽取")
print(c1)
print("随机但不重复抽取")
print(c2)
print("随机但按制度概率抽取")
print(c3)

output:

D:\anaconda\python.exe D:/桌面/numpy学习/task_10.py
300.0
随机可重复抽取
[[14. 22. 20. 11.]
 [ 6. 18. 15. 21.]
 [ 3. 19. 23.  3.]]
随机但不重复抽取
[[ 6. 17. 13. 24.]
 [ 5. 20. 19. 12.]
 [10.  2. 21. 23.]]
随机但按制度概率抽取
[[19. 18. 24. 11.]
 [ 5.  7. 11. 20.]
 [10. 15. 16. 20.]]

The first two are easy to explain. size is the size of the extracted matrix, and replace is whether the extraction can be repeated. Let's focus on the parameter p, which is the probability of extraction. Here p=a / np.sum(a), that is, the larger the value, the higher the probability of being drawn.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_63866037/article/details/130193378