当前位置:网站首页>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]
边栏推荐
- Su embedded training - Day7
- 2022 free test questions of fusion welding and thermal cutting and summary of fusion welding and thermal cutting examination
- Fundamentals - integrating third-party technology
- 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
- redis的持久化方式-RDB和AOF 两种持久化机制
- 2022 high altitude installation, maintenance and demolition examination materials and high altitude installation, maintenance and demolition operation certificate examination
- Apt get error
- 2021-03-14 - play with generics
- Frrouting BGP protocol learning
- Su embedded training - Day8
猜你喜欢
4. Strategic Learning
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
Vs code configuration latex environment nanny level configuration tutorial (dual system)
Know how to get the traffic password
Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design
Design method and application of ag9311maq and ag9311mcq in USB type-C docking station or converter
Macro definition and multiple parameters
Talk about smart Park
Common effects of line chart
2021 tea master (primary) examination materials and tea master (primary) simulation test questions
随机推荐
Introduction to the types and repair methods of chip Eco
2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
Su embedded training - Day8
Application of state mode in JSF source code
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
Micro rabbit gets a field of API interface JSON
On the concept and application of filtering in radar signal processing
USB type-C mobile phone projection scheme | USB type-C docking station scheme | TV / projector type-C converter scheme | ag9300ag9310ag9320
General configuration tooltip
基础篇——整合第三方技术
swift获取url参数
利用GPU训练网络模型
2022 free test questions of fusion welding and thermal cutting and summary of fusion welding and thermal cutting examination
133. 克隆图
Macro definition and multiple parameters
The solution of frame dropping problem in gnuradio OFDM operation
Mathematical modeling -- knowledge map
Chapter VIII integrated learning
How to get the first and last days of a given month
2021 tea master (primary) examination materials and tea master (primary) simulation test questions