# Questions tagged [numpy]

32145 questions

1

votes

0

answer

36

Views

### Vectorize quintuple for loop in numpy, with non-trivial indexing

I have a series of nested for loops, used to populate a rank-3 array as follows:
S = ... # Some number
N = ... # Some other number
an_array = np.zeros((S, N * N, N * N))
for s in range(S):
for i in range(N):
for a in range(N):
for j in range(N):
for b in range(N):
an_array[s, i * N + a, j * N + b]...

1

votes

1

answer

180

Views

### Numpy arrays from tuples of arrays for matrix based neural networks

To implement learning in a neural network I'm using stochastic gradient descent in which the mini batches are represented via the following list comprehension:
mini_batches = [training_data[j:j+mini_batch_size] for j in range(0,len(training_data),mini_batch_size)]
Inside the list comprehension the s...

1

votes

1

answer

157

Views

### Edge based binarization

I am trying to implement the edge based binarization algorithm that was written in the research paper 'Automatic License Plate Recognition Using Deep Learning Technique' but i as i implemented it i get the final image all black and can not find the problem.
import cv2
import numpy as np
def edge_bas...

1

votes

1

answer

64

Views

### Currently Animating Scatter Plot With Static Frames. Is there a way to animate over a moving window instead?

I have an array of arrays with format [2000][200,3] that I am creating an animated scatter plot of. 2000 is the number of frames and the interior arrays have format [length, [x,y,inten]] which are the points to scatter.
So for an example a single frame will look like:
Array[0]=np.array([x_1,y_1,I_1...

1

votes

0

answer

640

Views

### Applying a specific High pass filter on a RGB image in numpy

I'm trying to preproccess my image before feeding it to the CNN.
Goal
To extract the residual after applying a high pass filter( Reference 1 ) on a RGB image of dimensions 512x512 ( basically a shape of (512,512, 3) ) using the following equation:
link to image
where I is the Image and the matrix is...

-1

votes

1

answer

57

Views

### Python multi-dimensional notation transpose automatically

I have the following minimal example:
a = np.zeros((5,5,5))
a[1,1,:] = [1,1,1,1,1]
print(a[1,:,range(4)])
I would expect as output an array with 5 rows and 4 columns, where we have ones on the second row. Instead it is an array with 4 rows and 5 columns with ones on the second column. What is happen...

1

votes

0

answer

77

Views

### Equivalent of numpy 'ufunc.at' method in Tensorflow

Note that this is essentially the same question as here but unfortunately the solution does not work in general even though it was accepted as the answer. Even more importantly, one of the main use cases for the ufunc.at methods, when there are multiple identical indices, is where it fails.
For now...

1

votes

1

answer

53

Views

### dtype of ndarray containing string in python

I know that in case of ndarray containing strings, dtype returned will be of the form dtype(S#) where # denotes the length of the string.
As shown in figure the array 'a' which is generated from a list [1,'2','3']. Once the array is created all the elements become string type. Array 'b' is created...

1

votes

0

answer

2.8k

Views

### Python ImportError "cannot import name 'multiarray'

I'm trying to simply import numpy into a python script (called 'MatPlotLib.py using PyCharm) and I'm receiving this odd error. It worked fine, but then I started messing around with Jupyter. I tried uninstalling and reinstalling Python, then a system restore, but nothing seems to be working. I th...

-1

votes

2

answer

30

Views

### How to find maximum negative and minimum positive number in a numpy array?

I am given an array containing both positive and negative numbers.
import numpy as np
arr = np.array([-10.2, -5.3, -2.1, 0, 1.2, 3.4])
I would like to find the index corresponding to the maximum negative number and to a minimum positive number. In the above, my expected outcome is 2 and 4. Is there...

5

votes

1

answer

30

Views

### Python partitioning an N-dimensional volume into uniform sub-volumes

I have an N-dimensional square grid (represented by a numpy array) from which I need to get the coordinates of the vertices of all the 1x1x1x... N-cubes (i.e., hypercubes). For each 1x1x1x... N-cube, I need to store the 2^N coordinates (grid points) of the vertices in a numpy array for further proce...

1

votes

1

answer

216

Views

### LSTM with Numpy, can't find a definitive algorithm

I am doing LSTM from scratch and am following this guide, but the loss is not decreasing but increasing. This is the best guide I have found thus far but that is not saying much as even this one is incomplete. Aside from spotting the problem specific to my code I would appreciate any sources showing...

1

votes

1

answer

291

Views

### 3d plot of list of (hist, bin_edges) where histogram bar chart or lines are in the z-y plane

EDIT - reworked question
I need to print a 3D histogram of fitness data for 50 generations of computer programmes. This data is in calculated and stored in a logbook using the DEAP framework. The form of the plot needs to be with the fitness frequency on the z axis, generation on the x axis and bi...

1

votes

1

answer

64

Views

### How do I manipulate python numpy values in a separate C++ plugin

I'm trying to send the data of a numpy array in Python to a separate C++ plugin.
Is it correct to just send the address I get from __array_interface__['data'][0] to a C++ plugin, and create a double pointer from it there? I'm assuming that since it's a flattened array, all the values are aligned w...

1

votes

0

answer

249

Views

### Elementwise bitwise-XOR numpy matrix with numpy vector

Suppose that we are given a two dimensional matrix A of dtype=uint8 with N rows and M columns and a uint8 vector of size N called x. We need to bit-wise XOR each row of A, e.g. A[i], with the corresponding element in x, i.e. x[i].
Currently, I am doing this as follows, but think that there are more...

1

votes

1

answer

529

Views

### Python - 2/3D scatter plot with surface plot from that data

Using: [python] [numpy] [matplotlib]
So I have a 3D array to create a scatter plot making a n * n * n cube. Those points have different values of potential represented by colors.
size = 11
z = y = x = size
potential = np.zeros((z, y, x))
Positive = 1...

1

votes

0

answer

304

Views

### Tensorflow NaN loss during training

I have made a neural network which is basically a copy of a tutorial on the tensorflow website (https://www.tensorflow.org/tutorials/layers). The only difference is that my neural network is a 3D conv net and not a 2D conv net. I had many errors regarding my data and I'm not sure whether this one al...

1

votes

0

answer

89

Views

### Matrix operations on labeled arrays

I have a system with a linearised set of equations, such that the time-update operation can be performed by a matrix multiplication, y' = Ay, but I would also like to be able to index y using the names of the state variables, e.g. y['vel']. Is there a way to index in this way without losing the abil...

1

votes

2

answer

271

Views

### Python: How to pass subarrays of array into array function

The ultimate goal of my question is that I want to generate a new array 'output' by passing the subarrays of an array into a function, where the return of the function for each subarray generates a new element into 'output'.
My input array was generated as follows:
aggregate_input = np.random.rand(1...

1

votes

0

answer

34

Views

### concise definition of variable Multi-exponential function(s) within a loop (or in a class)

i'm trying to define a callable sum-of-exponentials function with a variable number of terms, in a condensed code. the concept i'm looking for should be applicable to any sort mathematical function with repetitive terms.
the individual functions should look like this:
from numpy import exp, zeros
de...

1

votes

0

answer

43

Views

### Elegant way of evaluating 3d function on cube faces

I am trying to evaluate a 3d function F(x,y,z) on the six faces of a 3d-cube (all with the same mesh) and am looking for an elegant pythonic way of doing so. The order of evaluation doesn't matter. Currently I'm using collections to rotate lists, consisting of two grids and one constant, that repres...

1

votes

1

answer

155

Views

### import cv2 terminal error - python

I'm trying to simply install and use cv2, numpy and matplotlib.
All installed successfully but when I type into the terminal 'import ...' it gives me the following error?
I'm confused because what the terminal is suggesting, I've already installed it that way?
I'm not sure about why it's pointin...

1

votes

0

answer

26

Views

### Pandas - Writing a Change Log Between Multiple Dataframes

I am trying to think of the most efficient way to write a change log for large dataframes. I have thousands of dataframes that have one million rows and 20 column, so efficiency is paramount. I have a couple solutions for checking for differences between two dataframes, but I cannot figure out the...

1

votes

2

answer

1.8k

Views

### best way to create numpy array from FOR loop

Is there a better way to create a multidimensional array in numpy using a FOR loop, rather than creating a list? This is the only method I could come up with:
import numpy as np
a = []
for x in range(1,6):
for y in range(1,6):
a.append([x,y])
a = np.array(a)
print(f'Type(a) = {type(a)}. a = {a}')...

1

votes

0

answer

35

Views

### Python/NumPy - Tail letter placement algorithm

I'm currently trying to write a program that correctly centers handwritten letters in a similar fashion to Microsoft Word. Using a standard handwriting grid for comparison, the bottom of the letter is placed right on the lowest gridline for most letters: Image.
However, there are five tail letters w...

1

votes

0

answer

103

Views

### How to read Chinese word embedding in plain text with numpy

I'm a new beginner of numpy. I want to use pretrained Chinese word embedding for RNN training. I want to read the embedding in plain text and convert them into .npy file. But I always got errors. I used python3.4, and numpy1.13.
The embedding are stored in plain text as the examples show, separated...

1

votes

1

answer

265

Views

### Can't reshape to right shape size

My original dataset is 7049 images(96x96) with following format:
train_x.shape= (7049,)
train_x[:3]
0 238 236 237 238 240 240 239 241 241 243 240 23...
1 219 215 204 196 204 211 212 200 180 168 178 19...
2 144 142 159 180 188 188 184 180 167 132 84 59 ...
Name: Image, dtype: object
I want t...

1

votes

0

answer

60

Views

### Partial Indexing Error in Python Series [duplicate]

This question already has an answer here:
key error and MultiIndex lexsort depth
1 answer
What exactly is the lexsort_depth of a multi-index Dataframe?
1 answer
I have created a Hierarchical indexed Series and I wanted to partially index some values of the Series. But When I changed the alphabetic...

1

votes

1

answer

1.1k

Views

### Changing WAV format from 32-bit floating point to 16 bit PCM

I'm trying to convert my 32-bit floating point sampled WAVE file to 16-bit integer WAVE file.
I want to generate 16-bit integer WAV file from ultrasonic sine wave which range from -1.414 to 1.414. In SciPy.io.wavfile, the output format is fixed according to type array which I generated. But I chang...

1

votes

1

answer

134

Views

### Calculating average raster values using numpy (fast)

I have a JPEG image and I need to calculate the average value of each raster (red, blue and green) as fast as possible. When I try to use two for loops to access each pixel and add the values, the process takes quite long (about 30 seconds). Is there a way to calculate the average raster values fast...

1

votes

0

answer

175

Views

### Wheel depends on build-time numpy version

I'm trying to build a python extension which uses the numpy C-API to manipulate numpy arrays. While setting up a deployment chain, I encountered a problem.
In my requirements.txt and setup.py I have added the dependency numpy>=1.7, because I'm using API features which have been introduced in that ve...

1

votes

2

answer

65

Views

### Sparse logical comparison of numpy arrays

I have two numpy arrays, A and B, representing coordinates of points in a 2D plane. Let's say A is 10000-by-2, and B is 20000-by-2. Both have float64 dtype.
I want to find out which of the points in first array, A, are in the second (B). Doing this with a for loop would be very slow. I came up with...

1

votes

0

answer

42

Views

### Numpy: Walk along vector within 2d matrix

Consider a function A(x(z, y), y(z)). I want to start at arbitrary (x, y), and walk along z. I have
dy/dz = 1
dx/dz = z y(1-x)/(y(y+z)). For epsilon z, this gives y(1-x)/(yy)
Now, take the following discretization:
import numpy as np
x = np.linspace(0.01, 0.99, 100)
y = np.linspace(0.5, 2, 50)
X, Y...

1

votes

0

answer

212

Views

### Count the numbers of neighbors in an array with 3D coordinates

I have the following problem. I have an an array of 3D coordinates like this:
arr = np.array([[21.000,48.000,28.000],[27.000,48.000,31.000],[21.000, 47.000,27.000],[22.000, 21.000, 97.000],[22.000, 20.000, 97.000],[22.000, 20.000, 95.000]])
This is only a small extract of the coordinates, because th...

1

votes

1

answer

32

Views

### Remove all rows preceding the first occurence null string in a pandas dataframe column

if(df_TA.index[df_TA['GSNumber'].isnull()][0] is None):
df_TA=df_TA.loc[:]
else:
idx=df_TA.index[df_TA['GSN'].isnull()][0]
df_TA=df_TA.loc[:idx-1]
I have many dataframes some of them contain null in a column followed by columns having value which is not required.I want to remove those extra occurenc...

1

votes

1

answer

150

Views

### Equivalent API of `tf.norm` of Tensorflow 1.+ for Tensorflow0.11

I want to run this code. It's written in Py3 and TF v1.4+ However it throws a an error. The tf.norm in Discriminator_Regularizer function doesn't exist in TF 0.11. I couldn't find the equivalent in TF v0.11. I can't upgrade for some CUDA/Cudnn build/install issues.
I used numpy.linalg.norm instead...

1

votes

0

answer

190

Views

### Sudoku solver - slice indices must be integers or None or have an __index__ method

I am trying to write a solver for my Sudoku Solver but I am getting the following error when I ran my sudoku solver. I didn't understand what the problem is. I look other questions but could not fix the problem also.
Below is my code and the problem:
import numpy as np
class SolveSudoku(object):
def...

1

votes

2

answer

69

Views

### Need to add a column to a pd.df using a function from a package - TypeError: 'Series' objects are mutable

Trying to add a column to a pandas dataframe which is calculated using implied_volatility() function from pyvollib (from py_vollib.black.implied_volatility import implied_volatility). All input parameter come from the following df:
In [101]: odf.head()
Out[101]:
Expiry K Type close...

1

votes

3

answer

305

Views

### How to delete the lowest number in an array, and if there's several minimum numbers, delete the first

I'm trying to make a script, where the input is an array with random numbers. I try to delete the lowest number in the array which is no problem. But if there are several occurrences of this number in the array, how do I make sure that it is only the first occurrence of this number that gets deleted...

1

votes

0

answer

214

Views

### liac-arff to read arff file TypeError

i want to read arff file but i am getting this error. Could any one help me with this
import arff, numpy as np
dataset = arff.load(open('ckdfull.arff', 'rb'))
data = np.array(dataset['data'])
Traceback (most recent call last):
File 'C:/Users/username/PycharmProjects/ckd/ckd.py', line 2, in
dataset...