# Questions tagged [numpy]

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

522 questions
Filter by
Sorted by
Tagged with
46 views

### Evaluate joint probability density function of a Markov random field

The following code evaluates probability mass function for all possible states of a model. ...
42 views

### Grouping sorted coordinates based on proximity to each other

I created an algotrithm that groups a sorted list of coordinates into buckets based on their proximity (30) to one another. Steps: Create a new key with a list ...
28 views

### Find the next predicted time a satellite is going to take picture of place

create a function which predicts the next time a satellite image will be taken of a certain location. When there is enough data to do so, the function should print a prediction for when the next ...
53 views

### Hurst Exponent calculator

I am aiming to compute the Hurst Exponent of a 1-D signal time series in Python. For now, I have one existing function hurst(sig) which returns the Hurst exponent ...
35 views

### Trapezoidal rule for set of data

Here is the question from the book of Mark Newman-Computational Physics Exc 5.1 a) Read in the data and, using the trapezoidal rule, calculate from them the approximate distance traveled by the ...
477 views

### Generating Julia set

So, I just wanted to post something on Rosetta Code, and I found this task of generating and plotting a Julia set: http://www.rosettacode.org/wiki/Julia_set. There was already one solution but it was ...
22 views

### Converting string of hex values to numpy array of specific shape

I am working on a project that needs to take a .bdf font file and turn it into a Numpy array. I am using the code below to turn the bitmap for the font into a numpy array. It currently works, I just ...
49 views

### Select optimal piecewise regression fit

I'm making a program which fits a piecewise linear regression with up to 4-5 breakpoints in the data, and then deciding how many breakpoints is best to prevent over and underfitting. However, my code ...
89 views

### Calculate mean and standard error of a generator in Python

The goal of the following code is to calculate the mean and the standard error of vectors of randomly-generated numbers. I am looking for feedback both on the correctness of the calculation, and on ...
73 views

### Optimising iteration and substitution over large dataset

I have the following code: ...
390 views

### MNIST Deep Neural Network in TensorFlow

I have been working on this code for a while and it gave me a lot of headaches before I got it to work. It basically tries to use the MNIST dataset to classify handwritten digits. I am not using the ...
26k 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 ...
108 views

### Performance issues with double for loops, EM-algorithm

The alpha that I create is not created in a crazily efficient way I know, but the main bottleneck in this code is in what I call the "Maximization step" (see comments in code). There are double for-...
23 views

### Parsing and plotting with numpy (or pandas)

I am trying to improve the following code to make it more broadly applicable and quick to use. ...
50 views

### Nested list performance tuning

I need to make a function like these two, but at least an order of magnitude faster in python. Any tips or tricks? These functions inputs are nested lists, like: ...
858 views

### KenKen solver - Python

Here is my code for the KenKen puzzle: The user must fill in each embolded regions with numbers in the range 1-n where n is the board dimensions (e.g: 4*4, n=4) ...
55 views

### Generating Latin hypercube samples with numpy

I wrote some code to generate Latin hypercube samples for interpolation over high-dimensional parameter spaces. Latin hypercubes are essentially collections of points on a hypercube that are placed on ...
799 views

### Plotting polynomials roots

NOTE: See follow up to this question here I created a simple python script to plot quadratic, cubic and quartic polynomials with integer coefficients between -4 and 4. It uses numpy to find the roots ...
86 views

### Agent based model in Python

My model is based on the Ultimatum Game. I won't go into the intuition behind it but generally speaking it functions as follows: The game consists of a n × n lattice on which an agent is placed at ...
32 views

### Implementing a tic tac toe board in python

This is my attempt at making a class to represent a game board for tic tac toe. I normally hate while loops but I used one because I cant seem to make the ...
59 views

### Pad a ragged multidimensional array to rectangular shape

For some machine learning purpose, I need to work with sequences with different lengths. To be able to process efficiently those sequences, I need to process them in batches of size ...
35 views

### Efficient resampling of time series

I'm trying to create an efficient function for re-sampling time-series data. Assumption: Both sets of time-series data have the same start and end time. (I do this in a separate step.) Resample ...
41 views

### Grid class for games using python

I made this class to eventually create a tic tac toe game. I used numpy which is a first for me. Here it is: ...
29 views

### Sortings stocks into quantiles based on their signal

This function sorts stocks into quantiles. To do so I used this function that accepts sig_df (dataframe with the timeseries of stocks signal) and number of ...
90 views

