From binary data to integers python [duplicate] - python

This question already has answers here:
How to get the index of a maximum element in a NumPy array along one axis
(5 answers)
Closed 4 years ago.
I have a numpy vector of size (N,) which contains integers betwen 1-5 and by using the keras function to_categorical I am constructing the corresponing binary matrix that have size (Nx5). For example if the first value of the vector is 1 then the first row of the array it is (1, 0, 0, 0, 0). How can I do the opposite? By having an array of values (could also double values) to return the index with the highest value? Is there any command that can do that automatically?
As an example my input could be an 2D array with doubles for example one row could be [[0.025, 0.022, 0.58, 0.011, 0.22 ]....] and the result to be for that row [3 ...].

If a is your array, use:
a.argmax(axis=1)
This returns the index of maximum value of each row of the array.
Demo:
>>> a = np.array([[5,2,3],[1,3,1]])
>>> a.argmax(axis=1)
[0 1]

Related

How to extract specific part of a numpy array? [duplicate]

This question already has answers here:
Numpy: find index of the elements within range
(12 answers)
How to return indices of values between two numbers in numpy array
(2 answers)
How to conditionally select elements in numpy array
(2 answers)
Closed 1 year ago.
I have the following data lets call it y with the corresponding x values. Plotting plt.plot(x,y) results in: I now want to extract a specific part of that data that is between the x-values of 8.6075 and 8.62. Plotting the part using plt.xlim(8.6075, 8.62) gives the following. I have tried to find the indices using of the x-values using index1=np.where(x==8.6075), index2=np.where(x==8.62) and than just cutting out that specific part of the data using y_cutout = y[index1:index2]. The problem was that the exact values 8.6075 and 9.62 have no indices that they are defined on.
You can find the index of the nearest value by creating a new array of the differences between the values in the original array and the target, then find the index of the minimum value in the new array.
For example, starting with an array of random values in the range 5.0 - 10.0:
import numpy as np
x = np.random.uniform(low=5.0, high=10.0, size=(20,))
print(x)
Find the index of the value closest to 8 using:
target = 8
diff_array = np.absolute(x - target)
print(diff_array)
index = diff_array.argmin()
print(index, x[index])
Output:
[7.74605146 8.31130556 7.39744138 7.98543982 7.63140243 8.0526093
7.36218916 6.62080638 6.18071939 6.54172198 5.76584536 8.69961399
5.83097522 9.93261906 8.21888006 7.63466418 6.9092988 9.2193369
5.41356164 5.93828971]
[0.25394854 0.31130556 0.60255862 0.01456018 0.36859757 0.0526093
0.63781084 1.37919362 1.81928061 1.45827802 2.23415464 0.69961399
2.16902478 1.93261906 0.21888006 0.36533582 1.0907012 1.2193369
2.58643836 2.06171029]
3 7.985439815743841
You can make a selection by following
y_cutout = y[(x >= 8.6075) & (x <= 8.62)]
(Fixed as #AcaNg mentioned in the comment)

Convert tensor of integers to binary tensor with 1 only at that index [duplicate]

This question already has answers here:
converting tensor to one hot encoded tensor of indices
(3 answers)
Closed 1 year ago.
Is there a pain free to convert a tensor of integers to a binary tensor with 1 only at that integers index in pytorch?
e.g.
tensor([[1,3,2,6]])
would become
tensor([[0,1,0,0,0,0,0],
[0,0,0,1,0,0,0],
[0,0,1,0,0,0,0],
[0,0,0,0,0,0,1]])
t = tensor([[1,3,2,6]])
rows = t.shape[1]
cols = t.max() + 1
output = torch.zeros(rows, cols) # initializes zeros array of desired dimensions
output[list(range(rows)), t.tolist()] = 1 # sets cells to 1
To clarify the last operation, you can pass in a list of the row numbers and column numbers, and it will set all those elements to the value after the equal.
So in our case we want to set the following locations to 1:
(0,1), (1,3), (2,2), (3,6)
Which we'd represent as:
output[[0,1,2,3], [1,3,2,6]] = 1
And you'll see that those lists line up with a) an increasing list up to the total row count, and b) our original tensor

Sort rows of a 2d numpy array in ascending order [duplicate]

This question already has answers here:
Python Numpy Sort rows [duplicate]
(2 answers)
Closed 2 years ago.
I have an numpy array arr with shape (1500,10) where each element is a digit from 0 to 9. I'd like to sort the array as each row's elements are concatenated to form a single number and then sort these numbers in ascending order.Let a simple array be like:
arr = ([[3,4,1,5,1,2,3,4,5,6],
[1,2,3,5,6,2,9,2,1,2],
[0,3,1,4,2,1,6,8,2,1],
[0,1,3,5,1,2,9,2,1,7],
[2,3,5,7,1,2,5,7,1,5]])
it should return
arr = ([[0,1,3,5,1,2,9,2,1,7],
[0,3,1,4,2,1,6,8,2,1],
[1,2,3,5,6,2,9,2,1,2],
[2,3,5,7,1,2,5,7,1,5],
[3,4,1,5,1,2,3,4,5,6]])
You can do the following:
arr[np.lexsort(np.flip(arr.transpose(), axis=0))]

How to adjust dimensions in numpy.array [duplicate]

This question already has answers here:
Convert Python sequence to NumPy array, filling missing values
(8 answers)
Convert list of lists with different lengths to a numpy array [duplicate]
(3 answers)
Closed 3 years ago.
all
I have a numpy array, the dimension of the element is different. for example:
[
[1,2,3],
[2,3,4,5,6]
[1,2]
]
I want to adjust the element dimension, set the dimension as the largest one, and fill with 0, how can I do it?
You can create a placeholder with the desired shape first, then fill the placeholder with the data list
data = [[1,2,3], [2,3,4,5,6], [1,2]]
# create a placeholder
tmp = np.zeros((len(data), max([len(item) for item in data])))
# fill the placeholder with data
for ind, line in enumerate(data):
tmp[ind, :len(line)] = line
However, this may not be super fast when the size of the data list is large.

How to divide matrix elements by corresponding matrix row sum in numpy? [duplicate]

This question already has answers here:
numpy divide row by row sum
(3 answers)
Closed 7 years ago.
For example,
M= [[1,2], [7,8]]
then I want
[[1/3, 2/3], [7/15, 8/15]]
I'm trying to do this vectorized. One idea I have is to write s = np.sum(M, axis=1); this gives us the corresponding row sums. Then I could maybe transpose s, and copy it along the columns, then do an elementwise division of M/s, but even this seems too hacky. What's the right way?
Use tile to repeat it along the dimension on which sum operated.
M / np.tile(np.sum(M, 1), (1, M.shape[1]))

Categories

Resources