I have a Numpy array of datetime64 objects that I need to convert to a specific time format yyyy-mm-dd,HH:MM:SS.SSS Numpy has a function called datetime_as_string that outputs ISO8601 (yyyy-mm-ddTHH:MM:SS.SSS) time, which is extremely close to what I want, the only difference being there is a T where I want a comma. Is there a way to quickly swap the "T" for a ","? Here is an example data set: offset = np.arange(0, 1000) epoch = np.datetime64('1970-01-01T00:00:00.000') time_objects = epoch + offset.astype('timedelta64[ms]') time_strings = np.datetime_as_string(time_objects) I have had success
Unluckily it is terribly similar to: Filter a numpy array if any list within it contains at least one value of a previous row which is a question I asked some minutes ago. In this case I have a list b = np.array([[1,2], [1,8], [2,3], [4,2], [5,6], [7,8], [3,3], [10,1]]) What I want to do is slightly different now. I want to start at the beginning of the list and for each subarray. I want to check whether the element in position i (with respect to the subarray) is encountered in position i also in other subarrays. Hence, removing all such elements. For instance: Look at [1,2]: eliminate [1,8]
I have the following minimal example: a = np.zeros((5,5,5)) a[1,1,:] = [1,1,1,1,1] print(a[1,:,range(4)]) I would expect as output an array with 5 rows and 4 columns, where we have ones on the second row. Instead it is an array with 4 rows and 5 columns with ones on the second column. What is happening here, and what can I do to get the output I expected?
Split a NumPy array into subarrays according to the values (not sorted, but grouped) of another array
Suppose I have two NumPy arrays x = [[1, 2, 8], [2, 9, 1], [3, 8, 9], [4, 3, 5], [5, 2, 3], [6, 4, 7], [7, 2, 3], [8, 2, 2], [9, 5, 3], [10, 2, 3], [11, 2, 4]] y = [0, 0, 1, 0, 1, 1, 2, 2, 2, 0, 0] Note: (values in x are not sorted in any way. I chose this example to better illustrate the example) (These are just two examples of x and y. values of x and y can be arbitrarily many different numbers and y can have arbitrarily different numbers, but there are always as many values in x as there are in y) I want to efficiently split the array x into sub-arrays according to the values in y. My
I am trying to use scipy.optimize.newton_krylov() to solve a least-squares optimization problem, i.e. finding x such that (Ax - b)**2 = 0. My understanding is that A has to be mxn with m>n, b has to be mx1, and x will be nx1. When I try to run the optimization, I get an error: ValueError: expected square matrix, but got shape=(40, 6) Presumably this error concerns the computation of the Jacobian and not my input matrix A? But if so, how can I change the values I am providing to the functions to resolve this problem? Any advice would be appreciated. The following code reproduces the error
Another of my queries about an embedded Python 2.7.3 interpreter on Windows. I need to install numpy and matplotlib as support packages for an embedded interpreter in a C++ program compiled in Visual Studio 2010. Some background: I've been trying to make this work for a few months. The main issue is that numpy for Python 2.7 is compiled against an earlier version of the MS C library (VS2008) whereas my application is compiled against the 2010 version (a fact beyond my control - that is the office compiler). I worked around this by fiddling with the manifest for my application to let it load
I have two Numpy arrays which look like this: a = [[ [1,2,3], [4,5,6] ], [ [7,8,9], [10,11,12] ]] b = [[ [1,1,1], [0,0,0] ], [ [3,3,3], [4,4,4] ]] I want to perform c = a - b with condition that c = 255 if b>0 else a So c should be like this: c = [[ [255,255,255], [4,5,6] ], [ [255,255,255], [255,255,255] ]] How to do it efficiently without any loop?
I spent more time on this issue than I am willing to admit. I have a function called: def array_funct(filename): ... data = np.array((array)) return data which reads in .txt files from a folder and returns a numpy array. The first row is a list of x coordinates and second row are the cooresponding y coordinates. Hence I use: array_funct(filename)[:,0] array_funct(filename)[:,1] to access the x and y coordinates. Now all I want to do is to create a for loop which would read in more than 1 file and store them in following way for i in range(0,number_of_files): array_funct(file[i])[:,0] array
Is there a numpy function that efficiently allows nwise iteration? # http://seriously.dontusethiscode.com/2013/04/28/nwise.html from itertools import tee, islice nwise = lambda xs, n=2: zip(*(islice(xs, idx, None) for idx, xs in enumerate(tee(xs, n)))) Eg. Apply mean to the elements nwise? To get a moving average?
问题 使用 Python，如何从多元对数正态分布中采样数据？ 例如，对于多元法线，有两个选项。 假设我们有一个 3 x 3 协方差矩阵和一个 3 维均值向量 mu。 # Method 1 sample = np.random.multivariate_normal(mu, covariance) # Method 2 L = np.linalg.cholesky(covariance) sample = L.dot(np.random.randn(3)) + mu 我发现 numpy 的 numpy.random.lognormal，但这似乎只适用于单变量样本。 我还注意到 scipy 的 scipy.stats.lognorm。 这似乎确实具有多变量样本的潜力。 但是，我无法弄清楚如何做到这一点。 回答1 多元对数正态分布随机变量Rv应具有此属性： log(Rv)应遵循正态分布。 因此，问题实际上只是生成一个多元正态分布的随机变量和np.exp它。 In : import numpy.random as nr In : cov = np.array([[1.0, 0.2, 0.3,], [0.2, 1.0, 0.3,], [0.3, 0.3, 1.0]]) In : mu = np.log([0.3, 0.4, 0.5]) In : mvn = nr