SoFunction
Updated on 2024-11-20

python numpy query to locate rows and columns of assigned values

Filtering rows according to conditions (filtering)

Filter the rows of the matrix with a value of 5 in column 7

B = A[ A[:,6] == 5]

Filter the rows of the matrix whose 7th column is greater than 5

B = A[ A[:,6] > 5]

Numpy Basic Operations

Taking values based on row and column numbers (query)

Take the number in row 2, column 2

import numpy as np
# Produce a matrix with 3 rows and 4 columns
x=(0,12)
x=((3,4))
print(x)

y=x[1,1]
print(y)

Intercept the first few rows and columns

import numpy as np
# Produce a matrix with 3 rows and 4 columns
x=(0,12)
x=((3,4))
print(x)

#[a:b,c:d] means take rows a-b, columns c-d. a, c can be omitted, means start from 0
# Take the first two rows of x (all columns)
y=x[:2] # Equivalent to y=x[:2,:] Equivalent to y=x[0:2,...]
print(y)

# Take the first two columns of x (of all rows)
z=x[:,:2] # is equivalent to z=x[:,0:2], which is equivalent to z=x[... ,0:2]
print(z)

Intercepting certain rows and columns

import numpy as np
# Produce a matrix with 3 rows and 4 columns
x=(0,12)
x=((3,4))
print(x)

#[a:b,c:d] means take rows a-b, columns c-d. a, c can be omitted, means start from 0
# Take the 2nd row and 2nd-3rd columns of x.
y=x[1,1:3]

Determine the row and column numbers from the values (locate)

Output the index of the column where the maximum value of a row is located

import numpy as np
# Produce a matrix with 3 rows and 4 columns
x=(0,12)
x=((3,4))
print(x)

# Take the index corresponding to the maximum value of the element in x and search for the maximum value in the direction of a[1] in a[0][1], i.e., the row direction.
y = (x, 1)
print(y)

Output the row index where the maximum value of a column is located

# Fetch the line number corresponding to the maximum value of the element in x
y = (x, 0)
print(y)

The row and column number where the maximum value is located

x=(0,12)
x=((3,4))
print(x)

# where returns a tuple of length 2, the first element holds the row number, the second element holds the column number
y = (x == (x))
print(y)
print("Maximum value in row:",y[0],"Column with maximum value:",y[1])

Summing by row/column (summation)

Summing by rows, adding the sum to the last column of the matrix

x=(0,12)
x=((3,4))
print(x)

y = (x, axis=1)
print(y)

xy = ((x, ([0], -1)))
print(xy)

Summing by column, adding the sum to the last row of the matrix

x=(0,12)
x=((3,4))
print(x)

z = (x, axis=0)
print(z)

xz = ((x, z))
print(xz)

assign a value to something

conditional assignment

x=(0,12)
x=((3,4))
print(x)

x[x<=5]=0 # Assigning numbers less than 5 to 0
print(x)

where()

x=(0,12)
x=((3,4))
print(x)

# results = (condition, x, y)
# When the condition is true, the corresponding position returns the value in x. If the condition is not true, the value in y is returned.
y = (x>5,x,0)  #Values that satisfy greater than 5 are set to x and those that do not are set to 0
print(y)

logical operation

x=(0,12)
x=((3,4))
print(x)

print(x>5) # x>5traveler's dutyTure,or elseFalse

Assigns a value to the specified row/column/[row, column].

x=(0,12)
x=((3,4))
print(x)

x[1,1] = 999 # Assignment of specified row and column numbers
print(x)

x[1] = 123 # Assigning a value to a specific line
print(x)

x[:,1] = 321 # Assign values to specified columns
print(x)

to this article on the python numpy query to locate the assignment of numerical values where the article is introduced to this, more related python numpy content please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!