What is the role of axis parameter in np.argmax()? - python

Wee know, in axis parameter 0,1 means column and row wise maximum element index but
for 2,3 & so on what it indicates? An example code is given here. What is the output significance in this code?

When you have an array of higher dimensions you will also have new axes. For example, in a dimension 3 array (e.g. a cube) you will have 3 axes (row, column, depth).
When you pass the axis in the np.argmax you are telling numpy along which axis you want the maximum argument. 3 will throw an error because your array only has 3 axes (0, 1, 2).
Here is an article about numpy arrays axes.

Related

Min numpy - 3D array

I get confused by this example.
A = np.random.random((6, 4, 5))
A
A.min(axis=0)
A.min(axis=1)
A.min(axis=2)
What mins are we really computing here?
I know I can think of this array as a 6x5x4 Parallelepiped in 3D space and I know A.min(axis=0) means we go along the 0-th axis. OK, but as we go along that 0-th axis all we get is 6 "layers" which are basically rectangles of size 4x5 filled with numbers. So what min am I computing when saying A.min(axis=0) for example?!?! I am just trying to visualize it in my head.
From A.min(axis=0) I get back a 4x5 2D matrix. Why? Shouldn't I get just 6 values in a 1D array. I am walking along the 0-th axis so shouldn't I get 6 values back - one value for each of these 4x5 rectangles?
I always find this notation confusing and just don't get it, sorry.
You calculate the min across one particular axis when you are interested in maintaining the structure of the remainder axes.
The gif below may help to understand.
In this example, your result will have shape (3, 2).
That's because you are getting the smallest value along axis 0, which squeezes that dimension into only 1 value, so we don't need the dimension anymore.

How the shape is (3 2 1) | Numpy |

I am learning numpy , have a question in my mind not able to clearly visualise from where this 1 as come in shape
import numpy as np
a = np.array([ [[1],[56]] , [[8],[98]] ,[[89],[62]] ])
np.shape(a)
The output is printed as : (3 ,2 , 1)
Will be appreciated if you could represent in diagrammatic / image format
What actually the 1 means in output
Basically, that last 1 is because every number in a has brackets around it.
Formally, it's the length of your "last" or "innermost" dimension. You can take your first two dimensions and arrange a as you would a normal matrix, but note that each element itself has brackets around it - each element is itself an array:
[[ [1] [56]]
[ [8] [98]]
[[89] [62]]]
If you add an element to each innermost-array, making that third shape number get larger, it's like stacking more arrays behind this top one in 3d, where now the corresponding elements in the "behind" array are in the same innermost array as the "front" array.
Equivalently, instead of considering the first two indices to denote the regular flat matrices, you can think of the back two making the flat matrices. This is how numpy does it: try printing out an array like this: x = np.random.randint(10, size = (3,3,3)). Along the first dimension, x[0], x[1], and x[2] are printed after each other, and each one individually is formatted like a 3x3 matrix. Then the second index corresponds to the rows of each individual matrix, and the third index corresponds to the columns. Note that when you print a, there's only one column displayed - its third dimension has size 1. You can play with the definition of x to see more what's going on (change the numbers in the size argument).
An alright example of visualizing a 3d array this way is this image, found on the Wikipedia page for the Levi-Civita symbol:
Don't worry too much about what the Levi-Civita symbol actually is - just note that here, if it were a numpy array it would have shape (3,3,3) (like the x I defined above). You use three indices to specify each element, i, j, and k. i tells you the depth (blue, red, or green), j tells you the row, and k tells you the column. When numpy prints, it just lists out blue, red, then green in order.

Trouble Finding Spectrum Peaks on Python/ Google Colab

I have a spectrum (of an oil sample) as a 2D array in a cvs file that i want to find the peaks for in wavelengths 600 - 1800 cm-1. I've tried the scipy.signal.find_peaks but that takes a 1D array and I have a 2D array with the wavelengths and corresponding peak values.
any help would be appreactiated since im very beginner at python
Edit: I also tried doing the following:
from detecta import detect_peaks
ind = detect_peaks(df)
where df is the name of my array (which has two columns) and an error pops up: ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
scipy.signal.find_peaks() only takes a one-dimensional array containing the peaks. So you should be able to just select the column in your DataFrame with the peaks as so:
# note that find_peaks returns an array of peak indices, and a dictionary of properties
ind, properties = scipy.signal.find_peaks(df["name of column with peaks"])
Then if you only want the peaks, select the rows using the ind array you just created:
peak_df = df[df.index.isin(ind)]

Python: How to get a unit vector along a given numbered axis?

I start with a vector, say (a,b,c), and I want to get back a collection of three unit vectors in n dimensions, the first along axis a, the second along axis b and the third axis c. How can I do this without just looping over all my values.

changing shapes of a numpy array

How to change the shape of array from ixMxNx3 to (M*N)xix3?
I have a ixMxNx3 array L. You can think of L as an array containing i images, each image has height=M, width=N, and in each pixel it has a three-dimensional vector (or rgb). Let P = M*N. I can change its shape to ixPx3 by L.reshape(i,P,3). (I hope it is really changing it to the shape I want). How do I change its shape to Pxix3? i.e. an array that contains P points, each point has i images, each image of that point has a three-dimensional vector.
How can this change of shape be accomplished?
numpy.rollaxis can shift the position of an axis in a NumPy array:
L = L.reshape([i, P, 3])
L = numpy.rollaxis(L, 1)
It takes 3 arguments, one optional. The first is the array, the second is the axis to move, and the third is confusingly documented as "The axis is rolled until it lies before this position". Basically, if you want to move the ith axis to the jth position and j<i, the third argument should be j. If j>i, the third argument should be j+1. I don't know why it works that way. The third argument defaults to 0.

Categories

Resources