当前位置:网站首页>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 ======== #
364、 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]]
75、 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
27、 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]
边栏推荐
- 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
- Study notes of single chip microcomputer and embedded system
- Getting started STM32 -- how to learn stm32
- 2022 high altitude installation, maintenance and demolition examination materials and high altitude installation, maintenance and demolition operation certificate examination
- Redis 主从复制
- 【深度学习】AI一键换天
- Gnuradio transmits video and displays it in real time using VLC
- Su embedded training - Day7
- Introduction to the types and repair methods of chip Eco
- 基础篇——整合第三方技术
猜你喜欢
![[loss function] entropy / relative entropy / cross entropy](/img/bc/574a4745336b0baf1a4ca53af41a82.jpg)
[loss function] entropy / relative entropy / cross entropy

Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation

Gnuradio 3.9 using OOT custom module problem record

The solution of frame dropping problem in gnuradio OFDM operation

2021-04-12 - new features lambda expression and function functional interface programming
![[deep learning] AI one click to change the sky](/img/74/f2e854b9f24129bcd9376733c2369f.png)
[deep learning] AI one click to change the sky

Ag9310 for type-C docking station scheme circuit design method | ag9310 for type-C audio and video converter scheme circuit design reference

Basic realization of line chart (II)

Getting started STM32 -- how to learn stm32

Redis 主从复制
随机推荐
133. Clone map
Know how to get the traffic password
Frrouting BGP protocol learning
High quality USB sound card / audio chip sss1700 | sss1700 design 96 kHz 24 bit sampling rate USB headset microphone scheme | sss1700 Chinese design scheme explanation
C#中string用法
How to get the first and last days of a given month
Frequency probability and Bayesian probability
10. CNN applied to handwritten digit recognition
50Mhz产生时间
Codeforces Round #804 (Div. 2)
Using GPU to train network model
2022 safety officer-c certificate examination summary and safety officer-c certificate reexamination examination
3. MNIST dataset classification
8. Optimizer
完整的模型训练套路
Use "recombined netlist" to automatically activate eco "APR netlist"
Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation
2022 R1 fast opening pressure vessel operation test question bank and R1 fast opening pressure vessel operation free test questions
Scheme selection and scheme design of multifunctional docking station for type C to VGA HDMI audio and video launched by ange in Taiwan | scheme selection and scheme explanation of usb-c to VGA HDMI c
2021-04-12 - new features lambda expression and function functional interface programming