Questions tagged [numpy]
NumPy is the fundamental package for scientific computing with the programming language Python.
765 questions
4
votes
4
answers
1k
views
10
votes
3
answers
2k
views
A Rubik's Cube game in Python
I implemented this Rubik's Cube game in Python, using Canvas and Context2d for user interaction. This was mainly an exercise in &...
4
votes
1
answer
367
views
Half-hexagon neighbour identification under (novel) hierarchical hexagonal grid
Context & Background
Please let me remind you that this project may involve original research; it is important to remember that all content on this site is subject to CC BY-SA 4.0. It is a breach ...
5
votes
1
answer
155
views
backward induction algorithm computation
Is there a way to significantly speed-up this code?
I'm solving a dynamic programming model using a backward induction algorithm. A crucial step is to calculate the current-period value function (VF), ...
4
votes
3
answers
388
views
numpy array/item handling functions
Recently I have been using numpy arrays, which have great utility via their broadcasting methods.
I am attempting to write a useful public facing library, and this ...
-2
votes
1
answer
106
views
Intersect two arrays in python
I have two unidimensional numpy arrays in python, gs and ni. I want to intersect them but preserving the order in gs.
When they have high cardinality this operation becomes so costly. Is there any ...
6
votes
1
answer
460
views
Implementing a joint differential equation and eigenvalue solver
I've written a Python script to solve a system of coupled differential equations and then use the results to solve an eigenvalue problem. The full problem is:
\$\frac{d^2}{dr^2} K(r) = \frac{K(r)}{r^2}...
9
votes
2
answers
516
views
Correctly sum pixel values into bins of angle relative to center
A quick way to try to do this would be to use scipy.ndimage.map_coordinates, mapping radius to one axis of a rectangular array, and angle to the other, then summing along the radius axis.
But, I think ...
2
votes
1
answer
87
views
Building Multi Period Linear Programming Matrices
I'm solving a linear programming problem, made of many small problems with some common shared constraints, meaning that they are all together.
Each individual unit has constraints unique to the unit ...
2
votes
2
answers
303
views
Infinite 2d block world (2d minecraft) with chunk system, physics and slower loading
Follow up from this question. I have added a slower loading system that allows the main game to run faster but I still get under 30 fps with caves even if I allow the player to outrun the loading. I ...
4
votes
1
answer
214
views
Finding specific promotions from two columns [closed]
I'm trying to build a function that identifies those who are promoted into a list of jobcodes, or are promoted within that list of jobcodes.
Initially I was using ...
1
vote
1
answer
105
views
Replace iterrow loops in pandas matrices with something else to shorten the running time
This post is modified from this one: https://codereview.stackexchange.com/posts/292885/edit (Alternatives to iterrow loops in python pandas dataframes).
I have a piece of code to calculate price ...
6
votes
2
answers
748
views
Alternatives to iterrow loops in python pandas dataframes
I have a piece of code to calculate price sensitivity based on the product and its rating.
Below is the original data set with product type, reported year, customer’s rating, price per unit, and ...
2
votes
1
answer
58
views
Maintain a log containing values if certain conditions are met
I'm trying to capture profits and set a stop loss in my trading strategy. I want the stop loss to be set daily based on the past data and if the current price, i.e., price for the date falls below the ...
5
votes
2
answers
319
views
Srivastava multivariate Fox H function in MATLAB
I was trying to rewrite the Python code in MATLAB.
The result is consistent.
But, the MATLAB code is so slow. Any help would be appreciated.
Ref python code link
The MATLAB code written by me is as ...
4
votes
1
answer
90
views
Array Math to Calculate Ice Thickness -- Need Help Simplifying
I posted this on Stack but since it's working code, it was suggested I post it here. I wrote this code to calculate ice thickness using the following equation from Stephenson, 2011.
It's a daily ...
3
votes
5
answers
2k
views
How do I speed up this simulation program
This is a program to solve a differential equation numerically using Euler method. As of now, it is very slow, and I need to run 10000 Monte Carlo simulations.
The differential equation is called ...
3
votes
1
answer
129
views
"Flattening" a 2D STM image by subtracting an Nth order polynomial without typing out all the terms for each N?
I have some scanning tunneling microscopy (STM) topography images that I need to flatten. I have found that for some data I need a 4th order polynomial, but in other cases a lower order works better.
...
10
votes
3
answers
1k
views
Function that returns activation function, as well as its derivative
I have the following function that returns one of several functions along with their derivative for training neural networks.
Can this function be improved and made more pythonic in any way?
Are there ...
4
votes
1
answer
142
views
Finding the earliest time we can schedule
We have a list of \$j \in \{1,\ldots,N\}\$ jobs with a processing time \$p_j\$ and a demand \$d_j\$ between 0 and 1. These are real numbers. Jobs require the fixed demand over the entirety of their ...
-1
votes
1
answer
63
views
Python manual SVD only working for some matrices - how to stabilize it? [closed]
I use the following code for a manual single value decomposition using numpy. Depending on the array I choose it sometimes works out well and I can verify the svd and sometimes it does not work out ...
5
votes
2
answers
538
views
14
votes
1
answer
1k
views
Text-mode 2048 in Python (using numpy)
Good evening. I am studying mathematics at the moment, so I have little to no formal education in actual computer engineering. However, I am trying my hands at learning Python because I will need lots ...
4
votes
1
answer
121
views
Covariance of random variables of Dirichlet distribution
I don't find an implementation in the library I work with of covariance function:
I give a try at implementing it in Python:
...
3
votes
1
answer
118
views
3ph 2l inverter simulation with SPWM
Here is a code for a simple model of three phase two level inverter with constant DC voltage source and three phase RL load.
...
2
votes
0
answers
83
views
GUI Tic-Tac-Toe game with six AI players - part 2: the styling
This question is part of a series of posts about my massive Tic Tac Toe game with AI players, the previous question is: GUI Tic-Tac-Toe game with six AI players - part 1: the UI. This question ...
0
votes
2
answers
161
views
Optimize a Python code which indicates duplicated values in an excel file [closed]
I wrote this code to indicate duplicated values. It actually works but I hope to know if there's another possible solution to optimize this process. Thanks.
...
4
votes
2
answers
190
views
NumPy script to convert BGR to HSL and back
This is a NumPy script that converts BGR arrays to HSL arrays and back, without using OpenCV. Input and output values are arrays of 3 dimensions with values ranging from 0 to 1, the shape of the ...
5
votes
1
answer
102
views
Generating Abelian sandpile "zeros" in python
Here is some code that generates the zero element of an Abelian sandpile model (ASM), for any given model dimensions, and then plots the result as a colormesh. Here is a wiki page explaining the ASM. ...
0
votes
1
answer
263
views
Find the first value bigger than a threshold
This function takes two inputs: A is 2D (N,5) while B is 1D (N).
It tries to find the smallest ...
5
votes
3
answers
318
views
Fast complex absolute argmax in Cython
I'm thinking I implemented it optimally, but somehow it's much slower than what should be much slower, np.argmax(np.abs(x)). Where am I off?
Code rationale & ...
2
votes
1
answer
210
views
Is this the right implementation for Linear Programming (puLP) on python?
I have created a LP function to help maximize a set of features. My first time playing with this library and also conducting LP.
Variables:
Number of features => X
Number of Categories => Y
...
4
votes
3
answers
165
views
Calculate length of continuous gaps
I have a list with some nan values, out of which some are continuous, e.g.:
list = [1, 2, 3, np.nan, np.nan, 6, 7, np.nan, 9, np.nan,np.nan,np.nan, 12]
For some ...
6
votes
1
answer
168
views
basic Hartee-Fock program to compute the total energies and some properties of a molecule
I am trying to write a basic Hartee-Fock program that computes the total energies and some properties of a molecule given as an input. The code uses Cython to statically compile the computationally ...
3
votes
0
answers
183
views
Image generator using prime numbers in polar coordinates
Related
This is a Python script that generates images using prime numbers up to a given positive integer, it generates prime numbers using the Sieve of Eratosthenes with some rudimentary Wheel ...
2
votes
0
answers
128
views
Python script that generates images using Thue-Morse sequence
In mathematics, the Thue–Morse sequence, or Prouhet–Thue–Morse sequence, is the binary sequence (an infinite sequence of 0s and 1s) obtained by starting with 0 and successively appending the Boolean ...
6
votes
2
answers
561
views
Means square displacement (MSD)
I have written a code to calculate the MSD of some molecules. The code averages over multiple time origins (sliding time window) and over all the molecules. I have also made it do one extra thing: do ...
2
votes
1
answer
174
views
Parallelization of the tick function in a Game of Life simulation using Rust and Python (+ NumPy)
I've been writing a small Rust module recently with the sole purpose of speeding up my Python program, which is a Conway's Game of Life simulation. The function written in Rust is called from Python ...
2
votes
1
answer
103
views
Readable Backprogragation calculations in Numpy Neural Network
As an exercise we should write a small Neural Network with the following structure:
There should be additionally a bias for each layer and sigmoid should be used as the activation function.
The ...
8
votes
2
answers
557
views
Fourier Series of a given function
This is a very simple code that expresses a function in terms of Trigonometric Fourier Series and generates a animation based on the harmonics. I would like to know some ways to improve the ...
1
vote
1
answer
94
views
Fixing math library functions in Black-Scholes options pricing model
I've amended a code for the Black-Scholes formula for European pricing options found here at the bottom of the page and fixed the math functions accordingly. Unlike the code on the website, mine has ...
1
vote
1
answer
159
views
Unpythonic matrix manipulation
This code does exactly what I want it to, however I want to try and get rid of the nested loop to make it more pythonic. I have been trying to somehow use broadcasting, including playing with ...
3
votes
1
answer
190
views
Solve an option pricing PDE in Python - Part 1 [closed]
The Github repository NM-Heston solves call option prices under the Heston 2-factor model using ADI splitting schemes. I am adapting the code to price options under the 3-factor Heston-Hull-White ...
5
votes
1
answer
171
views
E-car-sharing problem with charging between the bookings
I am writing code that, at some later point, will be part of an academic multi-agent simulation of the learning process of car-sharing users and grid stability. This code deals with finding the price ...
2
votes
1
answer
167
views
Further optimizing the ISING model
I've implemented the 2D ISING model in Python, using NumPy and Numba's JIT:
...
2
votes
1
answer
116
views
Optimize python computation of transition matrix
I have an array of integers labels, for instance array([0, 1, 1, 2, 2, 2, 3, 3, 1, 4, 4, 1, 1, 0, 0, -1, -1]). The array can be ...
2
votes
2
answers
2k
views
Poker hand valuator in Python
As you will probably notice very quickly from the code below, I'm not very experienced coder. Below is my attempt at valuating poker hands in Python. The code below may not be pretty but it seems to ...
2
votes
2
answers
197
views
pixelating images
I'm a hobby programer and I had an idea for pixelating images and I wrote it.
I was wondering how would a better programer write the same function.
I want to learn and I think that this would help me ...
9
votes
1
answer
4k
views
1
vote
1
answer
153
views
How to using custom colormap with pixel calculation, gray scale value by opencv and make it workable on camera [closed]
I tried to implement the mcolor colormap in the paper
at page 8 to 10.
I developed the code to process a image file and it works ok.
Now, I want to adopt this image processing method to the camera ...