# Questions tagged [numpy]

NumPy is the fundamental package for scientific computing with the programming language Python.

727 questions
Filter by
Sorted by
Tagged with
1 vote
49 views

### Generate new column based on two columns of dataframe

I need to generate column a_b based on column a and column b of ...
• 135
43 views

### Python image triangular mosaic creator

This is a Python script that turns images into "low-poly" art/creates a triangular mosaic from the image, I wrote this script completely by myself. In short, it takes a filepath to an image, ...
• 1,656
42 views

### Deep Reinforcement Learning training on gym data

Related question: Extract the last row in a dataframe with a timestamp before some given time This is part of the code for Deep Reinforcement Learning training on a standards-based gym env. What I ...
• 135
32 views

### Numpy way of selecting indices of 2D numpy array if a condition met [closed]

I have a snippet of code to get indices of a 2D numpy array if the maximum value of that raw is greater than a number. ...
1 vote
62 views

### A collection of Python functions that fill a given region with rectangles

These are what I was working on in the last few days, I wrote a bunch of functions that fill a given region with rectangles of different colors. The functions can: Randomly split a region into sub ...
• 1,656
33 views

### Python function that skews an image while preserving its aspect ratio using affine transformation

See this post for more details. I re-implemented my idea using affine transformations, and finally understood how transformation matrices are formed... Turns out it is stupidly simple, but nothing I ...
• 1,656
109 views

### Python function that shears image by n degrees while keeping the relationship between height and width

I am trying to find a method to shear/skew an image horizontally or vertically by n degrees (n ranges from -90 to 90 excluding the terminals) so that the result would meet the following conditions: ...
• 1,656
43 views

### Sampling from k circles with different radii

Description: I would like to sample n_points points from k circles whose radii and centers are given as lists. Note: This is my ...
• 121
63 views

### Python 3 OpenCV script that scans a giant picture to make a video

This is a Python script that I wrote completely by myself. Basically it scans a giant picture to make a video. They say one picture is worth more than a thousand words. Since a video contains ...
• 1,656
43 views

### Extract all elements of array with the given list of coordinates [duplicate]

I have to extract all elements of array with the given list of coordinates and insert into 2*298808 as position given in while loop How can I compress the code even more? ...
53 views

...
22 views

### Creating an image from data, and slicing it into zxy tile service

I have some data that comes out of a grib file. The values in the grib file come out in shape of (721, 1440). Each value ...
• 519
59 views

### Stacking Z axis on multiple [1440x720] DataFrames (X, Y)

Each datum represents a point in a 1440x720 image of the globe, The values are 0-9. Each new layer is of a higher elevation. I need to structure a DataFrame in a ...
• 519
104 views

### Python 3 script to make photo collages

