# Questions tagged [numpy]

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

679 questions
Filter by
Sorted by
Tagged with
24 views

### Vectorized code to find the position and length of runs of 1s in a bit matrix

I'm trying to write something like an adjusted run-length encoding for row-wise bit matrices. Specifically I want to "collapse" runs of 1s into the number of 1s in that run while maintaining ...
23 views

### Vectorizing a working custom similarity function further using numpy

I am new to python, and even more new to vectorization. I have attempted to vectorize a custom similarity function that should return a matrix of pairwise similarities between each row in an input ...
50 views

### Fast bruth force numpy array combination

I want to extract combinations of numpy arrays in that way: ...
16 views

### Applying Minimum Image Convention in Python

I am computing pairwise Euclidean distances for 3-D vectors representing particle positions in a periodic system. The minimum image convention is applied for each periodic boundary such that a ...
37 views

### Nearest neighbor search algorithm of quantization

I'm doing some research on Nearest Neighbor Search of quantization, where running speed is very important. As I've just started using Python, I wonder if the following code for multi-processes can be ...
29 views

### Slow calculating sum of individual cells of matrix

I have a 3-D array in numpy, of dimensions 3000 x 2000 x 8 I need to modify the cells of the array so that each cell will contain the sum of all cells above and left of it, inclusive the current cell. ...
326 views

### Optimising Probabilistic Weighted Moving Average (PEWMA) df.iterrows loop in Pandas

I'm implementing the Probabilistic Exponentially Weighted Mean for real time prediction of sensor data in pandas but have issues with optimising the pandas notebook for quick iterations. Is there a ...
49 views

### Snake from the viewpoint of the snake

I wrote a little game of snake that where you can see the field in which the snake moves fixed and you can also see the "viewpoint" of the snake, which is basically calculating the positions ...
24 views

### Convolution layer using numpy in python

I'm trying to make a CNN in python using numpy. I have a finished product but it seems that it can be improved. On testing the convolutional layer is the biggest bottleneck ...
56 views

### Efficient Comparison Of Two Images Using Numpy

I want to compare 2 images using numpy. This is what I have got so far. One of the outputs should be a white image with black pixels where pixels are different. I ...
30 views

### Get a “train or test” slicing elegantly after splitting

I am learning numpy, pandas, and sklearn for machine learning now. My teacher gives me code like below, but I guess it is ugly. This code is for splitting the data into training set and testing set, ...
48 views

### How to structure and write exchangable calculation models (in python3 modules)?

I do research and compare different numeric models to each other. Therefore, I want to easily switch out the numeric models in my simulations and evaluation code. Some models work in cartesian ...
35 views

### Using Numba works slower than not using it for my Python code

I thought this community is better place to ask my question so I ask here rather than at StackOverflow. Recently, I learned that Numba can make Python function ...
311 views

### Genetic algorithm to guess coefficient of a polynomial

I have tried to code a genetic algorithm to guess the coefficients of a degree 4 polynomial. The information initially provided is values of y = f(x) for different x using the original polynomial. I ...
87 views

### Computing the angle between two vectors (vectorized) for small angles and with few copies

I am implementing a function that computes the angle between two vectors when given two n-dimensional arrays and an axis along which to operate. I want to do this with as few copies as possible, and ...
101 views

### Bancroft's method implementation

Background I've written an algorithm to solve the three-dimensional Time Difference of Arrival (TDoA) multi-lateration problem. That is, given the coordinates of n ...
80 views

### Dataframe transformation to numpy ndarray takes ages to complete

I would like to transform my dataframe into an array of fixed-sized chunks from each unique segment. Specifically, I would like to transform the ...
55 views

### Python utility to clean noise from flat text files

I have created simple code in which it takes flat file (txt) and eliminate the noise and error and only return clean rows, i.e. return flat file with the data I am interested in. it works fine but ...
25 views

### Removing Loop from Numpy XOR

I have been playing around with numpy and matplotlib. My little project was to create a scatter plot ranging from -1 to 1 on both X and Y, but where the shading is done with the XOR scheme. The ...
61 views

### Finding distance between vectors of matrices

So here is the problem: Given 2D numpy arrays 'a' and 'b' of sizes n×m and k×m respectively and one natural number 'p'. You need to find the distance(Euclidean) of the rows of the matrices 'a' and ...
27 views

84 views

### Selecting a quantity from a list

I want to implement a function take_upto_n(A, n) such that for e.g. ...
4k views

### Harmonic analysis of time series applied to arrays

I've developed some code in Python to apply a harmonic analysis of timeseries (for satellite imagery data). It's based on this, but then I would like to optimize the performance. So instead of a ...
58 views

### Speed up calculation time of for loop in numpy [closed]

I am trying to calculate some values using numpy. Here is my code, ...
11k views

### Generate sample coordinates inside a Polygon

