Questions tagged [numpy]
NumPy is the fundamental package for scientific computing with the programming language Python.
1
vote
1answer
17 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
50 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
42 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
111 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
36 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
34 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
45 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 ...
3
votes
1answer
41 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
49 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
296 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
280 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
75 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
51 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
42 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
121 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
63 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
69 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
48 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
26 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
75 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
77 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
14 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
34 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
35 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
119 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
55 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
542 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
65 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 ...
10
votes
0answers
120 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
82 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 ...
11
votes
1answer
470 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
82 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,...
2
votes
0answers
162 views
Better way to set number of threads used by NumPy
Background
When NumPy is linked against multithreaded implementations of BLAS (like MKL or OpenBLAS), the computationally intensive parts of a program run on multiple cores (sometimes all cores) ...
-1
votes
1answer
60 views
Marking a rectangular region in a NumPy array as an image mask
I'm currently working on creating a mask for an image. I have initialized a two-dimensional numpy zeros array. I now want to replace the values of the mask corresponding to pixels following some ...
12
votes
1answer
1k views
Looking for a way to create a particular matrix in python in a less amount of time
I want to create a matrix like this in Python:
$$
\begin{pmatrix}
0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 2 & 2 & 2 & 2 & 3 & 3 & 3 & 3\\
0 & 1 &...
1
vote
0answers
38 views
Live plot with many subplots
I have a stream of sensor data which I want to visualize in a plot with many subplots. Plotting the data is a real bottleneck in my code. Right now I get with small resolution only 16 FPS which is far ...
1
vote
0answers
159 views
Gray-level co-occurrence matrix feature computation
I have many images and want to compute the GLCM properties for every image. Below my code that runs many hours to complete the task:
...
2
votes
3answers
74 views
Speed up weighted average for Leela Chess Zero
Problem: Given a vector of about 40 values m with normal error sd compute the weighted average of the values weighted by the ...
4
votes
1answer
59 views
Loops through each row in a binary image and gets the width of each group of black or white pixels
Example image:
Code:
...
3
votes
0answers
57 views
Variable bit length lossy floating point compression
I am implementing a new compression algorithm for the weights of a neural network for the Leela Chess project. the weights are roughly 100Mb of ...
3
votes
1answer
230 views
k-means using numpy
This is k-means implementation using Python (numpy). I believe there is room for improvement when it comes to computing distances (given I'm using a list comprehension, maybe I could also pack it in a ...
1
vote
1answer
39 views
Centre all columns of an array
I have this working code tested over lots of np.arrays. It is a for-loop that walks through all the values of a np.array (x,y).
For each y-row, it finds the first x-column for which the value is ...
4
votes
1answer
62 views
Python program computing some statistics on Scottish geographic areas
This simple script computes some basic descriptive statistics, like mean, standard deviation, kurtosis, etc. on data column imported from a CSV file with use of ...
0
votes
1answer
35 views
Modeling a capacitor with DC Bias
I am modeling a capacitor which has its capacitance varying according to the DC Bias. The DC Bias is computed by taking the mean of the voltage across the capacitance.
...
1
vote
0answers
37 views
Applying a filter to a large array with elements that are not regularly spaced
I need to apply a Gaussian filter to a 2D numpy array where the distance between adjacent array elements depends on the row of the array. (Specifically, the data are evenly spaced in latitude and ...
3
votes
1answer
59 views
Statistics and calculations
I went through a series of lessons in data science using pandas and numpy.
I have attempted to replicate some of the more common algorithms based on maths forumlas and psuedocode, except for the <...
3
votes
1answer
74 views
Centered finite difference approximation for edge enhancement
I implemented a centered finite difference approximation for edge enhancement in images. The difference approximation has a step size h of the partial derivatives in the x- and y-directions of a 2D-...