Questions tagged [numpy]

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

Filter by
Sorted by
Tagged with
39
votes
2answers
68k views

Finding the closest point to a list of points

I'm trying to find the closest point (Euclidean distance) from a user-inputted point to a list of 50,000 points that I have. Note that the list of points changes all the time. and the closest distance ...
23
votes
1answer
1k views

Backpropagation in simple Neural Network

I've been working on a simple neural network implemented in python. Currently, it seems to be learning, but unfortunately it doesn't seem to be learning effectively. The graph below shows the output ...
22
votes
3answers
3k views

Remove points in a straight line

I wrote this code to remove points that create a straight line when plotted. For instance, this has many points in a straight line: ...
22
votes
2answers
15k views

Sending a C++ array to Python/NumPy and back

I am going to send a C++ array to a Python function as NumPy array and get back another NumPy array. After consulting with NumPy documentation and some other threads and tweaking the code, the code is ...
20
votes
1answer
570 views

Remapping and interpolating 255x1024 array

I have a function that accepts a \$255\times1024\$ array and remaps it to another array in order to account for some hardware related distortion (lines that should be straight are curved by the lens). ...
19
votes
2answers
6k views

Finding a zero crossing in a matrix

I am trying create an algorithm for finding the zero crossing (check that the signs of all the entries around the entry of interest are not the same) in a two dimensional matrix, as part of ...
17
votes
1answer
302 views

Solve the phase state between two haplotype blocks using markov transition probabilities

I have spent about more than a year in python, but I come from biology background. I should say I have got some understanding of for-loop and nested-for-loop to workout the solution to the problem I ...
16
votes
3answers
25k views

Crop black border of image using NumPy

Objective: Crop the image so only the number stays in the image Problem: Slow Performance I have code that crops an image. The image pixels are 0 or 255. There are no values between. The ...
16
votes
2answers
79k views

Fastest way to iterate over Numpy array

I wrote a function to calculate the gamma coefficient of a clustering. The bottleneck is the comparison of values from dist_withing to ...
16
votes
1answer
371 views

Warhammer: How many of my attacks will succeed?

Me and a couple of mates sometimes play a game called Warhammer. When playing the game you have options of what each model attacks. This can lead to situations where you know if you shoot with 100% of ...
16
votes
2answers
390 views

MNIST Deep Neural Network in TensorFlow

I have been working on this code for a while and it gave me a lot of headaches before I got it to work. It basically tries to use the MNIST dataset to classify handwritten digits. I am not using the ...
15
votes
3answers
9k views

Processing an image to extract green-screen mask

I am using OpenCV to process an image, and in my code, I have to check / edit each pixel separately: ...
15
votes
3answers
4k views

Orbital Trajectory simulator

I have written a simple program to do trajectory simulation in the Earth-Moon system, it still has a long way to go I am working on making it more class oriented and am looking into implementing a ...
15
votes
1answer
1k views

Plot timings for a range of inputs

When writing Code Review answers, it becomes often necessary to measure how long the modified code takes vs how long the OP's code takes. I needed a nice way to visualize this as a function of the ...
14
votes
3answers
469 views

Plotting different parameterized polynoms

For a university assignment I had to plot different polynomial functions depending on one single parameter. That parameter gave the number of supporting points to interpolate a given function in the ...
14
votes
2answers
8k views

Possible optimizations for calculating squared euclidean distance

I need to do a few hundred million euclidean distance calculations every day in a Python project. Here is what I started out with: ...
14
votes
2answers
442 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 ...
14
votes
1answer
484 views

Racetrack plotter

My Racetrack is just that. A Racetrack. You can't race it (yet) because I had trouble with collision detection, but I wanted to share it anyway. It creates a base polygon by using ...
14
votes
1answer
796 views

Plotting polynomials roots

NOTE: See follow up to this question here I created a simple python script to plot quadratic, cubic and quartic polynomials with integer coefficients between -4 and 4. It uses numpy to find the roots ...
14
votes
0answers
268 views

Efficiently selecting spatially distributed weighted points

Background: Motivation behind writing the following code is originated in the area of computer vision. More specifically – image rectification. In order to obtain rectified images, one has to find a ...
13
votes
1answer
5k views

Faster computation of barycentric coordinates for many points

I'm just starting to understand the Python syntax and I created a module that does what I wanted, but really slow. Here are the stats of cProfile, top 10 ordered by ...
13
votes
2answers
715 views

Numerics for a game theory calculation using expected utility

I am trying to replicate Bruce B. de Mesquita's (BDM) results on political game theory for prediction. Based on where actors stand on issues, their capabilities, salience, BDM's method attempts to ...
13
votes
1answer
712 views

Temperature Interpolation

I want to interpolate temperature in correlation with height. I have temperature data from stations with coordinates and height in this format: ...
13
votes
1answer
1k views

Pretty printing of the numpy ndarrays

Following this idea for pretty printing of numpy ndarrays, I have developed a very primitive prototype: ...
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 &...
12
votes
1answer
569 views

