Tagged Questions
NumPy is the fundamental package for scientific computing with the programming language Python.
3
votes
2answers
38 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
...
4
votes
1answer
77 views
+50
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:
...
2
votes
2answers
47 views
Crank-Nicolson. Advection - Diffusion. Perfomance
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
54 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
52 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
128 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
51 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
51 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
73 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
27 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
170 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
74 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
42 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
80 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) + \\
...
3
votes
0answers
50 views
Generalized NumPy reshape function to imitate einsum syntax
I often perform a lot of reshape(), transpose() or squeeze() operations on ...
3
votes
1answer
35 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 ...
11
votes
3answers
286 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
74 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
47 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
114 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
41 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:
...
13
votes
0answers
255 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
68 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
75 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
36 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
152 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
67 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
110 views
5
votes
2answers
68 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
75 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
121 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
34 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
184 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?
...
1
vote
1answer
56 views
Counts instances for each category for many Matlab files
This is for machine learning. I have 5 matlab files: AWA, REM, S1, S2, SWS.
Each one has 2 arrays: x and y. ...
2
votes
1answer
65 views
Generate a random discrete signal
I currently have this code to generate a discrete signal with random amplitudes, but a fixed frequency:
...
8
votes
1answer
116 views
Monty Hall Optimization in Python 3.5
This is for homework but the deadline is over and I know the implementation is correct because it concurs with the math. At this point I just want to check my implementation for potential ...
4
votes
1answer
42 views
Surface Density Profile
I am trying to compute the surface density profile, given the spherical density profile in 3D for different parameters in order to interpolate and have it as a function of them to use later on for ...
4
votes
1answer
164 views
Conditional removal of columns in sparse matrix
I have a large dataset (78k instances x 490k features) that is loaded as a scipy.sparse.csr_matrix format. From this dataset I want to filter certain features (i.e. ...
5
votes
0answers
105 views
Two versions of Sudoku
I have written two versions of a sudoku solver. Both can solve 9x9 sudoku boards in <200 ms. One of the implementations uses numpy. I am looking for feedback on both, but particularly on why my ...
3
votes
2answers
68 views
Returning a NumPy array (or list) of strings of words repeated N times
I have a list of \$n\$ words, and a corresponding \$m \space x \space n\$ frequency matrix (as a NumPy array). I would like to return a list/array of strings of length \$m\$ where the \$m\$th string ...
4
votes
2answers
80 views
Export data from Python to spreadsheet
I have written the following code to export to data to a spreadsheet with this result:
Please let me know how to improve it.
...
1
vote
1answer
42 views
Mean of combined sublists with different lengths
I have a seemingly simple issue. Consider this list:
a = [[12.0, 5.0, 63.0], [0.1, 2.0, 7.1, 3.0, 2.3, 5.0, 8.4]]
I want to find the mean (using ...
5
votes
0answers
85 views
Computing intersections of a Polyline and a line
I wrote a script to compute / interpolate the nodes of a Polyline at a given latitude, input_lat. It works for my purpose but I am wondering if there is a better, ...
1
vote
1answer
139 views
Sum over selected numpy.ndarray column and write to a file
I am trying to write the sum of selected NumPy array column and write to a file.
The following code snippet is working fine:
...
7
votes
2answers
2k 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 ...
2
votes
2answers
2k views
Plot heat map from csv file using numpy and matplotlib
There's a csv file with format:
x0, y0, v00
x0, y1, v01
...
x1, y0 v10
...
And what I want to do is to plot a heat map, in which at location (x, y) the value v ...
3
votes
1answer
69 views
Scoreboard with score evolution plots using classes
I've very recently started using OOP paradigms for the code in my work (academic research). As an exercise I tried coding this quick scoreboard.
I'm not interested in improving the 'display' part, I ...
5
votes
0answers
164 views
Calculating complex exponential numbers using OpenMP in Cython
I am trying to create a function that can either beat numexpr or perform comparably for the vectorized mathematical operation ...
3
votes
2answers
97 views
Assembling edges of a graph
I'm making a neural network that comprises five populations of feature-selective neurons and one population of non-selective neurons. Each neuron in this network receives connections from
c * f * ...
7
votes
2answers
368 views
Histogram of a string
I'm teaching myself Python and when a friend posted this sentence
Only the fool would take trouble to verify that his sentence was
composed of ten a's, three b's, four c's, four d's, forty-six e'...