Questions tagged [numpy]

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

Filter by
Sorted by
Tagged with
17 votes
1 answer
5k 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 ...
user avatar
  • 40.6k
10 votes
1 answer
14k views

K-Mean with Numpy

I have implemented the K-Mean clustering Algorithm in Numpy: ...
user avatar
13 votes
1 answer
8k 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 ...
user avatar
14 votes
1 answer
758 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 ...
user avatar
  • 12.7k
10 votes
1 answer
5k 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 ...
user avatar
  • 601
6 votes
2 answers
5k views

Python / Numpy running 15x slower than MATLAB - am I using Numpy effeciently?

Here is some code I wrote in Python / Numpy that I pretty much directly translated from MATLAB code. When I run the code in MATLAB on my machine, it takes roughly 17 seconds. When I run the code in ...
user avatar
4 votes
1 answer
82 views

Back to Basics - Tic Tac Toe

I've seen a few TTT related posts recently, and thought I might learn something or be reminded of certain techniques from attempting my own rendition. Here is that attempt! Hopefully any new coders ...
user avatar
  • 2,897
-1 votes
1 answer
53 views

How can i refactor python numpy code? [closed]

...
user avatar
  • 5
48 votes
3 answers
139k 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 ...
user avatar
  • 1,061
11 votes
2 answers
15k views

Generating a 3D Point Cloud

I'm trying to produce a 3D point cloud from a depth image and some camera intrinsics. The image is 640x480, and is a NumPy array of bytes. The output is a (rows * columns) x 3 array of points. I've ...
user avatar
  • 111
15 votes
1 answer
490 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 ...
user avatar
  • 41.7k
13 votes
1 answer
1k 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 ...
user avatar
  • 917
8 votes
1 answer
4k views

Remove pixel patch in image which is stored as array

I have an array I which stores N images of size P (number of pixels). Every image is of size ...
user avatar
  • 1,003
5 votes
1 answer
902 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 ...
user avatar
13 votes
1 answer
897 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 ...
user avatar
  • 475
7 votes
2 answers
7k views

Python, numpy, optimize module to handle big file

With the help of some people here and there, I wrote the module below which works very well if the file targets.csv is not too big. As soon as this file exceeds ...
user avatar
5 votes
1 answer
956 views

Hints to make Sudoku solver more Pythonic

As you will see, I am not very familiar with Python and NumPy but want to learn it. The following code is a very basic Sudoku solver which works fine for simple examples. Although it runs, I still ...
user avatar
  • 958
2 votes
2 answers
4k views

Simulation of 2D elastic balls

Following this SO post and this Wikipedia article, I wrote a Python script to simulate physics of 2D elastic balls. I define the physical behaviour of each ball in the ...
user avatar
  • 329
13 votes
1 answer
301 views

Population dynamic simulation on biological information maintenance 2

This question is the follow-up to this previous question. Background Using this simulation I investigate a system in which enzymes proliferate in cells. During the replications of enzymes, ...
user avatar
12 votes
1 answer
2k 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 ...
user avatar
10 votes
1 answer
31k 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. ...
user avatar
  • 233
9 votes
1 answer
181 views

Population dynamic simulation on biological information maintenance

Background Using this simulation I investigate a system in which enzymes proliferate in cells. During the replications of enzymes, parasites can come to be due to mutation. They can drive the system ...
user avatar
8 votes
3 answers
1k views

Python differential analysis of heat loss across a pipe

I made a differential equation solver to forecast heat loss through a pipe with heat loss coefficient contours. It does what I want it to do but I had to use global variables to change the outputs of ...
user avatar
7 votes
0 answers
156 views

Multithreaded HD Image Processing + Logistic reg. Classifier + Visualization