### Positive cumulative sum of difference

I have a water reservoir with input and output rates. I want to determine when the input is exceeding the output by a certain constant. To accomplish this, I need to cumulatively sum all cases where ...
61 views

### Optimal algorithm to move a a rope from one position to another

I'm an engineer working with a deformable membrane that is attached to actuators. The goal is to move the membrane from one shape to another, without ripping the membrane. This imposes "neighbor ...
126 views

### Python solution for the N-Queens challenge

Here is my coded solution for the N-queens challenge The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. The program outputs all ...
55 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 ...
54 views

### Numerical integration in cython

I have a set of nested functions that I need to call multiple times. I know scipy.quad is pretty fast, but I will need to call the integrator recursively and want ...
47 views

### Gravity simulation using Numpy and Pygame

This is a simple gravity simulator coded in Python 3.7 using Numpy and Pygame. I was wondering if it can be optimized further. Initially I had coded it using pure Python lists, using nested loops to ...
91 views

### Choosing evenly distributed points from a million points on a sphere

I have $10^5$ to $10^6$ points on a sphere, and want to choose some points from them which are as close as uniformly distributed as possible. For that reason, I do the following: at each step I ...
160 views

### Population dynamic simulation on biological information maintenance 2

This question is the follow-up to this previous question. Background Using this simulation I investigate a system in which enzymes proliferate in cells. During the replications of enzymes, ...
51 views

### Find indices using for loop

I should estimate the effect of B fields on particle deflection using $F=V \times B \times Force$. I have a 2D mesh with $n_x$ and $n_y$ which show the number of points in each direction. $B$ ...
47 views

### Python list operation - merge adjacent cubes into minimum number of shapes

I am hoping to find a way to make the process a lot faster. The lists are made of rows of 6 numbers, i.e. [[float] * 6, ...]. The intent is to pass a list of cube ...
80 views

### 3D metrics for segmentation evaluation

I am new to programming and I am trying to develop a machine learning approach for image segmentation (https://en.wikipedia.org/wiki/Image_segmentation) and here I want to evaluate the predicted (...
63 views

### Extract principal components

First, I am aware that this can be done in sklearn - I'm intentionally trying to do it myself. I am trying to extract the eigenvectors from np.linalg.eig to form ...
152 views

### Population dynamic simulation on biological information maintenance

Background Using this simulation I investigate a system in which enzymes proliferate in cells. During the replications of enzymes, parasites can come to be due to mutation. They can drive the system ...
45 views

### Extract cell values from multiband rasters

I have the following function and code snippet to extract cell values for multiple years, format it, and save to a list. Each raster has 365 bands — one for each day. A separate operation is performed ...
54 views

### U-Net: A TensorFlow model

What I want to know I am writing a ML model called U-Net with Python (and TensorFlow). My question is not about the machine learning or Tensorflow, I want to know the best structure of the code. I ...
3k views

### Converting a vector image to matrix

I am playing with the cifar-10 dataset (available here) and for now I would like to plot one of the images of a batch. The images are represented as vectors when I get them from pickle: From the ...
70 views

I'm writing a simulator which checks how many balls we need to occupy every box or how many balls we need for any box has at least 2 balls (birthday paradox). I wrote the script in python and it is so ...
194 views

### Improve Performance of Comparing two Numpy Arrays

I had a code challenge for a class I'm taking that built a NN algorithm. I got it to work but I used really basic methods for solving it. There are two 1D NP Arrays that have values 0-2 in them, ...
1k views

### Parallelizing some model fitting code using multiprocessing

I'm trying to refactor some code for fitting parametric models that are defined symbolically using Theano. My goal is for all models to expose a common interface so that, as far as possible, they can ...
204 views

### Deep Neural Net implementation in Python3

I created my first implementation of an arbitrary feed forward neural network with a simple back-propagation training implementation. ...
118 views

### Longest carbon chain

My Code measures the longest path, not allowing cycles. For the example input below, the longest chain would be 4 Please can you review my code? ...
62 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 ...
19 views

### TensorFlow regressor to predict probability of winning, given a current score

This is a TensorFlow regressor that tells me that if you get x score, you will have x% to win a game in x game(game is not important). Doing this as a project to learn tensorflow. ...
11 views

### Super-minimal implementation for inference only of fully a connected neural network in Python + Numpy

There are a lot of Neural Networks Frameworks available but in order to understand how things work internally reimplementing can be a good exercise In this case the goal is to develop a super-...