NumPy is the fundamental package for scientific computing with the programming language Python.
1
vote
0answers
8 views
Calculating speed from a Pandas Dataframe with Time, X, and Y columns
I'm trying to calculate speed between consecutive timepoints given data that is in a '.csv' file with the following columns: "Time Elapsed", "x", and "y". The ultimate goal is to get the data into a ...
4
votes
0answers
36 views
Python Code For Drawing a Fractal Tree using pyglet
So I've just finished doing a coding challenge that involved drawing a fractal tree. Instead of doing it recursively(which is what I did initially but it proved to be to slow for branches greater than ...
5
votes
1answer
45 views
Increase performance of Bayesian likelihood equation
I need to calculate a Bayesian likelihood in its negative logarithmic form:
$$-\ln L = N\ln M -\sum\limits_{i=1}^{N} \ln\left\{P_i \sum\limits_{j=1}^M \frac{\exp\left[ -\frac{1}{2}\left(\sum_{k=1}^D \...
3
votes
0answers
32 views
Simple finite difference employing Maccormack technique
I am relatively new to programming and while I am fairly comfortable with the math, I cannot say with confidence that I feel the same way with programming. Anyway here is a simple finite difference ...
3
votes
0answers
36 views
Drawing a mask of the black pixels of an image [closed]
I'm drawing a pixel-by-pixel image with the following code
...
-1
votes
0answers
9 views
7
votes
1answer
69 views
Filling an array with the same data at a regular interval without a for loop
I have some code which generates the coordinates of a cylindrically-symmetric surface, with coordinates given as \$(r, \theta, \phi)\$. At the moment, I generate the coordinates of one \$\phi\$ slice,...
3
votes
0answers
73 views
Subtract multiple columns in PANDAS DataFrame by a series (single column)
Background
I have tons of very large pandas DataFrames that need to be normalized with the following operation; log2(data) - mean(log2(data))
Example Data
The example DataFrame ...
8
votes
2answers
77 views
Extract unique terms from a PANDAS series
Background
I have process tons of DataFrames with shapes of ~230 columns x ~2000-50000+ rows. Here is an extremely simplified example;
...
6
votes
3answers
255 views
Karplus Strong pluck generation
I want to use a simple implementation of the Karplus-Strong algorithm to generate pluck sounds and chords.
My existing code seems correct but is slow when more than a few plucks are being generated. ...
6
votes
0answers
35 views
Pandas code for calculating distance and time between waypoints for large files
This is very similar to other code I've posted, however this is designed for very large csv files, for example 35gb.
The files typically look like this:
...
8
votes
2answers
104 views
Curried function
From the question
and since I'm currently learning functional programming I was inspired to write the following (curried) function:
...
5
votes
1answer
84 views
Clustering points on a sphere
I have written a short Python program which does the following: loads a large data file (\$10^9+\$ rows) where each row is a point on a sphere. The code then loads a pre-determined triangular grid on ...
8
votes
1answer
118 views
Efficient extraction of patch features over an image
Input:
data - a matrix of size n*m*3*3 (complex values)
indices - a list of coordinates (x,y), where x < n and y < m
fp - a feature parameter which is a tuple of ((fp11, fp12), (fp21, fp22)), ...
6
votes
0answers
110 views
Calculating T-Test within Large Pandas Dataframes
The below code runs a t-statistic within a large dataframe (rnadf) based on masked values from another dataframe (cnvdf_maked). ...
0
votes
1answer
39 views
Calls one column of a dataframe, turns it into an array and plots it
My code calls 1 column of a dataframe, turns it into an array and plots it.
I want to able to do this for all the columns without having to repeat the code many times.
How can I do this?
...
3
votes
2answers
63 views
Pairwise distance and residual calculation
I have a code for calculating the pairwise distances and the residuals of my data (X, Y, Z). Data is quite large (average of 7000 rows) and so my interest is code efficiency. My initial code is
...
3
votes
0answers
159 views
Implementing the stochastic gradient descent algorithm of the softmax regression with only NumPy
I am implementing the stochastic gradient descent algorithm. I think there is plenty of room for improvement.
...
4
votes
1answer
143 views
Runge-Kutta 4th order using Python numexpr.evaluate()
I am implementing an ODE solver, where the user provides rates and coefficients as a string. ODE solver has to work with vectors. The best implementation I got so far is the following:
...
3
votes
2answers
74 views
Crank-Nicolson. Advection - Diffusion
I am writing an advection-diffusion solver in Python. I am quite experienced in MATLAB and, therefore, the code implementation looks very close to possible implementation in MATLAB. I implemented the ...
4
votes
2answers
68 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 ...
3
votes
1answer
105 views
Multivariable Gradient Descent in Numpy
Just recently started learning ML, first I've gone through the notes of Ng's Coursera stuff. While I have nothing against Octave, I'm trying to solve exercises in Python. It's my beginning with that ...
5
votes
2answers
134 views
Split latitude/longitude by degree to make file names and folder directory names
This code works and does exactly as I want, but I am wondering if/how it could be done faster/more efficiently? This runs quickly on a demo file, but bogs down when I introduce much larger files (3-...
2
votes
1answer
54 views
Taking in data from several large files
I am working on a script in Python 3.5 that will take in data from several large files. The script takes a while to run, so I am trying to optimize the functions that take the longest. In particular, ...
5
votes
2answers
70 views
A command-line utility to solve a system of linear equations
I was re-checking my answers for a Math paper on my laptop. I needed to verify my solution for a system of linear equations. Unfortunately, I found no quick way to do that. That's why I wrote a (...
10
votes
1answer
78 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.
...
1
vote
1answer
29 views
Plot on a 2D grid a spherically symmetric function
I am trying to compute a spherically symmetric function on a 2D grid. The simple solution that I came up with is the following:
...
4
votes
2answers
180 views
Randomly generate a list with predetermined mean
Aim: randomly generate \$n\$ numbers between \$a\$ and \$b\$ with (roughly) mean \$m\$.
Problem: The simple beginner's code I wrote becomes very inefficient as \$m\$ moves away from \$\frac{a + b}{2}\...
3
votes
1answer
78 views
Analyzing pair by pair DNA sequences with for loops in Python
Here is a code I would need to make far faster: it is made to analyse more than 3000 DNA sequences of more than 100 000 characters each.
The matter is I have to compare it by pair, so that would be ...
2
votes
0answers
43 views
Sorting overlapping shells in dynamical simulation - for loops vs array methods
I didn't know whether it was best to start with the general task I'm trying to solve, or the meat of the programming problem. They're both below but the former is rather long and solving the latter ...
1
vote
1answer
95 views
2D lattice with fixed boundary condition
I am writing code for the following equation with fixed boundary condition on a 2 dimensional lattice of \$L\times L\$ sites:
$$\begin{align}
x_{i+1} =&\ (1-\varepsilon)r\, x_i (1-x_i) + \\
...
4
votes
0answers
79 views
Generalized NumPy reshape function to imitate einsum syntax
I often perform a lot of reshape(), transpose() or squeeze() operations on ...
4
votes
1answer
46 views
Python script to store nd array into images
I am trying to write a generic Python script that do the following types of tasks:
Load .npy file (the .npy file is of shape (m_samples, channels, row, column), which corresponds to ...
12
votes
3answers
296 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 ...
3
votes
2answers
87 views
Plotting a 2D iso-probability curve
I have a library function that returns a pair of two numbers like this
def library_function(x0, x1):
return x0**2 + x1**2, 10*x0*x1 + 1
(Actually, it is a ...
2
votes
0answers
57 views
Computing the weighted centroid dependent on previous row/column
I am working on a project for a Raspberry Pi that requires some image processing.
The aim is to find a white line on a black background by finding the weighted mean in each row/column. However to ...
0
votes
1answer
180 views
Cosine similarity computation
I have a matrix of ~4.5 million vector [4.5mil, 300] and I want to calculate the distance between a vector of length 300 against all the entries in the matrix.
I got some great performance time ...
2
votes
0answers
59 views
Optimize iteration over numpy array with vectorize [closed]
I have the following method which deals with numpy arrays to find the cumulative minimum from each element and its neighbors above:
...
18
votes
0answers
340 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 ...
3
votes
1answer
83 views
Compressing time series by removing repeated samples
I work on a project with time series data. So there are samples (\$y\$), and each sample has a timestamp (\$x\$). The data will be visualized, but often there are time series which contain samples ...
4
votes
2answers
82 views
Vectorizing a pixel-averaging operation in Numpy
I am reading from a file containing some segments (irregular parcels of the image) and trying to average the entire segment to have one pixel value. This is the code I use:
...
3
votes
0answers
40 views
Speed up a Gibbs sampler with Cython
I have implemented a Gibbs sampler to generate textured images. According to the beta parameters (array of shape(4)), we can generate various textures.
Here is my ...
4
votes
2answers
161 views
Intersection of N lists, or of an N-d array
I often find myself having to do np.intersect1d() multiple times because I need to find the intersection of a number of list like or array like things. So I ended ...
2
votes
2answers
92 views
Generating fractals and space filling curves, using a general function for L-systems
An L-system is a rewriting system that can be used to generate fractals and space filling curves, because of its recursive nature.
Some L-systems for mathematical curves can be found here.
An ...
3
votes
3answers
116 views
5
votes
2answers
79 views
Python Cartesian Product in a constrained dictonary
I want to calculate the Cartesian product of n copies of a small list, marker=[0,1,2]. I want to use these Cartesian product ...
5
votes
1answer
76 views
Calculating a table of deBroglie wavelengths for various electron energies
Here is the formula for the deBroglie wavelength of an electron versus its kinetic energy:
$$ \lambda(E_k) = h\left/\sqrt{\frac{(E_k+m_eC^2)^2-m_e^2C^4}{C^2}}\right.$$
and here is simple script that ...
3
votes
1answer
244 views
OpenCV 3: Using k-Nearest Neighbors to analyse RGB image
I'm new to computer vision and numpy.
I wrote a simple script to seperate red, green and blue colors from the original image by using the kNN algorithm.
After reading through some numpy tutorials, I'...
2
votes
1answer
37 views
Find signal's maximum peak in window
I have a 9-dimensional signal (as a csv from this Gist) that looks like this:
A signal peaks every 30 steps. I want to get the maximum values of the peaks in that ...
6
votes
1answer
198 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?
...