Questions tagged [numpy]

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

Filter by
Sorted by
Tagged with
-4
votes
1answer
50 views

Array-indexed sum [closed]

...
4
votes
1answer
67 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
33 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 ...
-1
votes
0answers
17 views

Is there a way to find Shapiro-Wilk p-values [closed]

I have tried to find p values for my test statistic, but I got following error value: The codes for test statistic and data 7 in here ''' compute Shapiro-Wilk p-values ...
3
votes
1answer
84 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
61 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
28 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
68 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
35 views

Pandas : Apply Merge operations from a column

I have a data frame, which contains three columns: ...
1
vote
2answers
84 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
95 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
24 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
20 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
780 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
55 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
78 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
70 views

Code optimisation: Converting dataframe to numpy's ndarray

I am working with a dataframe of over 21M rows. ...
1
vote
1answer
77 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
117 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
40 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
88 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
61 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
85 views

Multiplying numpy arrays

I have written a function to multiply two numpy arrays. ...
2
votes
1answer
38 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
91 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
179 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
16 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
159 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
61 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
99 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
28 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
54 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
33 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
41 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
66 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
85 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
194 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 ...
5
votes
3answers
273 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
40 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). ...
7
votes
2answers
114 views

Largest product in a grid - Project Euler 11

A stab at Project Euler Problem 11: Largest product in a grid ...
4
votes
1answer
79 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 ...
2
votes
0answers
28 views

Profiling for Bézier curve calculations

Recently I posted an answer on a question about Bézier curve calculations. As a micro-synopsis: there are three implementations of De Casteljau's algorithm here, including the original poster's, AJ ...
1
vote
0answers
19 views

How to optimize code to take advantage of NumPy broadcasting when calculating pairwise correlation?

I'm trying to take advantage of NumPy broadcasting and backend array computations to significantly speed up this function. Unfortunately, it doesn't scale so well so I'm hoping to greatly improve the ...
3
votes
1answer
64 views

Fast way to calculate Standing Wave modes in NumPy

I am trying to plot the standing wave modes in a 2D box over time. The equation for this is: $$ A(x,y,t) = \sum\limits_{n,m} sin \left( \frac{n \pi x}{L_x} \right) sin \left( \frac{m \pi y}{L_y} \...
1
vote
1answer
33 views

What would be the computationally faster way to implement this 2D numerical integration?

I am interested in doing a 2D numerical integration. Right now I am using the scipy.integrate.dblquad but it is very slow. Please see the code below. My need is to ...
4
votes
1answer
74 views

Code for wireless communication work

I need some pointers on how I can speed up my code, as of now it is incredible slow for larger inputs. The way the code works is that the file Loc_Circle_50U.txt contains the true locations of 50 ...
6
votes
3answers
502 views

Improvement on data normalization

I have a part of code that is loading a dataset and normalizing property values to [0, 1]. My implementation is: ...
2
votes
1answer
125 views

Building optimal portfolios for all combinations of stocks

Hi I recently wrote some code in python that does the following: 1.) Pulls stock closing data from yahoo finance for x number of stock 2.) finds all possible combinations of x stocks in groups of y ...
4
votes
1answer
106 views

Can this sampling code be vectorized or optimized?

I have some code that is slow because it loops over every row in an input matrix Y. Obviously, this code does not scale with the number of rows. I don't think it's ...
5
votes
1answer
60 views

Binning whilst avoiding the need for for-loops in Python?

I am working on a relatively simple binning program, where I take a 5D array and bin it based on two 3D arrays to create a contour plot. See the sample code below. In actuality, my arrays are of size <...

1
2 3 4 5
13