当前位置:网站首页>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]
边栏推荐
- Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design
- 5. Discrete control and continuous control
- Micro rabbit gets a field of API interface JSON
- 4、策略学习
- 2022 examination for safety production management personnel of hazardous chemical production units and new version of examination questions for safety production management personnel of hazardous chem
- Guojingxin center "APEC education +" Shanghai Jiaotong University Japan Cooperation Center x Fudan philosophy class "Zhe Yi" 2022 New Year greetings
- C#中string用法
- Leetcode notes No.21
- 14. Draw network model structure
- Ag9310meq ag9310mfq angle two USB type C to HDMI audio and video data conversion function chips parameter difference and design circuit reference
猜你喜欢
Ag7120 and ag7220 explain the driving scheme of HDMI signal extension amplifier | ag7120 and ag7220 design HDMI signal extension amplifier circuit reference
2、TD+Learning
[deep learning] AI one click to change the sky
Y59. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (III, II)
2022 high altitude installation, maintenance and demolition examination materials and high altitude installation, maintenance and demolition operation certificate examination
14. Draw network model structure
Su embedded training - Day9
2021-03-06 - play with the application of reflection in the framework
Chapter XI feature selection
Parade ps8625 | replace ps8625 | EDP to LVDS screen adapter or screen drive board
随机推荐
Four digit nixie tube display multi digit timing
Chapter XI feature selection
Capstone/cs5210 chip | cs5210 design scheme | cs5210 design data
2. Nonlinear regression
2022 low voltage electrician examination content and low voltage electrician simulation examination question bank
USB type-C mobile phone projection scheme | USB type-C docking station scheme | TV / projector type-C converter scheme | ag9300ag9310ag9320
4. Cross entropy
The solution of frame dropping problem in gnuradio OFDM operation
Serial port receives a packet of data
Content of one frame
Talk about smart Park
How to write mark down on vscode
Ag9310 design USB type C to hdmi+u2+5v slow charging scheme design | ag9310 expansion dock scheme circuit | type-C dongle design data
[deep learning] AI one click to change the sky
Fundamentals - integrating third-party technology
5. Discrete control and continuous control
Recommend a document management tool mendely Reference Manager
8. Optimizer
6. Dropout application
4. Apprentissage stratégique