I was trying to simulate multivariate normal data in python the following way However, the resulting variables are always perfectly linearly correlated. I then checked np.linalg.eigvals(Sigma) and realized that Sigma is not positive semi-definite. This surprised me since 1) I did not get an error from multiva…
Tag: numpy
How to split a 2d numpy array vertically into a new 2d numpy array?
I have this code that essentially splits a two-dimensional numpy array horizontally and makes a new two-dimensional numpy array out of it. Output of my code: How can I do this with less lines of code? I assume it could be very resource intensive, as soon as I apply this example to my larger task. Answer I sup…
Python numba returned data types when calculating MSE
I am using numba to calculate MSE. The input are images which are ready as numpy arrays of uint8. Each element is 0-255. When calculating the squared difference between two images the python function returns (expectedly) a uint8 result, but the same function when using numba returns int64. What’s unclea…
How to inverse the rgb image color from “white-black” to “black-white” in matplotlib
The following two images are from the Mnist dataset. The image_7 is a gray image with shape (28, 28) while image_2 is (28, 28, 3). I want to inverse the image colors and display it in the matplotlib. As you can see, the second image is inversed. But how to inverse the fourth image from “white-black̶…
How to get prior close when you have all stocks in a single DF?
Sorry for the noob question. I have a bunch of stocks in a sqlite3 database: When I print the df, it gives me the following (where each stock_id refers to a unique stock, e.g APPL): I need to target each unique stock_id individually, and get the prior close. I know if each stock was in its own separate datafr…
fastest way to reshape 2D numpy array (gray image) into a 3D stacked array
I have a 2D image with the shape (M, N), and would like to reshape it into (M//m * N//n, m, n). That is, to stack small patches of images into a 3D array. Currently, I used two for-loop to achieve that Is there any other faster way to do this? Thanks a lot! Answer Use skimage.util.view_as_blocks: Output: NB. …
Alternating column values
I am working on a project where my dataset looks like bellow: Origin Destination Num_Trips Hamburg Frankfurt 2 Hamburg Cologne 1 Cologne Hamburg 3 Frankfurt Hamburg 5 I am interested only on one way either “Hamburg – Frankfurt” or “Frankfurt – Hamburg” and add them as numbe…
Numpy multiplication using * (asterisk) returning wrong values when using named variables
I am running into a problem using the operator * with numpy scalars, and it would be great if someone can explain what is going on. Basically, I needed to multiply the sums of columns and rows from various dataframes, and the easiest way to do that was to assign each aggregate to a variable, and then multiply…
the speed of numpy sum of different axis
We know that numpy is C order stored so .sum(axis=1) should be faster that .sum(axis=0). But I find that But when the size change to 10000 Answer First of all, I cannot reproduce the effect with the last version of Numpy (dev) nor the version 1.21.5. I got respectively 30.5 ms and 36.5 ms (so the opposite beh…
storing result from function directly into DataFrame with return
I’m new to programming and python, I’m trying to create a function to iterate over a dataframe and directly store results from the function to dataframe, so far here is what I’ve done: after running it I’m able to get the NumPy array from p and store it to a variable then transform it …