[I'm awaiting suggestions for improvement/optimization/more speed/general feedback ...] This code takes a label and a folder path of subfolders as input that have certain labels ex: trees, cats with ...
user avatar
7 votes
1 answer
12k views

"Histogram of Oriented Gradients" (HOG) feature detector for computer vision

Here is a function that I've only slightly modified from its original context, found here. Before mentioning anything else, it should be noted that I'm desperately trying to optimize this code for ...
user avatar
5 votes
1 answer
213 views

process numerical input arguments of mixed ints, floats, and "array-like" things of same length>1

I'm trying to process numerical input arguments for a function. Users can mix ints, floats, and "array-like" things of ints and float, as long as they are all length=1 or the same length>1. Now I ...
user avatar
  • 401
4 votes
2 answers
5k views

Calculating if a point is within a polygon, or outside of it

Here is the function (along with its support functions): ...
user avatar
  • 399
4 votes
3 answers
375 views

Estimating the number of tanks based on a sample of serial numbers 2.0

There was some confusion with the code I had posted in my previous version of this question and there was some good advice from @Oscar Smith. The explanation is the same; please give improvements for ...
user avatar
4 votes
0 answers
213 views

Finding where column slices of elements in multi-dimensional array are equal

Skippable Intro: I have a dataset that corresponds to an observed time-series. The dataset is organized into a dictionary - which contains an array of years, an array of months, an array of days, an ...
user avatar
4 votes
2 answers
199 views

Temporal kernel filtering on video frames coming in a loop

I have a video which I read in a loop frame by frame. For every frame, I want to do temporal kernel filtering, the coefficients of which come from the input variable model (a dictionary). The temporal ...
user avatar
  • 159
0 votes
0 answers
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 ...
user avatar
  • 135
7 votes
1 answer
116 views

Weighted phase Laplacian formula

We have the following equation (weighted phase Laplacian): \$c_{i,j} = U(i,j)\Delta^{^x}_{i,j} - U(i-l, j)\Delta^{x}_{i-1,j} + V(i,j)\Delta^{y}_{i,j} - V(i,j-1)\Delta^{y}_{i,j-1}\$ Where \$U(i,j) = ...
user avatar
6 votes
1 answer
451 views

Schelling's model of Segregation Python implementation with Geopandas

If you don't know what is Schelling's model of segregation, you can read it here. The Schelling model of segregation is an agent-based model that illustrates how individual tendencies regarding ...
user avatar
6 votes
1 answer
2k views

Koch snowflake in Python with numpy and pygame

I've drawn a Koch snowflake with Python 3. I'm new to the Python world and would appreciate all feedback on how to make the code more Pythonic as I'm not used to these idioms, styles and modules. All ...
user avatar
  • 265
6 votes
1 answer
986 views

Python! (Snake)

This is now an Iterative Review. Next Iteration Nowhere near a full game yet. Just a basic overview and initialisation of a Snake() class. Thoughts? ...
user avatar
  • 8,800
5 votes
1 answer
1k views

Reading a binary file containing periodic samples

I have the following code for reading HTK feature files. The code below is working completely correct (verified it with unit tests and the output of the original HTK toolkit). ...
user avatar
4 votes
2 answers
1k views

Simple one-hot encoder

Because neither sklearn nor Pandas provide a straightforward and complete one-hot encoder, I decided to write one myself. Both Pandas and sklearn do have an encoder with no option to decode, and the <...
user avatar
  • 263
4 votes
2 answers
180 views

Creating nxm index list of array a

Here is the problem: Given a numpy array 'a' that contains n elements, denote by b the set of its unique values ​​in ascending order, denote by m the size of array b. You need to create a numpy array ...
user avatar
3 votes
1 answer
175 views

Getting angle between all points in array

I have got some code that calculates the angle between all of the points in an array. It works, but it is quite slow. This is because it has complexity O(n^2). It has to loop through the array and ...
user avatar
2 votes
2 answers
2k views

Rolling loop with an array of data points

I have a NumPy array of about 2500 data points. This function is called on a rolling basis where 363 data points is passed at a time. ...
user avatar
  • 183
2 votes
1 answer
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 ...
user avatar
  • 63
2 votes
1 answer
166 views

Simple utility/convenience module - am I doing it right?

I have a large library (four packages) of data processing code that I have written over the last 12 months. I am quite new to python and would like to see if I am doing things correctly. This utility/...
user avatar
1 vote
1 answer
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 ...
user avatar
1 vote
1 answer
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 ...
user avatar
1 vote
0 answers
63 views

Improving code for image manipulation

I'm fairly new to coding and am developing a set of functions to allow mammologists to progrmatically collect data from photos of specimens. The special issue with mammals is that their coats are ...
user avatar
  • 181
1 vote
1 answer
3k views

Data cleaning - am I using classes correctly here?

I have a data processing application that does some data cleaning as the first step. This is the module I import for that purpose. The only 'public' part of the api is the 'clean' function which ...
user avatar