Category "numpy"

Pandas Pivot table - How compute the following default ratio?

I am able to compute the default rate in number (e.g, the percentage of customers falled into default), with the code below, getting the following output: impor

How to generate a sphere in 3D Numpy array

Given a 3D numpy array of shape (256, 256, 256), how would I make a solid sphere shape inside? The code below generates a series of increasing and decreasing ci

Pythonic way to get both diagonals passing through a matrix entry (i,j)

What is the Pythonic way to get a list of diagonal elements in a matrix passing through entry (i,j)? For e.g., given a matrix like: [1 2 3 4 5] [6 7 8

Python-AttributeError: 'int' object has no attribute 'decode'" when trying to call the GML file on NetworkX

Premise・What I want to achieve I'm going to use Python to read the GML file. Error Message Traceback (most recent call last): File "firstgml.py", line

Vectorize a function for a GroupBy Pandas Dataframe

I have a Pandas dataframe sorted by a datetime column. Several rows will have the same datetime, but the "report type" column value is different. I need to se

Change 1's to 0 and 0's to 1 in numpy array without looping

Let's say I have a numpy array where I would like to swap all the 1's to 0 and all the 0's to 1 (the array will have other values, and there is nothing special

Fastest way in numpy to get distance of product of n pairs in array

I have N number of points, for example: A = [2, 3] B = [3, 4] C = [3, 3] . . . And they're in an array like so: arr = np.array([[2, 3], [3, 4], [3, 3]]) I nee

Sliding window input (image sequence) for convolutional neural network

I am currently trying to feed an image sequence as a single input entity to my CNN. I found the numpy utility numpy.lib.stride_tricks.sliding_window_view My ima

Mypy in Nox session not finding Nump type stubs

My nox session is defined like: @nox.session(python=["3.10", "3.9.10"]) def mypy(session: Session) -> None: args = session.posargs or locations insta

How to efficiently compute the heat map of two Gaussian distribution in Python?

I am trying to produce a heat map where the pixel values are governed by two independent 2D Gaussian distributions. Let them be Kernel1 (muX1, muY1, sigmaX1, si

Integer out of range when inserting large number of rows to postgress

I have tried multiple solutions and way around to solve this issue, probably something is still I am missing. I want to insert a list of values to my database.

How to append np.arrays() in a for-loop?

This application is in analysis of astrophysical data, but the problem is purely a question of programming/syntax. I want to go through my 53 bins of data (each

Stratified Sampling in Pandas

I've looked at the Sklearn stratified sampling docs as well as the pandas docs and also Stratified samples from Pandas and sklearn stratified sampling based on

Compute maxima and minima of a 4D tensor in PyTorch

Suppose that we have a 4-dimensional tensor, for instance import torch X = torch.rand(2, 3, 4, 4)

How to fix Python error "...failed to map segment from shared object" appearing when I try to import NumPy library on GCP?

I've recently started to use Google Cloud Platform and I run my python scripts in Cloud Shell within Linux environment. By running one of the scripts that is u

Extract misclassified images using CNN keras

I am having difficulties extracting misclassified images, I tried to use the following line of code: inc= np.nonzero(model.predict_classes(test_data).reshape(-

Matplotlib: display element indices in imshow

From this answer I know how to plot an image showing the array values. But how to show the i,j indices of each element of the array, instead of the values thems

Can numpy.tensordot or ufunc replace this nested for loop?

Note: I know this is very similar to Use numpy.tensordot to replace a nested loop , only that the actual instance we are working on seemed different( I need qua

create cosine similarity matrix numpy

Suppose I have a numpy matrix like the following: array([array([ 0.0072427 , 0.00669255, 0.00785213, 0.00845336, 0.01042869]), array([ 0.00710799, 0.00

Find positions of elements in sorted array

Suppose I have some numpy array (all elements are unique) that I want to sort in descending order. I need to find out which positions elements of initial array