当前位置:网站首页>Common operations of numpy on two-dimensional array
Common operations of numpy on two-dimensional array
2022-07-08 01:23:00 【You roll, I don't roll】
Catalog
1、 Extract some columns or rows of a two-dimensional array
4、 Calculate the number of non-zero elements in the array
5、 Extract certain rows or columns using Boolean masks
6、 Get the number of rows or columns of the array
7、 Get the last column ( Or yes ) The elements of
1、 Extract some columns or rows of a two-dimensional array
import numpy as np
# Definition 3*3 Of numpy Array
matrix = np.array([[1, 3, 2],
[8, 0, 6],
[9, 7, 0]])
# To extract the first 1、3 Column ( The extraction of rows is the same )
matrix1 = matrix[:, [0, 2]]
print(matrix1)
# ========== result ========== #
[[1 2]
[8 6]
[9 0]]
2、 Get a range of data
# For the first 1、2 In line 1、3 Columns of data . Be careful 0:2 Represents the interval of left closed and right open
matrix1 = matrix[0:2, [0, 2]]
print(matrix1)
# ======== result ======== #
[[1 2]
[8 6]]
3、 Sum all the elements
# Yes matrix Array sum
num = matrix.sum()
print(num)
# ======== result ======== #
36
4、 Calculate the number of non-zero elements in the array
# Determine whether the element in each position is non-zero
matrix2 = (matrix != 0)
# Sum Boolean matrices
N0_num = matrix2.sum()
print(matrix2)
print(N0_num)
# ======== result ======== #
[[ True True True]
[ True False True]
[ True True False]]
7
5、 Extract certain rows or columns using Boolean masks
# The goal is to extract the 1、3 Column ( Or yes ), Its size Must correspond to the number of columns ( Or number of rows ) identical
bool_mask = [True, False, True]
# Use bool_mask Extract the corresponding column ( The same is true for withdrawal lines )
matrix3 = matrix[:, bool_mask]
print(matrix3)
# ======== result ======== #
[[1 2]
[8 6]
[9 0]]
6、 Get the number of rows or columns of the array
# obtain matrix1 Dimensions , The result is in the form of tuples
size = matrix1.shape
# Get the number of lines
row = size[0]
# Get the number of columns
col = size[1]
print(size)
print(row)
print(col)
# ======== result ======== #
(3, 2)
3
2
7、 Get the last column ( Or yes ) The elements of
# obtain matrix The last column of elements , The result is a one-dimensional array . Get the same reason of the last line
end_col = matrix[:, -1]
print(end_col)
# ======== result ======== #
[2 6 0]
边栏推荐
- 133. Clone map
- y59.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三二)
- 5. Contrôle discret et contrôle continu
- Chapter IV decision tree
- 10. CNN applied to handwritten digit recognition
- Chapter improvement of clock -- multi-purpose signal modulation generation system based on ambient optical signal detection and custom signal rules
- General configuration tooltip
- Basic realization of line chart (II)
- Several frequently used OCR document scanning tools | no watermark | avoid IQ tax
- 50Mhz产生时间
猜你喜欢
解决报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
Know how to get the traffic password
Blue Bridge Cup embedded (F103) -1 STM32 clock operation and led operation method
14. Draw network model structure
Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation
The combination of relay and led small night light realizes the control of small night light cycle on and off
2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
4、策略学习
Basic realization of line graph
Running OFDM in gnuradio_ RX error: gr:: Log: info: packet_ headerparser_ b0 - Detected an invalid packet at item ××
随机推荐
General configuration toolbox
基础篇——整合第三方技术
Led serial communication
4. Strategic Learning
130. 被圍繞的區域
Study notes of single chip microcomputer and embedded system
4、策略學習
Cs5261type-c to HDMI alternative ag9310 | ag9310 alternative
Generic configuration legend
Su embedded training - Day8
Micro rabbit gets a field of API interface JSON
2021 Shanghai safety officer C certificate examination registration and analysis of Shanghai safety officer C certificate search
The examination contents of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge) in 2021 and the free examination questions of the third batch of Guangdong Prov
解决报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
C# ?,?.,?? .....
Ag9311maq design 100W USB type C docking station data | ag9311maq is used for 100W USB type C to HDMI with PD fast charging +u3+sd/cf docking station scheme description
跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
Content of one frame
Apt get error
Chapter VIII integrated learning