numpy

Save/Load MXNet model parameters using NumPy

不问归期 提交于 2021-01-28 09:36:06
问题 How can I save the parameters for an MXNet model into a NumPy file (.npy)? After doing so, how can I load these parameters from the .npy file back into my model? Here is a minimal example to save the parameters for MXNet model using MXNet API. import mxnet as mx from mxnet import gluon from mxnet.gluon.model_zoo import vision import numpy as np num_gpus = 0 ctx = [mx.gpu(i) for i in range(num_gpus)] if num_gpus > 0 else [mx.cpu()] resnet = vision.resnet50_v2(pretrained=True, ctx=ctx)

Rolling windows for ndarrays

回眸只為那壹抹淺笑 提交于 2021-01-28 09:00:37
问题 I have a ndimensional array with shape (30,2,2) and 2000 elements. So my final array is of shape (2000, 30, 2, 2). I now want to stack rolling 200 elements in a new array. So I assume my final array will look something like (1801, 200, 30, 2, 2) where each element in 1800 has 200 samples of (30,2,2) arrays. How do you create this rolling window in python. I have tried using vstack but not entirely sure how I achieve my desired results. import numpy as np input = np.zeros((2000, 30, 2, 2))

how to fix ''Found input variables with inconsistent numbers of samples: [219, 247]''

时间秒杀一切 提交于 2021-01-28 08:32:13
问题 As title says when running the following code i get a trouble Found input variables with inconsistent numbers of samples: [219, 247], i have read that the problem should be on the np.array set for X and y, but i cannot address the problem because there is a price for every date so i dont get why it is happening, any help will be appreciated thanks! import pandas as pd import quandl, math, datetime import numpy as np from sklearn import preprocessing, svm, model_selection from sklearn.linear

how to evenly distribute values in array python

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-28 08:26:56
问题 Suppose I have df with 16 index and I want to evenly distribute number A-L 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 My desired output is: 1 A 2 A 3 B 4 B 5 C 6 C 7 D 8 D 9 E 10 F 11 G 12 H 13 I 14 J 15 K 16 L Is there any possible way to do this rname is second column , df2 is first column , my way was np.repeat(rname, np.ceil(len(df2) / len(rname)))[:len(df2)] 回答1: You can do: df['new_col'] = np.sort((np.arange(16) % 12) +1) 回答2: This may help you import numpy as np rname = ['A','B','C','D','E

numpy: concatenate two arrays along the 3rd dimension

我只是一个虾纸丫 提交于 2021-01-28 08:22:36
问题 I would like to add another "slice" of data to an existing data cube: import numpy as np a = np.array([[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]]) print(a.shape) # (2, 3, 4) b = np.array([[7, 7, 7], [8, 8, 8]]) print(b.shape) # (2, 3) c = np.concatenate([a, b], axis=2) # ValueError: all the input arrays must have same number of dimensions print(c.shape) # wanted result: (2, 3, 5) So I basically want to add the 2x3 array to the 2x3x4 array by

How can I use multiple dimensional polynomials with numpy.polynomial?

社会主义新天地 提交于 2021-01-28 08:20:18
问题 I'm able to use numpy.polynomial to fit terms to 1D polynomials like f(x) = 1 + x + x^2 . How can I fit multidimensional polynomials, like f(x,y) = 1 + x + x^2 + y + yx + y x^2 + y^2 + y^2 x + y^2 x^2 ? It looks like numpy doesn't support multidimensional polynomials at all: is that the case? In my real application, I have 5 dimensions of input and I am interested in hermite polynomials. It looks like the polynomials in scipy.special are also only available for one dimension of inputs. # One

Element wise cross product of vectors contained in 2 arrays with Python

荒凉一梦 提交于 2021-01-28 08:15:55
问题 I have two arrays, one containing a list of vectors ( A ) and one containing a 2D list of vectors ( B ). I am looking to do an element wise cross product of the vectors in each array in a specific way. The fist vector in A should be cross producted (?) by all 3 vectors contained in the first element of B . Here is a minimal example: import numpy as np A = np.random.rand(2,3) B = np.random.rand(2,3,3) C = np.random.rand(2,3,3) C[0,0] = np.cross(A[0],B[0,0]) C[0,1] = np.cross(A[0],B[0,1]) C[0,2

min function in numpy array

空扰寡人 提交于 2021-01-28 08:10:27
问题 I am trying to find a min value from one dimensional numpy array which which looks like: col = array(['6.7', '0.9', '1.3', '4', '1.8'],dtype='|S7'), using col.min() , which is not working. I tried as suggested on NumPy: get min/max from record array of numeric values view function, it failed to recognize 'S7' as valid field. What is the best way to deal with this problem? Should I have specified the data type while reading the values or while using the min function? 回答1: The problem is that

Accessing a large numpy array while preserving its order

烂漫一生 提交于 2021-01-28 08:00:47
问题 I would like to access an numpy array data via an index idx , but still preserving the order in data . Below is an example where the array is accessed with an order different from the one in the original array. In [125]: data = np.array([2, 2.2, 2.5]) In [126]: idx=np.array([1,0]) In [127]: data[idx] Out[127]: array([2.2, 2. ]) I hope to get [2,2.2] instead. Is there a highly efficient way to do so? In my problem setting, I have the data with more than a million floating-point numbers, and

Compute pairwise element of two 1D array

安稳与你 提交于 2021-01-28 07:59:52
问题 Here is my problem : let's say my two array are : import numpy as np first = np.array(["hello", "hello", "hellllo"]) second = np.array(["hlo", "halo", "alle"]) Now I want to get the matrix of distance between each element of the two arrays so for example my distance function is : def diff_len(string1, string2): return abs(len(string1) - len(string2)) So I I would like to get the matrix : hello hello hellllo hlo result1 result2 result3 halo result4 result5 result6 alle result7 result8 result9