A big “Game of Life”

Our quest: Create a big simulation for Conway's Game of Life, and record the entire simulation history. Current Approach: Cython is used for an iterate method. The ...
12
votes
1answer
192 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, ...
12
votes
1answer
185 views

Calculate the closest point to many hyperbolic paraboloids

In this question I asked for a way to compute the closest projected point to a hyperbolic paraboloid using python. Using the iterative approximation answer, I'm able to use the code below to ...
11
votes
1answer
1k views

Balanced Random Assignment in Python

It has been a very long time since I've used Python. What I'm looking for: I would like to create a 6-by-6 random matrix where each component is either 1 or 2, so that there are 18 ones and 18 twos. ...
11
votes
2answers
8k views

Calculating the joint probability of n events from a sample sequence of occurrences

I'm writing an algorithm to take in a sample list of sequences of events, calculate 1-step transitional probabilities from the sequences, forward or in reverse, then calculate the joint probability of ...
11
votes
1answer
1k views

Python program that draws the Mandelbrot set fractal

I wrote a Python program that draws the Mandelbrot set fractal. However, the program is very slow. I would appreciate any feedback on the program, but I would especially appreciate feedback on how to ...
11
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 ...
11
votes
3answers
626 views

Fast Python spring network solver

I wanted a very simple spring system written in Python. The system would be defined as a simple network of knots, linked by links...
11
votes
1answer
4k views

Simple Phase Locked Loop

Here is a simple Phase Locked Loop, which is a circuit used in radio communications for synchronisation between transmitter and receiver. The loop works by calculating the (phase) difference between ...
10
votes
3answers
2k views

Table of Tribonacci sequence using NumPy and PANDAS

What I am trying to accomplish is calculate the Tribonacci numbers and the ratio. I can do this in Excel easy like this. So I tried do the same using Python 3 now. It works and gives such output. ...
10
votes
2answers
340 views

Approximate (250 over 100) permutation best fitting certain criteria

Given a list of 250 words of 4 letters each, what is the fastest way, in Python, to find a subsample of 100 words (drawn without replacement) so that the distribution of letters across the whole ...
10
votes
2answers
26k views

Scale Numpy array to certain range

As I've described in a StackOverflow question, I'm trying to fit a NumPy array into a certain range. Here is the solution I currently use: ...
10
votes
2answers
1k views

A 3-D vector class built on top of numpy.array

I wanted a convenient class to easy access the parameters inside. I am using a lot of math in my game; that's why a wanted to access them through x, ...
10
votes
2answers
3k views

Predator Prey Simulation

Below is a simple random walk predator prey simulation that is optimized to the best of my abilities. I would love to hear about any improvements that can made. ...
10
votes
1answer
8k views

Looping over pixels in an image

I have the following code which iterates over all pixels of an image and does some manipulations on two images of the same size. I would like to speed it up and to avoid iterating over the positions ...
10
votes
2answers
191 views

Improve performance of math function applied to arrays

This block of code is already somewhat optimized thanks to some answers given over at Stack Overflow. In the last question I made (Improve performance of function without parallelization) the code was ...
10
votes
1answer
2k views

Game of Life with NumPy

I started this exercise with NumPy with a goal to find neighbors and return the new matrix. I want to get your feedback. Here's an example from this website. It looks like it's \$O(N^2)\$, and I'm ...
10
votes
1answer
142 views

Game of 15 - A* search

I would be really thankful for review of this homework. I'm trying to write better code in terms of readability and design patterns. This program solves the classic 15 slider puzzle with A* search. ...
10
votes
1answer
176 views

Interpolate then maximize

I need to optimize the following function: $$ \max_{a',\ m'}\ f(a, m, e, m', a') $$ I have approximated \$f\$ with a grid \$F\$, which has a shape \$(nA, nM, nE, nM, nA)\$. Now, I want to ...
10
votes
1answer
1k views

Parallelizing some model fitting code using multiprocessing

I'm trying to refactor some code for fitting parametric models that are defined symbolically using Theano. My goal is for all models to expose a common interface so that, as far as possible, they can ...
9
votes
1answer
24k views

Efficient numpy cosine distance calculation

I want to calculate the nearest cosine neighbors of a vector using the rows of a matrix, and have been testing the performance of a few Python functions for doing this. ...
9
votes
1answer
13k views

K-Mean with Numpy

I have implemented the K-Mean clustering Algorithm in Numpy: ...
9
votes
3answers
846 views

KenKen solver - Python

Here is my code for the KenKen puzzle: The user must fill in each embolded regions with numbers in the range 1-n where n is the board dimensions (e.g: 4*4, n=4) ...
9
votes
2answers
216 views

Univariate linear regression from scratch in Python

I am relatively new to machine learning and I believe one of the best ways for me to get the intuition behind most algorithms is to write them from scratch before using tons of external libraries. ...
9
votes
2answers
263 views

Curried function

From the question and since I'm currently learning functional programming I was inspired to write the following (curried) function: ...