# Numpy Questions

Ad

## How do I resolve this make fatal error: numpy/arrayobject.h: No such file or directory?

I have the following snippet in my makefile (the first line was added by me after finding it as a candidate solution online -

## Fastest way to get dot product of a matrix and each point in the given array

I have an array nx3 of n points, each one has x, y and z coordinate. i need to rotate each point, so i have the rotation matrix 3x3. to do this, i

## What is the Fastest way to compare values across columns in pandas (Python)

I have the following dataframe: import numpy as np import pandas as pd df = pd.dataframe(np.array([[1, 1, 1, 1], [1, 1, np.nan,

## Fastest way to find 4-connected regions

I have a 2d numpy array filled with values in range [0,1] and i want to loop on the 4-connected region where value < 0.2. in order

## How to find the average of the differences between all the numbers of a Python List

I have a python list like this, arr = [110, 60, 30, 10, 5] what i need to do is actually find the difference of

## Integrating dynamic c lib.so to python

I was trying to integrate shared library written in c to already running python application. for that i have created a simple .so file and trying

## Rearranging data from a table in python to correlate columns

I got a table that look like this: code year

## Convert a numpy bool array to int

I have a numpy array (dtype bool) representing an array of bits. for example, the array np.array([true, false, false], dtype=bool)

## Replace characters with a space within a column while maintaining original value (In Pandas)

I have a dataset, where, in one column i would like to replace the '.' with a ' ' or space. note - the date is an object type

## How to reshape a this list vertical to horizontal

I have a list arranged like this: 0 [1, 6] 1 [2, 7] 3 [3, 8] 4 [4, 9] 5 [5, 10]

Ad

## Python Array Computations: get two values per item

I'm trying to get a list with all numbers that are in the form 6n+1 or 6n-1. currently i have this: n = 100000000 l = int(n/6) f1 =

## Assign value to subset of numpy array

Given the following array: x = np.zeros((1, 5)) x # array([[0., 0., 0., 0., 0.]])

## Run and access for loop over numpy array with multiplication and division in python

I have following list of arrays. now i would like to leave the first elements of each array as it is and then multiply each first element within

## applying a mask to matrices gives different objects in numpy

I've mostly worked with matlab, and i am converting some of my code that i have written into python. i am running into an issue where i have a

## Not defined variable in numpy

I'm trying to run the code below but i get the same when running the code. the issue is that q seems to not be defined but this should not be

## Inequality condition as argument of np.any() function

I'm trying to run code containing the following: if np.any(1.5<=np.abs(array1 - array2)<=2): return true

## How to capitalize first letter of multiple string in a column using pandas

I have a dataframe with 2 columns in it as follows, street city 8188 e pinnacle cir gold canyon 6701 s

## flatten list of numpy arrays while keeping inner arrays

L = [np.array([[1,2],[3,4]]), np.array([5,6]), np.array([[7,8],[9,10],[11,12]])] i'm trying to flatten this list of

## normalize the rows of numpy array based on a custom function

I have an numpy array. i want to normalized each rows based on this formula x_norm = (x-x_min)/(x_max-x_min) , where

## How to pull data from python pandas df1 to df2 based on values in each row of df2, sort of like a VLOOKUP nested in an HLOOKUP in Excel

Assume i have two dataframes, df1 and df2, described as follows. see code below that creates each of these dfs. df1

## Getting the location of indices missing from secondary DataFrame

Please examine the commented text in the code below in order to understand the problem. import pandas as pd import numpy as np

Ad

## Cases where numpy `mean` is more computationally efficient vs executing the math via python code

This is a followup to this so answer

## Numpy: Using two different sets of boolean masks at once using advanced indexing

Say that i have this array import numpy as np a = np.array( [ [[1,2], [3,4], [5,6]], [[11,21], [31,41], [51,61]],

## How to flip iamge in opencv without library functions in python

I am trying to flip an image in python but cannot use cv2.flip() as per my design brief. dose anyone has any ideas to help me? here is my code so

## Fastest way to duplicate 2D numpy array then insert zeros?

I tried looking for this problem extensively on stackoverflow but i couldn't find anything. i am coding some algorithms on a drone that need to be

## Is there a way to vectorize counting items' co-occurences in pandas/numpy?

I frequently need to generate network graphs based on the co-occurences of items in a column. i start of with something like this:

## cumulative sum in numpy array with condition for stop

I want to optimize my numpy code, im using large arrays so efficiency is required. i tried to omit using for-looop if possible. let`s assume

## Return mask from numpy isin function in 1 dimension

I am trying to use numpy's function isin to return a mask for a given query. for example, let's say i want to get a mask for element 2.1 in the

## How can I convert a SymPy expression to NumPy?

How can i convert a sympy expression to numpy code? for example, say i this was the code for the expression: expression = 2 * x/y +

## Convert numpy array to a range

I have a sorted list of numbers: [ 1, 2, 3, 4, 6, 7, 8, 9, 14, 15, 16, 17, 18, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,

## Passing objects by reference in python with ctypes

I am trying to use ctypes to interface with c code. i am trying to pass obj by reference to c and i am using ndpointer function from numpy to pass

## Change every n-th element of a row in a 2d numpy array depending on the row number

I have a 2d array: h = 12 a = np.ones([h, h]) print(a.astype(int)) [[1 1 1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1 1 1] [1 1 1 1

Ad

## Appending powers of a row at the and of the same row in numpy matrix

I have a numpy matrix in. i want to append the second power, third power, ..., nth power of the row to the end of the row. for instance,

## re-arranging a numpy array to 1d

Here is a numpy array i call this_col = [18 18 18 ... 24 24 24] i have tried to reshape my data in many ways

## Setting a seed for numpy uniform doesn't result in the same probabilities

Why doesn't the below shown code result in 3 arrays with the same probabilities? how can i generate reproducible probabilities?

## Graphing a dictionary with strange formatting in Python

I have a dictionary in python like so: test={'fg':['1','2','3','4','5'],

## Fillna/null Values Base on Below Line Based on Conditions in Pandas

I have a huge pandas data frame just like below. d = {'id1': ['85643',

## Unable to run PCA on a dataset

I am trying to run pca on the loan dataset - find

## how to make code faster by removing for loop

I am trying to do analysis of large amount of data , first i applied groupby function to divide data in different groups. then i check some

## [Pyinstaller]: Working code produces ModuleNotFoundError when turned to exe

Working with pyinstaller 3.5 and python 3.6 (installed with anaconda). i have a python code that explicitly imports numpy and works fine

## Pandas - Mapping dict with multiple index to column

I have two large data sets which i can't do the aggregations by combining two dataframes. i have to do the aggregation on df_train

## Pythonic way of replacing nan sub-array with array of same size

Suppose i have given a numpy array like this: a = np.array([1, 2, 3, 4, np.nan, 5, np.nan, 6, np.nan]) # [1, 2, 3, 4,

## Numpy: crop object of interest by removing x and y axis that are black

Problem: i have some images (in numpy) that have a black background. in the middle, i have an object that i am interested in. i would like to crop

Ad

## Drop rows that contains the data between specific dates

The file contains data by date and time: all i want i want drop rows that contains between these dates and includes the start and end dates:

## How to get the column header of a particular column in numpy

I have the following data admit_data = np.genfromtxt('/content/drive/my drive/colab/admission_predict.csv',

## Join columns and reshape in row

I have data like this: data = {'host' : ['a','a','a','a','a','a','b','b','b'], 'duration' :

## Python - Scipy: multivariate_normal - select the right subsets of input

Any help that pushes me towards the right solution is greatly appreciated... i am trying to do a classification in two steps: 1.)

## Element-wise division with accumulated numbers in Python?

The title may come across as confusing (honestly, not quite sure how to summarize it in a sentence), so here is a much better explanation:

## How to call `__rsub__` properly with a numpy array?

Consider the following snippet: import numpy as np class myclass: def __rsub__(self,

## nested loop in python with lists input

I want to have an array of signals which calculated from a function, by loop through different parameter combination, something like below:

## Numeric differences between two different dataframes in python

I would like to find the numeric difference between two or more columns of two different dataframe. the following

## Finding the distance to the next higher value in pandas dataframe

I have a data frame containing floating point values my_df = pd.dataframe([1,2,1,4,3,2,5,4,7]) i'm trying to

## Select rows from Dataframe and include another column python

I an new to python, how can i select columns from dataframe which end with _old and include b column as final

## how to convert array to dataframe in column only

I have tried to convert array to dataframe, and follow the documentation how to convert them. here my script; m =

Ad

## Pyaudio / Numpy Concatenated Notes Merely Extend the First Note

I'm trying to generate my own notes using pyaudio, but i'm running into what is surely a beginner's mistake. i can generate pure sin wav tones and

## Numpy: np.finfo does not fail as one would expect

I am using numpy.iinfo and np.finfo to test whether a given type or value is corresponds to an integer or a float. there is a weird behaviour when

## Numpy array computation slower than equivalent Java code

I am trying to work with large 2d arrays in python, but it's very slow. for example: start =

## numpy apply function over two 3d matrixes

So i would like to apply a function over two 3d matrixes with numpy and i can't figure out how. i read about numpy.apply_over_axes() but can't

## Get the coordinates of non-zero binary mask in certain range

I'm trying to get the non-zero values of a binary mask(that has been indexed by a certain range, the range here defined by the lower and upper x,y

## Making a 0 to nxn matrix and reshape it to pairs

Maybe i am wrong and i don't know how to search for it. i need to get a list of pairs (0,0) (0,1) (0,2) ... (0,999) ...

## how to delete a row from a matrix

I have 2 matrices: matrix_1 and matrix_2 matrix_1 = np.array[[1,2,3],[4,n,6],[7,8,9],[n ,1,2]] matrix_2 =

## Extracting all second column values from a ndarray for a given first column value

I have a numpy ndarray where first column is user id and second column is some product id. what would be the fastest way to get all product ids

## TypeError: 'builtin_function_or_method' object is not subscriptable for python

I'm writing some code to medial axis transform and got some type error for my array. import cv2 import numpy as np img =

## How to quickly label int ranges with a string?

I want to label "fare" quantils bands automatically as shown below. my data looks like: df.head() passengerid survived

## Faster way to threshold a 4-D numpy array

I have a 4d numpy array of size (98,359,256,269) that i want to threshold. right now, i have two separate lists that keep the coordinates of the

Ad

## Comparing two arrays using a if loop

I am trying to compare if one array is greater then the other array using a if statement. from scipy.stats import laplace from

## Speeding up operations on large arrays & datasets (Pandas slow, Numpy better, further improvements?)

I have a large dataset comprising millions of rows and around 6 columns. the data is currently in a pandas dataframe and i'm looking for the

## Replacing the value in a column based on a single vectorized condition

I tried the following code with multiple conditions and worked perfectly, somehow whatever i am trying with a single condition it won't work. it

## Numpy log2 returning NaN for small values

I've the following data frame with all positive values v1 v2 v3 v4 v5 0 f1h5n4s2 10.751263

## How to load images from memory to numpy using file system

I want to store my image directory in memory, then load the images into a numpy array. the normative way to load images that are

## Get the value at a position from all layers in python

I have 3 numpy arrays of shape (224, 224, 20). i want to go through each of (224, 224) values in all 20 layers

## How to count length of missing values for string variables as zero?

I'm trying to count the length of object variables in a dataframe with python. a lot of my variables are string with missing values and

## Numpy throwing TypeError: cannot unpack non-iterable NoneType object

I have code directly copy pasted from

## How to find correlation between two images using Numpy

This is inspired by this question. i'm trying to find correlation between two

## Result of function linspace

Could you explain me this result please? i expected value as [0, 0.1, 0.4, 0.9, ...]. >>> np.linspace(0, 1,

Ad

## How to avoid for-loop while using append()

First of all, i apologize for being an absolute beginner in both python and numpy. please forgive my ignorance. i have a 4d cube of

## Numba dictionary: signature in JIT() decorator

My function takes a list of numpy arrays and a dictionary (or a list of dictionaries) as input arguments and returns a list of values. the list of

## Change of behavior of a function to compute multiple numpy arrays mean

I have three bgr color values (stored into numpy arrays) and i want to compute their mean value (of each element, as to have a numpy array looking

## Merge array lists into one array list

I spent more time on this issue than i am willing to admit. i have a function called: def array_funct(filename): ... data =

## Numpy arrays; How to replace elements with another array based on conditions?

Given two numpy arrays: import numpy as np a = np.array([[0, 5, 0], [1, 0, 1],

## Use np.where on a Mixed data type column

I have a column with data type 'o'. it has numbers, as well string. for example:

## Python: scipy/numpy all pairs computation between two 1-D vectors

I have two lists l1 and l2 containing integers that may be of different lengths, and i want to perform a computation

## Generator not being recognized when passing validation data to .fit in Keras Sequential

The exact error: valueerror: when passing validation_data, it must contain 2 (x_val, y_val) or 3 (x_val, y_val,

## Copying numpy array with '=' operator. Why is it working?

According to this answer, b=a where a is a numpy array,

## Getting length of fractional part of a decimal

I want to get length of fractional part ex : 0.063 - 3 , 0.04 -2 i got fractional part using this df['fractional_part'] =

## Saving variables in Python as columns without brackets

I have a code which returns some variables that i would like to later on use in another program. however, the output doesn't look like i want it

Ad

## Convert a tensor to numpy array in keras

I want to convert the tensor of shape (?,224,224,3) to a numpy array in keras. tensor("add_1/add_3:0", shape=(?, 224, 224, 3),

## Is there a memory-efficient way of inserting zero-columns and rows into a numpy array?

I have a big (symmetric) numpy matrix arr. it is of shape arr.shape = (50_000, 50_000). i want to insert some

## Reversing the view of a numpy array twice in a jitted function makes the function run faster

So i was testing the speeds of two versions of the same function; one with reversing the view of a numpy array twice and one without. the code is

## convert image hash to python built in int

i am working on an image hashing project in opencv and python. i am reading this tutorial

## How to replace variables with names using pandas

Consider a column(result) in a data frame df which is stored with 0's and 1's. 0 indicates fail and 1 indicates pass. how do i

## pd.DataFrame.agg(np.var) vs pd.Series.np.var

Using np.var() in two ways to the same dataset but they are giving two different results. do not think it's because of n & n-1 issue since

## Numpy function that rounds to specified integer to specified range

Lets say i have an array y which contains continuos numbers which mostly go from -1 to 5 with probably some outliers but i want to fix them to

## Multithreading a numpy nditerator

For an mcmc implementation, i want to calculate the covariance tensor c in numpy. working single-threaded code the distance

## Using subprocess.run how to save the data of each loop in a different file?

In the following code i want to save data of each loop in a different file. can we store data in numpy format? #my data format #0

## Use each neighbor once in sklearn NearestNeighbor

I am comparing two point clouds of different sizes. i don't want to cutoff the last points in the larger pointcloud pc1. for points in pc1 i would

## How to sparsify a matrix/array in python

I have a simple "matrix" in python that i would like to sparsify: np.array([[5.72756493, 0. , 0. ],

Ad

## Is there a way to not make a copy when a numpy array is sliced?

I need to handle some large numpy arrays in my project. after such an array is loaded from the disk, over half of my computer's

## Getting ValueError when performing np.dot

I have to find annual returns and volatility of a portfolio. i have a dataframe with 5 columns, each containing the closing price of a stock

Ad

## Blog Categories

Ad