Let a = np.arange(1, 4). To get the 2 dimensional multiplication table for a, I do: For 3 dimensions, I can do the following: How could I write a function that takes a numpy array a and a number of dimensions n as input and ouputs the n dimensional multiplication table for a? Answer This should do what you ne…
Tag: numpy
Is there any way to show mean in box plot using Python?
I’m just starting using Matplotlib, and I’m trying to learn how to draw a box plot in Python using Colab. My problem is: I’m not able to put the median on the graph. The graph just showed the quartiles, mean, and outliers. Can someone help me? My code is the following. Answer I tried running…
Pandas apply unique random number to nan else go to next row
I would like to apply a unique random number to ‘nan’ and keep the group code where group code exists. I’ve tried the following, but i cant seem to get the syntax right, what am i doing wrong. Answer Step 0:- Your Dataframe:- Step 1:- Firstly define a function:- Step 2:- Then just use apply(…
Cupy says it has scipy’s cg implemented, but cannot find cg(conjugate gradient method) when installed
I am unable to find this https://docs.cupy.dev/en/latest/reference/generated/cupyx.scipy.sparse.linalg.cg.html#cupyx.scipy.sparse.linalg.cg after the installation of cupy. It says module not found Can anyone help me? I don’t know what’s wrong, but I have installed cupy via pip, and this library wa…
numpy filling the diagonal 0 for 3D array
Suppose I have a 3D array, how can I fill the diag of the first two dimensions to zero. For example Is there a way to replace the for loop? Answer The following is one of the solution
create a matrix from combinations with values
I have some combinations like Any idea on how I efficiently can create a four by four matrix with these 0,1 values from all these combinations? So the result will be something like: Answer Imagine if the “combinations” are stored in a file in the following format (or similar): Then you can do: Exa…
Pulling x-values of spline integer y-values?
I have the given sample data and interpolated spline: I would like to pull the x values that correspond to the integer y values of the spline, but am not sure how to do this. I assume I will be using np.where() and have tried (to no avail): Answer You could use the find_roots function from this post to find
Importing multiple excel files with similar name, pivoting each excel file and then appending the results into a single file
My problem statement is as above. Below is my progress so far I want to extract multiple excel files from the same location namely Test1 Test2 Test3…(I am using glob to do this) (DONE) 2. I want to iterate through the folder and find files starting with a string(DONE) 3. I then formed an empty dataframe…
3D numpy array threshold comparison
I’m trying to solve whether or not each element in a 3d numpy array fits within a certain threshold. Currently I have nested for loops that get the job done, but it’s really slow and I don’t have all day to wait for my code to run LOL. The input spec is a little bit weird so I haven’t …
How to quickly subset many dataframes?
I have 180 DataFrame objects, each one has 3130 rows and it’s about 300KB in memory. The index is a DatetimeIndex, business days from 2000-01-03 to 2011-12-31: I preprocess all the data taking advantage of numpy/pandas vectorization, then I have to loop through the dataframes day by day. To prevent the …