Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

Questions tagged [numpy]

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

2
votes
1answer
44 views

Aggregate Pandas Columns on Geospacial Distance

I have a dataframe that has 3 columns, Latitude, Longitude and Median_Income. I need to get the average median income for all points within x km of the original point into a 4th column. I need to do ...
12
votes
1answer
1k views

Factorio analysis: data munging

This project is... a little ridiculous. It's working, but it's a complete mess. Data about Factorio's game economy are pulled from the wiki via the MediaWiki API, scrubbed, preprocessed, and thrown ...
2
votes
0answers
31 views

Compare every combination of variables within the powerset

I have some survey data that represents individual's responses to multiple survey questions. There are about 10,000 people in my actual dataset, and each person answered 35 questions. From these 35 ...
2
votes
0answers
40 views

Local maxima 3D array python

I'm trying to find the local maxima in a 3D numpy array, but I can't seem to find an easy way to do that using numpy, scipy, or anything else. For now I implemented it using ...
2
votes
1answer
27 views

Function figuring out which close-by value comes from which input value

I would like to optimize the below traceback function. This function is part of an elementary step of the program and is called a lot... ...
6
votes
1answer
65 views

Monte Carlo errors estimation routine

I would value your opinion on the following piece of code. I am rather new to both Python and Monte Carlo analysis, so I was wondering whether the routine makes sense to more experienced and ...
5
votes
1answer
301 views

Improving the speed of creation for three Perlin Noise Maps in Python?

I am interested in learning how I can improve the speed of the code in this pygame file. I iterate over 6400 * 1800 * 3 or 34,560,000 elements of various numpy arrays here to apply noise values to ...
1
vote
0answers
31 views

Reduce memory consumption of numpy function

I have a Numpy function that takes the values of an existing array X and a size (int) k equivalent to the columns of the array. ...
8
votes
1answer
46 views

Python implementation of approximating the chance a particle is at a location after n steps in the cardinal directions

Recently, I became very interested in a probability practice problem in my textbook for my class. I decided to implement it in code and I think I got most of it implemented. Right now, I'm hoping to ...
6
votes
1answer
61 views

Plotting terrain pixels with PyGame based on random NumPy array

I am experimenting with Perlin Noise and random map generation. I have a 2D numpy ndarray full of 16-bit floats called map_list that I call from the singleton ...
6
votes
0answers
45 views

Extract principal components

First, I am aware that this can be done in sklearn - I'm intentionally trying to do it myself. I am trying to extract the eigenvectors from np.linalg.eig to form ...
2
votes
1answer
41 views

Calculating the potential of a matrix of size (424600, 55)

I have the following working code to calculate the potential of a matrix after normalizing the matrix. But it is too much time consuming. Is there a way to make this code run faster? ...
0
votes
0answers
40 views

Counting events that occur in some time intervals

I have a python code where I need to count the number of ids in a given range of timestamps as : ...
1
vote
0answers
32 views

How to speed up the retrieval of the corners of a mask