I have a Polygon named as poly. I attempted to randomly select 5 coordinate points that lies inside the polygon. ...
58 views

### Fill missing dates into array of np.datetime

Here is the problem: Given an array of a non-continuous sequence of dates. Make it a continuous sequence of dates, by filling in the missing dates. ...
690 views

### Marking duplicate entries in a numpy array as True

So the problem is Find the duplicate entries (2nd occurrence onwards) in the given numpy array and mark them as True. First time occurrences should be False. And my solution is: ...
41 views

### Finding the Euclidean distance between the vectors of matrix a, and vector b

Here is the problem, Given a 2D numpy array 'a' of sizes n×m and a 1D numpy array 'b' of size m. You need to find the distance(Euclidean) of the 'b' vector from the rows of the 'a' matrix. Fill the ...
75 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 ...
125 views

### Ensuring performance of sketching/streaming algorithm (countSketch)

I have implemented what is know as a countSketch in python (page 17: https://arxiv.org/pdf/1411.4357.pdf) but my implementation is currently lacking in performance. The algorithm is to compute the ...
29 views

### Cleaning Netflix-style recommendation engine datasets

I am working on a portfolio project that deals with creating a Netflix-style recommendation engine for movies. This code is currently ran locally but I will need to upload it into Jupyter Notebooks ...
19 views

### Reducing running time of creating integration field in flow field algorithm (python)

The integration field needs to get recalculated every time the starting or goal coordinates change. I wanted to use the flow field algorithm for a problem that includes bigger mazes with changing ...
133 views

### Optimizing an implementation of the RKF method

This is an algorithm regarding the RKF method: ...
162 views

### From Hummingbirds to Ornithopters: Simulating the Aerodynamics of Flapping Wings

Ptera Software A Flapping Wing Aerodynamics Simulator Written in Python Motivation About a year ago, I became fascinated by how animals fly. As an aerospace engineering student, it surprised me that ...
864 views

### Cython with variable-length arrays

Dynamically growing arrays are a type of array. They are very useful when you don't know the exact size of the array at design time. First you need to define an initial number of elements. (Wikipedia) ...
411 views

### Efficient NumPy sliding window function

Here is a function for creating sliding windows from a 1D NumPy array: ...
49 views

### Generating multiple new arrays using numpy

Say I have the following two different states: state1 = [0, 1, 0, 1, 1] state2 = [1, 1, 0, 0, 1] And I want to generate n number of new states by only changing the ...
5k views

### Calculating time deltas between rows in a Pandas dataframe

I am trying to compute the difference in timestamps and make a delta time column in a Pandas dataframe. This is the code I am currently using: ...
4k views

### Cubic spline interpolation in Python from scratch

I implemented the cubic spline interpolation explained in https://en.wikipedia.org/wiki/Spline_interpolation as a Python class. Of course, such an interpolation should exist already in some Python ...
1k views

### Plotting the Mandelbrot set at different zoom levels [closed]

I'm interested in making an animated movie of a zoom in on a part of the Mandelbrot set. My code works well for a few zooms, but upon trying to zoom in quite far, I find that the fractal becomes "...
31 views

### Generating basic Fractals

Trying to generate fractals image with given IFS equations. I have two classes: Shape - Gives co-ordinate to form a shape. Ifs -...
37 views

### Reimplemented R function(s) is surprisingly slow

I reimplemented the functions https://rdrr.io/cran/fclust/man/RI.F.html, https://rdrr.io/cran/fclust/man/ARI.F.html and https://rdrr.io/cran/fclust/man/JACCARD.F.html of the R package https://rdrr.io/...
94 views

### decreasing solver speed

How can I decrease the time complexity and increase efficiency, without writing a new algorithm. My solution solves the majority of puzzles in a fast time, but for some difficult ones it can take over ...
57 views

### Performance issue in python- network creation based on the Euclidean distance with nested for loops is too slow

I want to create a network in which the links are formed based on a similarity metric defined as the Euclidean distance between the nodes. The distance is calculated using socio-demographic features ...
95 views

So this is a project I have been working on for the last few weeks. Just started learning Python. Started out with bash scripting and got the itch to learn more. Anyway code fetches covid-19 data from ...
28 views

### Reduce computing time of planning method (grid)

Has anyone an idea how to change the planning(self, sx, sy, gx, gy)-method to save computing time? I'm new to NumPy and don't know how to use it effectively yet, but I heard it could also be a good ...
23 views

### Optimized method of transfering numpy image arrays over localhost to web-tech UI for desktop UI

I have been doing testing to find the most efficient way to display numpy array images to a HTML, CSS, JS UI (specifically, with CEF Python). I mainly used the method explained here to do the numpy ...
40 views

### IDF Function with a list of list

I wanted to build a Inverse Document Frequency function, because in my opinion was not easy to do with scikit and I wanted also to show how it works for educational reasons. Also reading this question ...