numpy-ndarray

How to count frequency of a element in numpy array?

北慕城南 提交于 2019-12-24 07:58:16
问题 I have a 3 D numpy array which contains elements with repetition. counterTraj.shape (13530, 1, 1 For example counterTraj contains such elements: I have shown few elements only: array([[[136.]], [[129.]], [[130.]], ..., [[103.]], [[102.]], [[101.]]]) ``` I need to find frequency of different element: Example: 136 count 5 (say), 101 count 12 (say). The array elements are not fixed and changes with input data. I try following: from collections import Counter Counter(counterTraj) Following error

How to check if a Numpy array is a subarray of another bigger array

老子叫甜甜 提交于 2019-12-24 07:34:51
问题 So basically I have two arrays, and I want to check if one array is in another... I'm looking for a way to do something like this: >>> arr1 = np.array([1, 0, 0, 1, 1, 0]) >>> arr2 = np.array([0, 0, 1, 1, 1, 0]) >>> test_array = np.array([1, 1, 1]) >>> test_array in arr1 ... False >>> test_array in arr2 ... True Is there any way to solve do something like this? Thanks. 回答1: The most intuitive way seems to be an iterative process like so: def isSubset(arr1, arr2): m = len(arr1) n = len(arr2)

boost python - nullptr while extracting ndarray

夙愿已清 提交于 2019-12-24 07:07:22
问题 I have a C++ code which execute python script with boost_python package. Everything is fine, as longa as I extract int, string, or other not-array variables from python. However I have to extract a numpy::ndarray and convert it to cpp vector . I tried as follow: main.cpp #include <iostream> #include <boost/python.hpp> #include <boost/python/numpy.hpp> using namespace boost::python; int main() double t_end=7 try { Py_Initialize(); object module = import("__main__"); object name_space = module

How to perform operations on cvxopt-matrices a la numpy?

♀尐吖头ヾ 提交于 2019-12-24 06:33:34
问题 I am working with cvxopt matrices in order to use them in picos library. In general I want to take a matrix, evaluate it on a certain vector, subtract something, then take the biggest absolute value of its entries import picos as pic import cvxopt as cvx import numpy as np (...) P = pic.Problem() theta = P.add_variable('theta', size=k, vtype='continuous', lower=-10, upper=10) theta P.add_constraint(max(abs(M*theta - b)) <= 5) P.minimize(theta) (Here b is some vector treated as cvxopt matrix.)

Confusion about numpy strides

大憨熊 提交于 2019-12-24 00:53:05
问题 I am looking at the answer to this question and can't wrap my head around how the as_strided function is viewing this array. This piece of code is part of the answer: >>> a = np.lib.stride_tricks.as_strided(np.array([1, 512, 0, 3], dtype=np.int16), shape=(3,), strides=(3,)) >>> a array([1, 2, 3], dtype=int16) >>> a.strides[0] 3 >>> a.itemsize 2 Assuming each element of the passed array is 2 bytes long, we have the following byte representation of the array: -----------------------------------

get ride of Recursion Error in large number of iteration and access to element of joint of 2 lists iteratively python

一世执手 提交于 2019-12-23 04:55:12
问题 I need to do iteratively (about 3 millions times) X=[X B] which B=[b1,b2,b3,b4,b5] and save this in a file to access in future. I encounter two problems: 1- The first problem is that the size of X is size=(2,) and I do not know how to access different element (i.e. b1..b5 of each time) because all of them are supposed to be element number zero of each time! For solving this problem I have tried to use X.append(B) but I saw an error. I have tried to use X.append(B[0])...X.append(B[4]) but the

Creating this numpy array in Python

空扰寡人 提交于 2019-12-22 13:58:47
问题 I have the following numpy array import numpy as np a = np.array([1,2,6,8]) I want to create another numpy array from a such that it contains all the different possible sums of TWO elements of a . It's easy to show then that there are int(a.size*(a.size-1)/2) different possible sums, composed from: a[0] + a[1] a[0] + a[2] a[0] + a[3] a[1] + a[2] a[1] + a[3] a[2] + a[3] How can I construct a numpy array with the above sums as elements without using a double for loop (the only way I can think

Constructing a ndarray of values from another ndarray containing dictionary keys

杀马特。学长 韩版系。学妹 提交于 2019-12-22 12:31:52
问题 I have a ndarray containing the dictionary keys arranged in a specific order. I want to create another ndarray containing the values of the respective keys. The order have to be maintained. Obvious approach is to iterate over the array containing the keys element by element but the problem is there is no way to know the shape of the array beforehand. Is it possible to flatten the ndarray of keys and iterate over it to generate flat ndarray of values and finally unravel it without harming the

How to convert an opencv Mat into a numpy.ndarray?

我的未来我决定 提交于 2019-12-20 03:52:20
问题 I have a code written in java (android) that open the camera of the phone and show frames. The below code represents the method in which we can retrieve frames. The project used Chaquopy to interpret python code. @Override public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) { mRgba = inputFrame.rgba(); Python py = Python.getInstance(); PyObject pym = (PyObject) py.getModule("MyPythonClass").callAttr("call",mRgba); return mRgba; } The python code is used to retrieve the

How do I calculate percentiles with python/numpy?

余生长醉 提交于 2019-12-17 04:41:48
问题 Is there a convenient way to calculate percentiles for a sequence or single-dimensional numpy array? I am looking for something similar to Excel's percentile function. I looked in NumPy's statistics reference, and couldn't find this. All I could find is the median (50th percentile), but not something more specific. 回答1: You might be interested in the SciPy Stats package. It has the percentile function you're after and many other statistical goodies. percentile() is available in numpy too.