I have this torch array.
tensor([[8.22266e-01, 1.34659e-03, 9.85146e-04, 8.11100e-04],
[9.35547e-01, 1.22261e-03, 8.70228e-04, 1.25122e-03],
[9.48730e-01, 1.21975e-03, 9.28402e-04, 8.44955e-04],
[7.97363e-01, 9.16004e-04, 9.16004e-04, 8.53539e-04],
[9.26270e-01, 7.69138e-04, 8.47816e-04, 1.12724e-03],
[9.43848e-01, 7.44820e-04, 8.53539e-04, 8.60691e-04],
[7.89062e-01, 6.50406e-04, 9.23634e-04, 8.44479e-04],
[9.29688e-01, 7.02858e-04, 7.60078e-04, 1.19591e-03],
[9.47266e-01, 5.88894e-04, 8.36849e-04, 9.37462e-04],
[8.27637e-01, 1.92642e-03, 1.73283e-03, 2.53105e-03],
[9.22363e-01, 2.23160e-03, 1.37615e-03, 2.46811e-03],
[9.31641e-01, 1.92928e-03, 1.49632e-03, 2.53296e-03],
[8.25684e-01, 1.89209e-03, 1.70994e-03, 2.39944e-03],
[9.21875e-01, 1.90926e-03, 1.28174e-03, 2.44904e-03],
[9.25781e-01, 1.65272e-03, 1.45912e-03, 2.44141e-03],
[8.39844e-01, 3.17955e-03, 4.02832e-03, 5.22614e-03],
[9.17480e-01, 3.13759e-03, 3.37982e-03, 4.72260e-03],
[9.37012e-01, 2.63405e-03, 3.57056e-03, 4.70734e-03]], device='cuda:0', dtype=torch.float16)
I like to get its max value and index for each row in Torch is
conf, j = x[:, 5:].max(1, keepdim=True)
How can I implement in numpy if I have 2D numpy array?
You are looking for numpy.argmax
Related
I have two arrays:
values_arr = [[100,1], [20,5], [40,50]...[50,30]]
images_arr = [img1, img2, img3,...imgn]
Both the arrays are numpy arrays.
The values_arr and images_arr are in the same order.
i.e
[100, 1] corresponds to img1
How do I get the image given the value of index?
index = [20,5]
In this case, I should get img2 given the value of index = [20,5].
You can make a dict as
values_arr_tup = [tuple(i) for i in values_arr]
dict_ = {key:value for key,value in zip(values_arr_tup ,images_arr)}
then perform dict_[tuple(index)] to get the image
You can use np.where to extract the index of the item :
images_arr[np.where(values_arr == [20,5])[0][0]]
The gyration tensor of a set of N points in 3d space is defined as
assuming the condition
.
How do I compute this in numpy without using an explicit for loop? I know that I can just do something like
import numpy as np
def calculate_gyration_tensor(points):
'''
Calculates the gyration tensor of a set of points.
'''
COM = centre_of_mass(points)
gyration_tensor = np.zeros((3, 3))
for p in points:
gyration_tensor += np.outer(p-COM, p-COM)
return gyration_tensor / len(points)
but this quickly becomes inefficient for large N, because of the for loop. Is there a better way to do it?
You can do with np.einsum like this:
def gyration(points):
'''
Calculate the gyrason tensor
points : numpy array of shape N x 3
'''
center = points.mean(0)
# normalized points
normed_points = points - center[None,:]
return np.einsum('im,in->mn', normed_points,normed_points)/len(points)
# test
points = np.arange(36).reshape(12,3)
gyration(points)
Output:
array([[107.25, 107.25, 107.25],
[107.25, 107.25, 107.25],
[107.25, 107.25, 107.25]])
Suppose i have a numpy array u with a given shape, a a divisor d of the total number of entries in u. How can i fastly reshape u to be shaped (something,d) ?
The case where u is just a double should be included as well -> (1,1)
The case where u is empty should become a (0,d) shaped array
You want to use reshape
u.reshape(-1, d)
There is no double in Python you do you mean float ?
In short :
import numpy as np
def div_reshape(arr, div):
if arr.size == 0:
return np.empty(shape=(0, div))
elif arr.size == 1:
return arr.reshape(1, 1)
else:
return arr.reshape(-1, d)
How to get data by querying radius from ball tree? For example
from sklearn.neighbors import BallTree
import pandas as pd
bt = BallTree(df[['lat','lng']], metric="haversine")
for idx, row in df.iterrow():
res = df[bt.query_radius(row[['lat','lng']],r=1)]
I want to get those rows in df that are in radius r=1. But it throws type error
TypeError: unhashable type: 'numpy.ndarray'
Following the first answer I got index out of range when iterating over the rows
5183
(5219, 25)
5205
(5219, 25)
5205
(5219, 25)
5221
(5219, 25)
Traceback (most recent call last):
File "/Users/Chu/Documents/dssg2018/sa4.py", line 45, in <module>
df.loc[idx,word]=len(df.iloc[indices[idx]][df[word]==1])/\
IndexError: index 5221 is out of bounds for axis 0 with size 5219
And the code is
bag_of_words = ['beautiful','love','fun','sunrise','sunset','waterfall','relax']
for idx,row in df.iterrows():
for word in bag_of_words:
if word in row['caption']:
df.loc[idx, word] = 1
else:
df.loc[idx, word] = 0
bt = BallTree(df[['lat','lng']], metric="haversine")
indices = bt.query_radius(df[['lat','lng']],r=(float(10)/40000)*360)
for idx,row in df.iterrows():
for word in bag_of_words:
if word in row['caption']:
print(idx)
print(df.shape)
df.loc[idx,word]=len(df.iloc[indices[idx]][df[word]==1])/\
np.max([1,len(df.iloc[indices[idx]][df[word]!=1])])
The error is not in the BallTree, but the indices returned by it are not used properly for putting it into index.
Do it this way:
for idx, row in df.iterrows():
indices = bt.query_radius(row[['lat','lng']].values.reshape(1,-1), r=1)
res = df.iloc[[x for b in indices for x in b]]
# Do what you want to do with res
This will also do (since we are sending only a single point each time):
res = df.iloc[indices[0]]
Explanation:
I'm using scikit 0.20. So the code you wrote above:
df[bt.query_radius(row[['lat','lng']],r=1)]
did not work for me. I needed to make it a 2-d array by using reshape().
Now bt.query_radius() returns array of array of indices within the radius r specified as mentioned in the documentation:
ind : array of objects, shape = X.shape[:-1]
each element is a numpy integer array listing the indices of neighbors of the corresponding point. Note that unlike the results of
a k-neighbors query, the returned neighbors are not sorted by distance
by default.
So we needed to iterate two arrays to reach the actual indices of the data.
Now once we got the indices, in a pandas Dataframe, iloc is the way to access data with indices.
Update:
You dont need to query the bt each time for individual points. You can send all the df at once to return a 2-d array containing the indices of points within the radius to the point specified that index.
indices = bt.query_radius(df, r=1)
for idx, row in df.iterrows():
nearest_points_index = indices[idx]
res = df.iloc[nearest_points_index]
# Do what you want to do with res
I'm not sure how to achieve the following (preferably without a loop).
I have a numpy array A having dimensions 100*100*3.
I also have a numpy array M having the same dimensions (100*100*3). M is actually a mask, and M[i,j] is [0,0,0] for most pairs (i,j) but for some pairs (i,j) it is not equal to [0,0,0].
What I would like to do is the following:
A[i,j] = M[i,j] when M[i,j] != [0,0,0]
A[ M != [0,0,0]] = M [ M != [0,0,0]] doesn't seem to work.
How can this be done efficiently with numpy?
You were needed to look for ALL match along the last axis and use that mask for boolean-indexing/masking -
mask = ~(M==0).all(-1) # or (M!=0).any(-1)
A[mask] = M[mask]
Or use np.where -
mask = ~(M==0).all(-1,keepdims=1)
Aout = np.where(mask, M, A)