numpy

Parallelize loop over numpy rows

对着背影说爱祢 提交于 2021-02-07 14:35:11
问题 I need to apply the same function onto every row in a numpy array and store the result again in a numpy array. # states will contain results of function applied to a row in array states = np.empty_like(array) for i, ar in enumerate(array): states[i] = function(ar, *args) # do some other stuff on states function does some non trivial filtering of my data and returns an array when the conditions are True and when they are False. function can either be pure python or cython compiled. The

Parallelize loop over numpy rows

大憨熊 提交于 2021-02-07 14:34:21
问题 I need to apply the same function onto every row in a numpy array and store the result again in a numpy array. # states will contain results of function applied to a row in array states = np.empty_like(array) for i, ar in enumerate(array): states[i] = function(ar, *args) # do some other stuff on states function does some non trivial filtering of my data and returns an array when the conditions are True and when they are False. function can either be pure python or cython compiled. The

Retrieving facets and point from VTK file in python

落爺英雄遲暮 提交于 2021-02-07 14:31:31
问题 I have a vtk file containing a 3d model, I would like to extract the point coordinates and the facets. Here is a minimal working example: import vtk import numpy from vtk.util.numpy_support import vtk_to_numpy reader = vtk.vtkPolyDataReader() reader.SetFileName('test.vtk') reader.Update() polydata = reader.GetOutput() points = polydata.GetPoints() array = points.GetData() numpy_nodes = vtk_to_numpy(array) This works as numpy_nodes contains the x,y,z coordinates of all points, but I am at loss

python how to put argument to function with numpy aply_along_axis

百般思念 提交于 2021-02-07 14:20:50
问题 I want to apply function to every column in matrix. I would like to use function with arguments but I don't know how to do it, things I tried ends with an error. code I am runnung import numpy as np M = np.array([[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]]) def my_function(arr, arg="default"): print arg return arr def my_function_allong_axis(M, argument): return np.apply_along_axis(my_function, axis=0, arr=M, arg=argument) my_function_allong_axis(M, "something else") this will produce

Checking user input using isnan function of NumPy

安稳与你 提交于 2021-02-07 14:20:21
问题 I'm trying to use NumPy to check if user input is numerical. I've tried using: import numpy as np a = input("\n\nInsert A: ") if np.isnan(a): print 'Not a number...' else: print "Yep,that's a number" On its own t works fine, however when I embed it into a function such as in this case: import numpy as np def test_this(a): if np.isnan(a): print '\n\nThis is not an accepted type of input for A\n\n' raise ValueError else: print "Yep,that's a number" a = input("\n\nInsert A: ") test_this(a) Then

What is the meaning of all abbreviations in wheel filename notation on Christoph Gohlke's website?

流过昼夜 提交于 2021-02-07 14:20:00
问题 I wanted to install OpenCV for Python3 on my machine following this tutorial, but I'm faced with a problem of numpy version notation on Christoph Gohlke's website , more precisely what does for example numpy‑1.14.2+mkl‑cp36‑cp36m‑win32.whl notation mean, I don't understand what version I have to download. Which one of versions listed here do I install based on my system specs? I've read through the header introduction on Christoph Gohlke's website but didn't find any explanation. 回答1: numpy‑1

How to multiply every column of one Pandas Dataframe with every column of another Dataframe efficiently?

谁说胖子不能爱 提交于 2021-02-07 13:56:40
问题 I'm trying to multiply two pandas dataframes with each other. Specifically, I want to multiply every column with every column of the other df. The dataframes are one-hot encoded, so they look like this: col_1, col_2, col_3, ... 0 1 0 1 0 0 0 0 1 ... I could just iterate through each of the columns using a for loop, but in python that is computationally expensive, and I'm hoping there's an easier way. One of the dataframes has 500 columns, the other has 100 columns. This is the fastest version

get index of the first block of at least n consecutive False values in boolean array

我是研究僧i 提交于 2021-02-07 13:54:11
问题 I have a numpy boolean array w=np.array([True,False,True,True,False,False,False]) I would like to get the index of the first time there are at n_at_least false values. For instance here `n_at_least`=1 -> desired_index=1 `n_at_least`=3 -> desired_index=4 I have tried np.cumsum(~w) which does increase every time a False value is encountered. However, when True is encountered the counter is not starting from 0 again so I only get the total count of False elements rather than the count of the

Fastest way to create and fill huge numpy 2D-array?

主宰稳场 提交于 2021-02-07 13:47:56
问题 I have to create and fill huge ( e.g. 96 Go, 72000 rows * 72000 columns) array with floats in each case that come from mathematical formulas. The array will be computed after. import itertools, operator, time, copy, os, sys import numpy from multiprocessing import Pool def f2(x): # more complex mathematical formulas that change according to values in *i* and *x* temp=[] for i in combine: temp.append(0.2*x[1]*i[1]/64.23) return temp def combinations_with_replacement_counts(n, r): #provide all

Fastest way to detect the non/least-changing pixels of successive images

笑着哭i 提交于 2021-02-07 13:47:10
问题 I want to find the pixels of a video stream that are static. This way I can detect logos and other non-moving items on my video stream. My idea behind the script is as follows: collect a number of equally-sized and graysized frames in a list called previous if a certain amount of frames is collected, call the function np.std This function loops over all the x- and y-coordinates of a new image. Calculate the standard deviation of the grayvalues for all the coordinates based on the grayvalues