Questions tagged [numpy]

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

Filter by
Sorted by
Tagged with
1
vote
0answers
27 views

Spectrograph using array-of-slice-references

I've written a working and complete proof of concept that shows a spectrograph in Matplotlib. I want to nail down this proof of concept before I continue with development, and I'm not thrilled with ...
1
vote
0answers
40 views

Resource reservation system

I'm using a function to determine if resources can be used again or not. This is the numpy array I'm using. ...
1
vote
1answer
85 views

TicTacToe: If an error messages do not appear straight away

I am fairly new to Python and have tried to create a tic tac toe game that is completely automatic, where you can see if a player1 has an advantage if you let it ...
3
votes
2answers
153 views

Active Brownian Motion

I am attempting to write a Python code to simulate many particles in a confined box. These particles behave in such a way that they move in the box in straight lines with a slight angular noise (small ...
0
votes
0answers
110 views

Vectorized numpy co-occurrence matrix generation

Update The implementation did not consider multiple occurrences of a same word, and self word occurrences. For instance when stride=2 and the word at the position is ...
3
votes
1answer
114 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 ...
3
votes
2answers
87 views

SineCosine Wave

As a beginner this is my first code. Any comments? ...
3
votes
0answers
122 views

Implement vectorization instead of nested loops on dataframe

I have a dataset ('sample_data.csv') of the form below: ...
4
votes
1answer
119 views

Counting Sequential Booleans

This function takes a list/array of booleans and converts them to an array that counts the number of either True/False values found next to each other. I'd like to see this optimized for performance. ...
5
votes
0answers
307 views

Slow copying of memmap array to numpy array

I have multiple binary (structured) file, each of 2GB, which I am currently reading in pair, using memmap to cross-correlate the same. I want to minimise the time required by this IO process, in the ...
1
vote
1answer
38 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 ...
4
votes
1answer
75 views

Can this numpy code be vectorized?

I've written the following function to produce n realizations of a CIR process for a given set of parameters: ...
2
votes
0answers
36 views

Converts Pandas and Numpy into Dictionary and Converts back to original format for data split

I'm trying to refactor some code so that it does not have so much repetition to it. What I'm trying to do is create an input for a multiple channel/input neural network. The features that are being ...
5
votes
2answers
1k 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) ...
4
votes
1answer
67 views

Searching in Numpy

I have sorted ascending number if you do abs(arr) in below code, I am trying to search for index where sign got changed from negative to positive. Wrote this below code, but for some reasons, I ...
2
votes
0answers
30 views

Populating 2 columns in a dataframe with frequencies from a dictionary of dataframes

I have a pandas dataframe, my_calculation, that is a Cartesian product of 4 different categories (Name, Freqset, Formula, and Location) and two additional ...
5
votes
1answer
123 views

Inplace operations for a class with numpy-style docs

In pandas lot's of methods have the keyword argument inplace. This means if inplace=True, the called function will be performed on the object itself, and returns ...
1
vote
1answer
36 views

Pandas : Apply Merge operations from a column

I have a data frame, which contains three columns: ...
1
vote
2answers
116 views

Better way to calculate double-scattering diffraction using cartesian product of arrays?

This script calculates points in reciprocal space for hexagonal 2D lattices, then uses the cartesian product from itertools to add each vector from one lattice to all of the vectors of the other in ...
2
votes
1answer
184 views

Print a array of size n*n, with borders as 1's and fill rest of element with 0's

Assessment Question: Print an array of size n*n, with border as 1's and rest of the element as 0's. Sample Input: 5 Sample Output: ...
2
votes
0answers
27 views

Moving mean square error between 2 arrays, 'valid', where they fully overlap

You can find my original post here on SO. I was very surprised that I couldn't find a pre-coded function doing a mean square error between a signal (array A, size a) and a pattern (array B, size b<...
1
vote
0answers
27 views

Hermit Interpolation error check

I have done this code. But I am not sure that I have got the exact answer. I have never solved this kind of problem earlier. General form of a $2n+1$ degree Hermite polynomial: $$p_{2n+1} = \sum_{k=0}^...
7
votes
2answers
787 views

How to clean the indexes, and ideally not create an additional array

So I answered this question on SO and probably did someone's homework along the way. In the original question the OP has the answer variable and wants it split to ...
4
votes
0answers
104 views

Text-based tic-tac-toe game in Python

I'm working on a text-based tic-tac-toe game, mostly for fun and because I'm learning programming. I would appreciate any comments you could give me to improve it. Main structure: Uses a Board class ...
4
votes
1answer
89 views

Calculating text similarity, filtering the results, and reshaping matrix into a list-of-tuples generates MemoryError

I have a constantly growing corpus of currently ~36,000 documents (growing daily) and I want calculate the similarity between each pair of documents. After calculating the similarity scores, I want ...
6
votes
0answers
79 views

Code optimisation: Converting dataframe to numpy's ndarray

I am working with a dataframe of over 21M rows. ...
1
vote
1answer
140 views

Reduce one for loop to decrease time complexity

I'm using NumPy to find out langrage polynomial interpolation. I'm using 2 for loop to find out langrage polynomial, but I want to reduce 2nd for loop so that my code time complexity can be less. Can ...
1
vote
1answer
675 views

Count the number of neighbors

