Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [numpy]

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

-2
votes
0answers
12 views

super(type, obj): obj must be an instance or subtype of type [on hold]

Im trying to run a short climate model in Python, but get the error: TypeError: super(type, obj): obj must be an instance or subtype of type. can somebody advise please? ...
3
votes
0answers
30 views

Distance transform on image using NumPy

I would like to find the find the distance transform of a binary image in the fastest way possible without using the scipy function distance_transform_edt(). The ...
3
votes
2answers
55 views

Simulation of an alien population

Background I've come across a puzzle (Problem 10 from this list of sample interview questions): One day, an alien comes to Earth. Every day, each alien does one of four things, each with equal ...
3
votes
1answer
50 views

Dice sums comparison probability

I was playing a fun little board game called U.S. Patent No. 1 about time traveling to register your time machine as the first U.S. patent. I started to wonder what the dice probabilities were for ...
0
votes
0answers
23 views

solving n queens in polynomial time when n is a multiple of 6 [closed]

I was trying to solve queens when I realized that the queens' position was geometrical shapes then probe every n and I discovered that the multiples of 6 form two parallel lines then create a code ...
4
votes
1answer
42 views

Project structure for a reactor model built in Python

I use Python for various computational reactor modeling activities. While there are many components to the models I develop, the basic structure of the project folder tends to be similar to the ...
2
votes
0answers
23 views

Calculate average round cell value from each 2D array cell

I am try to practice the leetcode and hit the following question 'image smooth'. Basically the question is to calculate average the around cell value from a 2D array. I have following code. But if ...
3
votes
1answer
71 views

List of word to list of index in a vocabulary [closed]

I have a dataset composed of ~700k samples. Each sample is composed of n_features features. Each feature is a word. Each feature has his own vocabulary. The size of the vocabularies range from 18 to ...
1
vote
1answer
68 views

Generate xyz coordinates of a tube made of multiple rings

In this code, I generate the coordinates of a tube made of multiple rings. Finally, I write those x,y,z coordinates in a .csv file. I want to simplify this code (maybe using a for loop?). ...
1
vote
0answers
13 views

Batch gradient descent and stochastic gradient descent

I'm trying to implement logistic regression and I believe my batch gradient descent is correct or at least it works well enough to give me decent accuracy for the dataset I'm using. When I use ...
2
votes
1answer
28 views

Implementation of a threshold detection function in Python