I have written a Python script that takes a list of filenames of pictures and generates a photo collage of them. This is my most complex project yet, and I have written it completely by myself (like I ...
• 1,656
102 views

### Replace nested for loops when assigning intial conditions in a 4-dimensional array

I create a 4-dimensional (x, y, z, t) array of zero values. I then set the initial values at t = Tmax. To do this, I use a nested for loop. I attempted to improve ...
• 69
66 views

### type hinting/documenting/extension of a Cython lib

I've updated some of the type hinting/documentation in a lib called pygrib. The source documentation can be found here. The goal is to extend the ...
• 519
1 vote
68 views

### Solving a 3D heat diffusion PDE

I am trying to solve a heat diffusion type PDE using a finite difference method. I would like to preface that I have seriously simplified the code. Just so that anyone who tries to help me, doesn't ...
• 69
68 views

### dataframe mean outliers to NaN to derive a higher quality mean for area,speed,azimuth

I have some data that represents the area, speed(meters per second), and azimuth(rads) of a polygon. The objective is to determine the mean for each set of parameters. With that mean value, apply a ...
• 519
1 vote
39 views

### numpy mean azimuth with functools.reduce

From the left to the right column calculating the mean azimuth, and using that mean value to calculate the subsequent columns mean value data (azimuth radians) ...
• 519
52 views

### Making a list of shades and styles for plots

For a section of a much larger program that generates plots, I am creating a list of styles that I can pull from to create a consistent format from plot-to-plot. It works as follows I have a list of ...
• 33
99 views

### pandas dataframe of temps and numpy.linalg.lstsq

The problem is this. Forecast model data indicates at 0700Z the 1000 milibar temps will be -4C°. The forecaster knows that is incorrect, so they choose to make a correction. This correction should ...
• 519
125 views

### Vincenty's distance Direct formulae numpy

I've refactored a function from the pygc library used to generate the great_circle. The Vincenty's equation below can be found ...
• 519
132 views

### Numerical integration in Python involving four dimensions

I'm trying to obtain the following S2 from a given S1,  S2(i, j) = \sum _{k=0} ^i \sum _{l=0} ^j S1(k, i-k, j, l) e^{ -\sqrt{...
66 views

### 3D to 4D surface fitter converter

I recently wrote this bit of code while trying to convert a 3D surface fitter to a 4D one. The solution works and I didn't want to leave the next guy to grind out the solution so I made the code as ...
107 views

### pandas conditions across rows and columns

Is there a better way to accomplish this? The function determines the Category for each row based on some conditions. Another set of conditions determines the <...
• 519
1 vote
39 views

### Pandas Numpy Nvector to generate a storm track algorithm

I made previous post here, relating to this topic. The class uses now uses some additional 3rd party libs nvector & pygc. ...
• 519
1 vote
60 views

### Multiple GeoJSON Feature Collections to a Pandas MultiIndex DataFrame

Edit: The end objective is to monitor the movement of severe weather, determine its vector and make a prediction. I'm working with a GeoJSON ...
• 519
474 views

### Laser physics simulation

This is for a computation for laser physics. The speed is quite slow, which seems to be delayed a lot by the three nested for loops with indices j,k, and l (with comments below). And the computation ...
1 vote
109 views

### Minimum re-labeling to make an array as close as possible to another one

I have an array with integer labels in [0, n-1], say arr1=[1, 0, 3, 2] with n = 3. And then I have another array again with integer labels in [0, n-1], say arr2=[0, 0, 2, 3]. How can I find the ...
• 13
82 views

### Calculating quantiles of timeseries data and then create a "fanplot"

My code is working properly, but I am looking for better approach in the calculation of quantiles and the finding of the data in the dataframe. ...
• 63
1 vote
51 views

### Exponentially-weighted moving mean and standard deviation of an irregularly-spaced weighted time series

The following numpy/python function computes exponentially-weighted moving mean and standard deviation of an irregularly-spaced weighted time series. I want to make it faster by getting rid of the ...
67 views

### Collatz conjecture with plots

I have an assignment where I write a Collatz Conjecture program for a series of starting values from 1 to N and make two plots: number of iterations vs starting value and computed numbers vs starting ...
• 215
1 vote
144 views

### Slicing multi index DataFrame into JSON object

I have a MultiIndex pd.DataFrame that I generated from a .txt that is forecast model data. ...
• 519
60 views

### Fastest way to find common events in 2 time series numpy arrays and calulate synchronisation statistic

Given 2 numpy arrays a1 and a2 (composed only of 0 and 1's), find index locations of all 0's in ...
• 162
1 vote
308 views

### Calculate a time weighted average of a feature

I'm trying to calculate a time weighted average of a feature (feat) based on previous rows by date for a given ...
• 133
1 vote
105 views

### Calculating all combinations from arrays AND (arrays OR floats)

Context: My question comes from needing to iterate different np.arange arrays. The main motivation is to calculate all the possible combinations between the arrays, ...
• 163
63 views

### Calculate of the average of time discounted values

Given the current table: ...
• 133
43 views

### find max value in islands defined by other vector

I have a vector of values vals, a same-dimension vector of frequencies freqs, and a set of frequency values ...
• 255
80 views

### Path of a particle in a non-uniform B field

I have completed my project which simulates the path of a particle trapped in a device known as a magnetic mirror. I would like your thoughts and improvements on my implementation. Here is the code: <...
• 215
1 vote
54 views

### Solving a linear system with Gaussian Elimination

I decided to implement a solver for linear systems of equations based on the gaussian elimination and reduction to upper triangular form. The gaussian elimination is quite simple to implement but the ...
• 27.2k
119 views

### Function to calculate the GC content variation in a sequence

I came across this BMC Genomics paper: Analysis of intra-genomic GC content homogeneity within prokaryotes And I implemented some Python functions to make this available as part of a personal project. ...
123 views

### How can I optimize this pairwise Euclidean distance search algorithm?

x and y are two-dimensional arrays with dimensions (AxN) and (BxN), i.e. they have the same number of columns. I need to get a matrix of Euclidean distances between each pair of rows from x and y. I ...
1 vote
81 views

### using pandas and numpy to parse dirty .csv

I'm relatively new to Python have been writing for a few months now. I've started a Pandas Numpy project that starts with parsing large and somewhat sloppy formatted textfile, its not exactly csv but ...
• 519
73 views

### Generating a matrix with each row having normalized weights

I just asked this question over Stack Over Flow on how to improve my code and reposting it here as someone on Stack Overflow recommended this platform. I have written two python functions and they are ...
• 21
56 views

### Implementation of a crypto formula in Python

I need to implement in Python the formula in the image. The set B is a set of real numbers $-1 \le t \le 1$. Also, $\mathbb{F}_2$ is a set with the elements 0 and 1. $y_i$ is the $i$-th ...
• 139
182 views

### Implementing histogram in python

I was trying to implement my own version of matplotlib's hist function and I came up with this: ...
1 vote
70 views

### Using arrays more effectively with numpy

I previously posted some code I've been working on and had a fantastic answer, but as I didn't post the full code I was then unable to bring it all together using the much faster numpy implementation, ...
• 63
100 views

### Calculating a function over a large array

I have written some code to determine an interpolation matrix over a grid of points. I originally wrote the code in matlab, where under the specified parameters it took just over 2 minutes. The ...
• 63
482 views

### line-circle intersection [closed]

I have this code that generates a random line and a random circle. ...
• 109