numpy

Python: numpy.dot / numpy.tensordot for multidimensional arrays

蓝咒 提交于 2021-02-10 15:14:00
问题 I'm optimising my implementation of the back-propagation algorithm to train a neural network. One of the aspects I'm working on is performing the matrix operations on the set of datapoints (input/output vector) as a batch process optimised by the numpy library instead of looping through every datapoint. In my original algorithm I did the following: for datapoint in datapoints: A = ... (created out of datapoint info) B = ... (created out of datapoint info) C = np.dot(A,B.transpose()) _________

Assigning elements in a numpy array of zeros

给你一囗甜甜゛ 提交于 2021-02-10 14:51:08
问题 I am trying to draw a "one" on a numpy array np.zeros((28, 28)) by assigning certain rows and columns to 255. I have written the following code: one = np.zeros((28, 28)) one[12:15][5:23] = 255 The output I'm getting after this is a simple array of zeros with no changes. Can anyone please explain this strange behavior? Bonus If you interchange [12:15] and [5:23] , rows 17 to 19 are filled with 255. 回答1: The notation you are using is valid but does something very different from what you expect.

Function returns a vector, how to minimize in via NumPy

为君一笑 提交于 2021-02-10 14:50:25
问题 I'm trying to minimize function, that returns a vector of values, and here is an error: setting an array element with a sequence Code: P = np.matrix([[0.3, 0.1, 0.2], [0.01, 0.4, 0.2], [0.0001, 0.3, 0.5]]) Ps = np.array([10,14,5]) def objective(x): x = np.array([x]) res = np.square(Ps - np.dot(x, P)) return res def main(): x = np.array([10, 11, 15]) print minimize(objective, x, method='Nelder-Mead') At these values of P, Ps, x function returns [[ 47.45143225 16.81 44.89 ]] Thank you for any

Unable to plot an accurate tangent to a curvature in Python

谁都会走 提交于 2021-02-10 14:21:16
问题 I have a dataset for curvature and I need to find the tangent to the curve but unfortunately, this is a bit far from the curve. Kindly guide me the issue solution related to the problem. Thank you! My code is as follows: fig, ax1 = plt.subplots() chData_m = efficient.get('Car.Road.y') x_fit = chData_m.timestamps y_fit = chData_m.samples fittedParameters = np.polyfit(x_fit[:],y_fit[:],1) f = plt.figure(figsize=(800/100.0, 600/100.0), dpi=100) axes = f.add_subplot(111) # first the raw data as a

how to load Matlab's struct (saved with v7.3) in Python

谁都会走 提交于 2021-02-10 14:19:35
问题 I created a 1X20 struct in Matlab. This struct has 9 fields. The struct is saved in -v7.3 version because of its dimensions (about 3 Giga). one of the fields contains 4D matrix, other contain cell arrays, meaning it is a complex struct. I would like to know if there is a way to load this struct into Python? 回答1: MATLAB v7.3 uses HDF5 storage; scipy.io.loadmat cannot handle that MATLAB: Differences between .mat versions Instead you have to use numpy plus h5py How to read a v7.3 mat file via

Iterating a Numpy array through arithmetic functions

与世无争的帅哥 提交于 2021-02-10 14:16:29
问题 In the code I have gotten from my previous issue: issue I could use iterations whilst modifying the a value of multiplication. I want to use the .prod function but with iterations of multiplication, division and addition. The calculations will go as follows, for the first calculation 10 + 10 *50/100 = 15 with the equation (Starting_val + Starting_val * Random_numb/100) . The first element in Random_numb is 50 and Starting_val is updated to the value of 15. So for the second calculations it

Calculate partitioned sum efficiently with CuPy or NumPy

强颜欢笑 提交于 2021-02-10 14:16:24
问题 I have a very long array* of length L (let's call it values ) that I want to sum over, and a sorted 1D array of the same length L that contains N integers with which to partition the original array – let's call this array labels . What I'm currently doing is this ( module being cupy or numpy ): result = module.empty(N) for i in range(N): result[i] = values[labels == i].sum() But this can't be the most efficient way of doing it (it should be possible to get rid of the for loop, but how?).

Getting the first occurrence of a value in an N-dimensional numpy array

别说谁变了你拦得住时间么 提交于 2021-02-10 13:08:10
问题 I have seen this question, but want to reduce the array created from mask = array == value mask = array([[[ True, True, True], [False, True, True]], [[False, True, True], [False, True, True]], [[False, False, True], [False, True, True]]]) which results in where(mask) = (array([0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2]), array([0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1]), array([0, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2])) and I want to reduce it to an array of the first occurrences of True array([[0, 1], [1, 1],

Getting the first occurrence of a value in an N-dimensional numpy array

流过昼夜 提交于 2021-02-10 13:07:47
问题 I have seen this question, but want to reduce the array created from mask = array == value mask = array([[[ True, True, True], [False, True, True]], [[False, True, True], [False, True, True]], [[False, False, True], [False, True, True]]]) which results in where(mask) = (array([0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2]), array([0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1]), array([0, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2])) and I want to reduce it to an array of the first occurrences of True array([[0, 1], [1, 1],

Why do negative numpy.float64 yield nan when expontiated with a fractional number?

痞子三分冷 提交于 2021-02-10 12:34:06
问题 A negative numpy.float64 exponentiated with a fractional (i.e., decimal, i.e., rational) number will yield a nan result and a warning. The same number using Python's float type returns a complex result. Here is a minimal example using Python 3.6.6 (for a comment on Python 2.7.15, see below): >>> import numpy as np >>> f = -2.0 >>> npf = np.float64(-2.0) >>> f**1.1 (-2.0386342710747223-0.6623924280875919j) >>> npf ** 1.1 __main__:1: RuntimeWarning: invalid value encountered in double_scalars