I have a numpy array called contour of \$N\$ coordinates (x,y) of dimension (N, 2). For each ...
6
votes
0answers
47 views

Subtracting elements of datasets of an HDF5 file

I am trying to solve the following problem: Input: Input is two arrays (Nx4, sorted in column-2) stored in datasets-1 and 2 in HDF5 file (input.h5). N is huge (...
3
votes
1answer
202 views

Add values from a dictionary to subarrays using numpy

I'm working with numpy arrays and dictionaries, the keys of the dictionary are coordinates in the numpy array and the values of the dictionary are a list of values I need to add in those coordinates, ...
2
votes
0answers
68 views

Find the available squares for rooks in empty board using boolean arrays

I will describe two techniques, then the question will be how I can make an algorithm that is more efficient, if possible. I want to find an efficient way to determine the squares where the pieces on ...
2
votes
2answers
145 views

Multiplying numpy arrays

I have written a function to multiply two numpy arrays. ...
2
votes
1answer
39 views

Construct a numpy array by repeating a 1 dimensional array sliced at different indices

I have a pandas dataframe (called base_mortality) with 1 column and n rows, which is of the following form: ...
1
vote
1answer
96 views

Getting the square of the difference between many points in a NumPy array

I have an array with 40000 numbers that are floats or ints. I need to perform some calculation. To do this I have used nested for loop, but the code is really slow. Can I use something instead of ...
7
votes
3answers
235 views

Find the maximum price change over any 1 to 5 day rolling window

The code below solves the following task Find the maximum price change over any 1 to 5 day rolling window, over a 1000 day period. To be clear "any 1 to 5 day rolling window" means ...
2
votes
0answers
17 views

Getting Graph and Block Animation to Run Smoothly Together

I have been working on some code that produces a live graphic of a graph and blocks moving. It works in theory but does not look like how I want. The animation runs very slow when showing both the ...
2
votes
1answer
180 views

Smart Tic Tac Toe, a reinforcement learning approach

I'm currently familiarizing myself with reinforcement learning (RL). For convenience, instead of manually entering coordinates in the terminal, I created a very simple UI for testing trained agents ...
7
votes
0answers
86 views

Using get_dummies to create a Simple Recommender System - Cold Start

Question: was using get_dummies a good choice for converting categorical strings? I used get_dummies to convert categorical ...
4
votes
1answer
189 views

Generating unique 2D numpy arrays with 2 adjacent row elements swapped while using BFS in Python

So I have a 12 x 6 2D numpy array for input which consists of 7 possible board objects (characters from 'abcdefg'). From a 2D numpy array I wish to generate all the possible unique 2D arrays, in whose ...
2
votes
1answer
39 views

Get the last activity per user per day in a dataframe

I have many users. Each time a user uses their smartphone it register them. I am determining the last time each user used their smartphone each day. Additionally smartphone usages from 18:00 to 06:00, ...
5
votes
2answers
122 views

Analyzing the darkest pixels of an image in Python

This script takes an image copied from the clipboard and analyzes the n darkest pixels of the image. It will loop through each found value, prints out the value information and the quantity, then ...
1
vote
0answers
36 views

Calculation Correlation Time

I want to calculate the correlation time. First I calculate the auto-correlation function: $$ \begin{align} \chi(t) = &\frac{1}{t_{max}-t}\sum\limits_{t'=0}^{t_{max}-t}m(t')m(t'+t)\\ &-\frac{...
3
votes
1answer
46 views

Numpy array slicing/reshape/concatination

I'm sure my question exists on the internet, i just don't know the right formulations. I have a data-sample as input for a NN. This sample has the shape (1, 8, 28, 80). Basically it is 80 timesteps ...
0
votes
1answer
68 views

Image color-moment extractor [closed]

I was just wondering if there is a way to speed up the performances of this for loops in Python. I'm trying to process an image to get the color-moments without using libraries. It takes about 12sec ...
3
votes
2answers
213 views

Implementing a Steepest Descent Algorithm

I am teaching myself some coding, and as my first "big" project I tried implementing a Steepest Descent algorithm to minimize the Rosenbrock function: $$f(x, y) = 100 (y - x^2)^2 + (1 - x)^2$$ The ...
5
votes
1answer
247 views

Jupyter notebook style help + code suggestions for pandas

I wanted to open source some code to scrape and analyze publicly-filed stock buys and sells from U.S. senators. I'm not familiar with code style for Jupyter notebooks or pandas in general. Would it be ...
6
votes
3answers
479 views

Efficient numpy array manipulation to convert an identity matrix to a permutation matrix

Task I want to be able to generate the permutation matrix that splits a 1D array of consecutive numbers (i.e. even, odd, even, odd, even, odd, ...) into a 1D array where the first half are the evens, ...
5
votes
1answer
72 views

Applying gaussian blur on RGBA images

I designed the code and it works quite well for images of lower res. However my program takes a lot of time and ram to display higher res images (occupies 2GB RAM for 4k images and takes 20 minutes). ...
8
votes
2answers
139 views

Largest product in a grid - Project Euler 11

A stab at Project Euler Problem 11: Largest product in a grid ...
4
votes
1answer
175 views

Simulated annealing for magic square-of-squares puzzle

I'm working on a function that takes in a NumPy array containing only mutually distinct positive square numbers. I want the function to pick an element at random, find if there is a lower positive ...

1
2
3 4 5
14