I have trained a polygon detector neural network to recognize the mask of "quadrilateral" (the mask generates curvy lines so it's not exactly a quadrilateral). I would like to get the corners of the ...
1
vote
1answer
75 views

Vectorized N-Dimensional Random Walk in Cartesian Coordinates

I have written a random-walk routine that I hope to build upon in the future. Before doing so, I was hoping to get some critique. I believe the implementation is correct. I noticed that many other ...
2
votes
1answer
70 views

Inverse mapping with bilinear interpolation on an image

The following is my solution for an inverse mapping with bilinear interpolation on an image. The original image is img and ...
5
votes
1answer
62 views

Code to plot graphs with multiple panels efficiently

I quite often plot graphs looking at how some property or function varies with different parameters. Normally, I find the analysis code can be written fairly succinctly and separated into a suitable ...
3
votes
0answers
46 views

Locality Sensitive Hash (similar to k-Nearest Neighbor), in Python+Numpy

I've tried implementing Locality Sensitive Hash, the algorithm that helps recommendation engines, and powers apps like Shazzam that can identify songs you heard at restaurants. LSH is supposed to run ...
3
votes
1answer
137 views

Image pre-processing and segmentation

I'm new learning python and image processing with python. For this reason, I took a project called "Classification of breast cancer images with deep learning". I applied the following techniques: 1)...
3
votes
0answers
42 views

Computing Doppler delay on a meshgrid

Objective Plot the contour of the iso-doppler and iso-delay lines for a transmitter-receiver reflection on a specular plane. Implementation This Doppler shift can be expressed as follows: $$f_{D,0}(...
1
vote
1answer
40 views

Plot a piecewise-defined function

I would like to plot the following function: $$ \begin{align} \Lambda(\delta\tau) &\equiv\ \chi(\delta\tau, 0) = \frac{1}{T_i} \int_0^{T_i} a(t_0+t')a(t_0+t'+\delta\tau) dt' \\ &= \begin{...
5
votes
1answer
50 views

Optimal algorithm to move a a rope from one position to another

I'm an engineer working with a deformable membrane that is attached to actuators. The goal is to move the membrane from one shape to another, without ripping the membrane. This imposes "neighbor ...
4
votes
1answer
49 views

Set of one-hot encoders in Python

In the absence of feature-complete and easy-to-use one-hot encoders in the Python ecosystem I've made a set of my own. This is intended to be a small library, so I want to make sure it's as clear and ...
3
votes
0answers
54 views

Minimization problem solving and its step limits

I am trying to solve the following minimization problem: $$ \min\lvert\lvert{x}\rvert\rvert_1 + \beta\lvert\lvert{x}\rvert\rvert^2_2 s.t. \sum_{m = 1}^M (y - \lvert{c}^{H} . x\rvert^2)^2 \le \epsilon ...
2
votes
2answers
38 views

Compare an array with a file and form groups from elements of an array

I have a text file with letters (tab delimited), and a numpy array (obj) with a few letters (single row). The text file has rows with different numbers of columns. ...
4
votes
1answer
304 views

Fast distance calculation for Starcraft2 bot

I am coding a bot for Starcraft 2, in which many distances have to be calculated every frame. Here is the part of the library that is being used and I want to improve: https://github.com/Dentosal/...
14
votes
2answers
315 views

Generating Julia set

So, I just wanted to post something on Rosetta Code, and I found this task of generating and plotting a Julia set: http://www.rosettacode.org/wiki/Julia_set. There was already one solution but it was ...
8
votes
1answer
86 views

Cumulative sum of the smallest neighbors in the previous row in a Numpy array

I have the following code snippet, which essentially does the following: Given a 2d numpy array, arr, compute sum_arr as follow:...
5
votes
1answer
68 views

Calculating time deltas between rows in a Pandas dataframe

I am trying to compute the difference in timestamps and make a delta time column in a Pandas dataframe. This is the code I am currently using: ...
4
votes
1answer
51 views

Python script for to evaluate business data

The following script is part of a further education I'm currently enrolled into. Not all of the code is written by myself. Function signatures for example. Therefore I have put the sections, written ...
3
votes
0answers
385 views

Distance transform on image using NumPy

I would like to find the find the distance transform of a binary image in the fastest way possible without using the scipy function distance_transform_edt(). The ...
3
votes
2answers
73 views

Simulation of an alien population

Background I've come across a puzzle (Problem 10 from this list of sample interview questions): One day, an alien comes to Earth. Every day, each alien does one of four things, each with equal ...
3
votes
1answer
72 views

Dice sums comparison probability

I was playing a fun little board game called U.S. Patent No. 1 about time traveling to register your time machine as the first U.S. patent. I started to wonder what the dice probabilities were for ...
4
votes
1answer
59 views

Project structure for a reactor model built in Python

I use Python for various computational reactor modeling activities. While there are many components to the models I develop, the basic structure of the project folder tends to be similar to the ...
2
votes
0answers
28 views

Calculate average round cell value from each 2D array cell

I am try to practice the leetcode and hit the following question 'image smooth'. Basically the question is to calculate average the around cell value from a 2D array. I have following code. But if ...
3
votes
1answer
87 views

List of word to list of index in a vocabulary [closed]

I have a dataset composed of ~700k samples. Each sample is composed of n_features features. Each feature is a word. Each feature has his own vocabulary. The size of the vocabularies range from 18 to ...
1
vote
1answer
78 views

Generate xyz coordinates of a tube made of multiple rings

In this code, I generate the coordinates of a tube made of multiple rings. Finally, I write those x,y,z coordinates in a .csv file. I want to simplify this code (maybe using a for loop?). ...
1
vote
0answers
28 views

Batch gradient descent and stochastic gradient descent

I'm trying to implement logistic regression and I believe my batch gradient descent is correct or at least it works well enough to give me decent accuracy for the dataset I'm using. When I use ...
2
votes
1answer
46 views

Implementation of a threshold detection function in Python

I want to implement following trigger function in Python: Input: time vector t [n dimensional numpy vector] data vector y [n dimensional numpy vector] (values correspond to t vector) threshold tr [...
1
vote
0answers
41 views

Get stacked game state in NHWC format

After reading this, I decided to transition my DQN code from the keras library to tf.keras library (code is located in this repo) and my original code used NCHW format, as it was faster with GPUs. As ...
6
votes
1answer
181 views

Rotating an Image using own algorithm in python

For an uni assignment I have been giving the task of making my own rotating algorithm in python. This is my code so far. ...
3
votes
2answers
63 views

Stroke Algorithm in python

Here is my next Numpy practice, I am not sure is it more accurate to call it "Stroke Algorithm" or "Draw edge stuff", xD. Anyway I did in steps: calculate each pixel's RGB absolute distance to ...
3
votes
1answer
2k views

Simplex method (linear programming) implementation

We've implemented a version of the Simplex method for solving linear programming problems. The concerns I have are with the design we adopted, and what would be some refactorings that would improve it ...
1
vote
0answers
28 views

Converting 2D arrays into strings

I have this function which converts a couple of 2D NumPy arrays into strings and then saves them in a text file (grid, quality, <...
1
vote
1answer
72 views

Renting vs. owning comparison calculation

I have working code that compares rental prices and owning costs, given some parameters. It works, but it surely does not look good. I hadn't worked with time-series before and I wonder how I could ...
12
votes
1answer
180 views

Replace color in image measured by Euclidean distance

This script replaces the red hair in image to black color (replace one color to another) Three major parts in this script Not just replace color red to black, I hope the output image looks natural, ...
2
votes
2answers
89 views

Genetic Algorithm - heavy memory usage

I have implemented two pieces of code as part of an Evolutionary Algorithm study: Particle Swarm Optimisation (PSO) and Genetic Algorithm (GA). Both are designed to find an optimal solution to a ...
0
votes
0answers
59 views

Get directions for unit movement with conditions

I am developing a bot for Starcraft II. In my code for unit movement, I often find myself doing this: Calculate points around the current position of the unit Check if these points are valid and can ...
12
votes
1answer
985 views

Pretty printing of the numpy ndarrays

Following this idea for pretty printing of numpy ndarrays, I have developed a very primitive prototype: ...
5
votes
1answer
91 views

process data with numpy seems rather slow than pure python?

In my opinion, Vectorization operation with numpy should be much faster than use for in pure python. I write two function to get and process data in a csv file, one in numpy and another in pure python,...