numpy

Installing pygame on ubuntu

一笑奈何 提交于 2021-01-29 04:42:33
问题 I am trying to install pygame on an Ubuntu 16.04 system. My default python is 2.7.12. I opened terminal and tried: sudo apt-get install python-pygame I got this message: Reading package lists... Done Building dependency tree Reading state information... Done Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming. The

NumPy - Vectorizing bincount over 2D array column wise with weights

江枫思渺然 提交于 2021-01-29 04:31:56
问题 I've been looking at the solutions here and here but failing to see how I can apply it to my structures. I have 3 arrays: an (M, N) of zeros, and (P,) of indexes (some repeat) and an (P, N) of values. I can accomplish it with a loop: # a: (M, N) # b: (P, N) # ix: (M,) for i in range(N): a[:, i] += np.bincount(ix, weights=b[:, i], minlength=M) I've not seen any examples that use indexes in this manner, or with the weights keyword. I understand I need to bring everything into a 1D array to

Getting length of “slices” of NumPy array (or list) without actually slicing

只谈情不闲聊 提交于 2021-01-29 04:16:41
问题 Suppose that I have a NumPy array arr like array([5, 3, 0, 5, 7, 6, 5, 9, 0, 6]) and a few included indices in another array, inds , array([3, 6]) I want to generate an array with the lengths of the subarrays of arr were I to split my array with inds . So in this case, my result would be [3, 3, 4] . I know that I could use np.split to do >>> np.split(arr, inds) [array([5, 3, 0]), array([5, 7, 6]), array([5, 9, 0, 6])] and map size() to get the correct result of [3, 3, 4] from there, but it

python modifying sys.path doesn't work

孤街醉人 提交于 2021-01-29 04:12:31
问题 I have a new numpy version under /opt/lib/python2.7/site-packages , and a standard (system) version under /usr/lib/python2.7/dist-packages . I want to temporarily use the new numpy version so I add the following at the beginning of my script: In [1]: import sys In [2]: sys.path.insert(1,'/opt/numpy/lib/python2.7/site-packages') In [3]: sys.path Out[3]: ['', '/opt/numpy/lib/python2.7/site-packages', '/usr/local/bin', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7', '/usr/lib/python2.7

PyopenCL 3D RGBA image from numpy array

若如初见. 提交于 2021-01-29 03:39:02
问题 I want to construct an OpenCL 3D RGBA image from a numpy array, using pyopencl. I know about the cl.image_from_array() function, that basically does exactly that, but doesn't give any control about command queues or events, that is exposed by cl.enqueue_copy() . So I really would like to use the latter function, to transfer a 3D RGBA image from host to device, but I seem to not being able getting the syntax of the image constructor right. So in this environment import pyopencl as cl import

ValueError: Domain error in arguments scipy rv_continuous

会有一股神秘感。 提交于 2021-01-29 03:17:04
问题 I was trying to sample random variables subject to a given probability density function (pdf) with scipy.stats.rv_continuous: class Distribution(stats.rv_continuous): def _pdf(self,x, _a, _c): return first_hitting_time(x, _a, _c) where the function first_hitting_time is #pdf of first hitting time of W_t + c*t on a. def first_hitting_time(_t, _a, _c=0.0): return _a/_t*np.exp(-0.5/_t*(_a-_c*_t)**2)/np.sqrt(2.0*np.pi*_t) then I continue with myrv= Distribution(name='hittingtime', a=0.002,b=30.0)

Construct 3 dimensions array from two dimensions array using tile

空扰寡人 提交于 2021-01-29 03:15:47
问题 I have an array arr = np.array([0.0, 0.0, 1.0]) I want to contract array of following shape promo_class.shape which is (2,3,3) I want to create repeated array of shape (2,3,3) array([[[0., 0., 1.], [0., 0., 1.], [0., 0., 1.]], [[0., 0., 1.], [0., 0., 1.], [0., 0., 1.]]]) Any idea how to do that with np.tile function ? 回答1: Simply use np.broadcast_to for a view - In [142]: arr = np.array([0.0, 0.0, 1.0]) In [144]: np.broadcast_to(arr,(2,3,3)) Out[144]: array([[[0., 0., 1.], [0., 0., 1.], [0.,

Replace character in numpy ndarray (Python)

痞子三分冷 提交于 2021-01-29 02:48:21
问题 I have a numpy ndarray with 6 elements: ['\tblah blah' '"""123' 'blah' '"""' '\t456' '78\t9'] I am trying to replace all tab characters \t with 4 spaces each so that the numpy array would now be: [' blah blah' '"""123' 'blah' '"""' ' 456' '78 9'] I have considered re.sub but cannot figure out how to implement it when it comes down to an numpy ndarray. Any suggestions/help please? 回答1: You could use NumPy's core.defchararray that deals with string related operations and for this case use

Replace character in numpy ndarray (Python)

我的未来我决定 提交于 2021-01-29 02:45:28
问题 I have a numpy ndarray with 6 elements: ['\tblah blah' '"""123' 'blah' '"""' '\t456' '78\t9'] I am trying to replace all tab characters \t with 4 spaces each so that the numpy array would now be: [' blah blah' '"""123' 'blah' '"""' ' 456' '78 9'] I have considered re.sub but cannot figure out how to implement it when it comes down to an numpy ndarray. Any suggestions/help please? 回答1: You could use NumPy's core.defchararray that deals with string related operations and for this case use

pd.Series.to_list() changing dtype

孤街浪徒 提交于 2021-01-29 02:25:30
问题 When I am programming on colab, I keep running into this issue: Here is my df: 0 1 0 [2.7436598593417045e-05, 3.731542193080655e-05] 1 [8.279973504084787e-05, 2.145002145002145e-05] 2 [0.00022534319714215346, 0.0002031172259231674] 3 [3.239841667031943e-05, 2.7771297808289177e-05] 4 [0.00011311134356928321, 9.428422928088026e-05] I want to get the data from df[1] into a list of lists so I can feed it into my model. To do so, I run: df[1].to_list() and i get: ['[2.7436598593417045e-05, 3