I want to implement following trigger function in Python: Input: time vector t [n dimensional numpy vector] data vector y [n dimensional numpy vector] (values correspond to t vector) threshold tr [...
1
vote
0answers
31 views

Get stacked game state in NHWC format

After reading this, I decided to transition my DQN code from the keras library to tf.keras library (code is located in this repo) and my original code used NCHW format, as it was faster with GPUs. As ...
0
votes
0answers
34 views

Rotate Algorithm making the picture fit

I have made this algorithm myself based on how to rotate an image. ...
6
votes
1answer
84 views

Rotating an Image using own algorithm in python

For an uni assignment I have been giving the task of making my own rotating algorithm in python. This is my code so far. ...
3
votes
2answers
50 views

Stroke Algorithm in python

Here is my next Numpy practice, I am not sure is it more accurate to call it "Stroke Algorithm" or "Draw edge stuff", xD. Anyway I did in steps: calculate each pixel's RGB absolute distance to ...
-1
votes
0answers
38 views

Generating randomised shuffles for an experiment, with verification

I am learning python for work and am trying to generate a matrix containing an experimental set up. The design requires a number of individuals (\$n\$) to see a number of options (\$k\$) in a random(...
3
votes
1answer
73 views

Simplex method (linear programming) implementation

We've implemented a version of the Simplex method for solving linear programming problems. The concerns I have are with the design we adopted, and what would be some refactorings that would improve it ...
1
vote
0answers
27 views

Converting 2D arrays into strings

I have this function which converts a couple of 2D NumPy arrays into strings and then saves them in a text file (grid, quality, <...
1
vote
1answer
58 views

Renting vs. owning comparison calculation

I have working code that compares rental prices and owning costs, given some parameters. It works, but it surely does not look good. I hadn't worked with time-series before and I wonder how I could ...
8
votes
0answers
76 views

Replace color in image measured by Euclidean distance

This script replaces the red hair in image to black color (replace one color to another) Three major parts in this script Not just replace color red to black, I hope the output image looks natural, ...
2
votes
2answers
76 views

Genetic Algorithm - heavy memory usage

I have implemented two pieces of code as part of an Evolutionary Algorithm study: Particle Swarm Optimisation (PSO) and Genetic Algorithm (GA). Both are designed to find an optimal solution to a ...
0
votes
0answers
58 views

Get directions for unit movement with conditions

I am developing a bot for Starcraft II. In my code for unit movement, I often find myself doing this: Calculate points around the current position of the unit Check if these points are valid and can ...
10
votes
1answer
250 views

Pretty printing of the numpy ndarrays

Following this idea for pretty printing of numpy ndarrays, I have developed a very primitive prototype: ...
5
votes
1answer
78 views

process data with numpy seems rather slow than pure python?

In my opinion, Vectorization operation with numpy should be much faster than use for in pure python. I write two function to get and process data in a csv file, one in numpy and another in pure python,...
1
vote
0answers
45 views

Better way to set number of threads used by NumPy

Background When NumPy is linked against multithreaded implementations of BLAS (like MKL or OpenBLAS), the computationally intensive parts of a program run on multiple cores (sometimes all cores) ...
-1
votes
1answer
43 views

Marking a rectangular region in a NumPy array as an image mask

I'm currently working on creating a mask for an image. I have initialized a two-dimensional numpy zeros array. I now want to replace the values of the mask corresponding to pixels following some ...
12
votes
1answer
1k views

Looking for a way to create a particular matrix in python in a less amount of time

I want to create a matrix like this in Python: $$ \begin{pmatrix} 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 2 & 2 & 2 & 2 & 3 & 3 & 3 & 3\\ 0 & 1 &...
1
vote
0answers
32 views

Live plot with many subplots

I have a stream of sensor data which I want to visualize in a plot with many subplots. Plotting the data is a real bottleneck in my code. Right now I get with small resolution only 16 FPS which is far ...
1
vote
0answers
40 views

Gray-level co-occurrence matrix feature computation

I have many images and want to compute the GLCM properties for every image. Below my code that runs many hours to complete the task: ...
2
votes
3answers
65 views

Speed up weighted average for Leela Chess Zero

Problem: Given a vector of about 40 values m with normal error sd compute the weighted average of the values weighted by the ...
4
votes
1answer
47 views
3
votes
0answers
53 views

Variable bit length lossy floating point compression

I am implementing a new compression algorithm for the weights of a neural network for the Leela Chess project. the weights are roughly 100Mb of ...
2
votes
1answer
108 views

k-means using numpy

This is k-means implementation using Python (numpy). I believe there is room for improvement when it comes to computing distances (given I'm using a list comprehension, maybe I could also pack it in a ...
1
vote
1answer
36 views

Centre all columns of an array

I have this working code tested over lots of np.arrays. It is a for-loop that walks through all the values of a np.array (x,y). For each y-row, it finds the first x-column for which the value is ...
4
votes
1answer
61 views

Python program computing some statistics on Scottish geographic areas

This simple script computes some basic descriptive statistics, like mean, standard deviation, kurtosis, etc. on data column imported from a CSV file with use of ...
0
votes
1answer
29 views

Modeling a capacitor with DC Bias

I am modeling a capacitor which has its capacitance varying according to the DC Bias. The DC Bias is computed by taking the mean of the voltage across the capacitance. ...
1
vote
0answers
35 views

Applying a filter to a large array with elements that are not regularly spaced

I need to apply a Gaussian filter to a 2D numpy array where the distance between adjacent array elements depends on the row of the array. (Specifically, the data are evenly spaced in latitude and ...
3
votes
1answer
52 views

Statistics and calculations

I went through a series of lessons in data science using pandas and numpy. I have attempted to replicate some of the more common algorithms based on maths forumlas and psuedocode, except for the <...
3
votes
1answer
69 views

Centered finite difference approximation for edge enhancement

I implemented a centered finite difference approximation for edge enhancement in images. The difference approximation has a step size h of the partial derivatives in the x- and y-directions of a 2D-...
3
votes
1answer
27 views

Create histogram with greater than symbol in x axis in pyplot

I wanted to create a histogram from a list of positive integers. I want to bin it so that I show all single numbers, say K through N, with more than k elements in the data set, as well as the number ...
6
votes
2answers
504 views

Find the intervals which have a non-empty intersection with a given interval

The problem is the following: I have a list intervals which consists of tuples of the form (start, end) [with ...
2
votes
3answers
105 views

Code to find the sum of the product of all size-n combinations of an array

The title is a bit of a mouthful, so here's a quick example: You've got an array, maybe just the digits 1-3, and you want to get the sum of product of all size-2 combinations of the array. Those ...
5
votes
1answer
49 views

Mapping DNA nucleotides into two-dimensional coordinates

I am the author of this package that turns DNA sequences into two dimensional visualizations. DNA, for the unaware, consists of four letters (A, T, G, and C). To convert the sequence, I am using this ...
3
votes
1answer
75 views

Solving sudoku using Backtracking-Search (BTS) with Minimum Remaining Values (MRV) heuristic

Intro I wrote this code to solve sudoku as part of an assignment (closed now) for an AI course. The program works correctly and I am quite happy with the code. Please let me know if I am rightfully ...
2
votes
1answer
40 views

Regularised regression code

I wrote some code to do regularised linear regression and it works, but I don't like the fact that I've had to double call the functions when plotting, nor the fact that I've sliced those calls to get ...
2
votes
2answers
59 views

Eigenvector calculation involving three matrices

Code performs well for small matrices, but gives memory error for large matrices of size 63000x93000, even when I am using server with memory of 64GB, space 100GB, swap 40GB. ...
1
vote
1answer
55 views

k-armed bandit problem in Python

I implemented k-armed bandit problem in C#, MATLAB and Python. C# and Matlab code run fairly fast (With same settings of T = 2000 and nRun = 1000 the elapsed time is about 6sec). However, the Python ...
1
vote
1answer
31 views

Extended matrix generation

I am extending a matrix with some calculated values. This code runs very slowly. Is there a way to vectorize this with slice and ...
5
votes
1answer
253 views

Spearman correlations between Numpy array and every Pandas DataFrame row

I want to efficiently calculate Spearman correlations between a Numpy array and every Pandas DataFrame row: ...
7
votes
2answers
94 views

Counting SKUs that have not appeared in previous orders

I need help with optimizing my Python code. I have a bunch of orders and each order has several SKUs. This is what my data looks like: ...