简体   繁体   English

python中的N维数组

[英]N dimensional array in python

New at Python and Numpy, trying to create 263-dimensional arrays. Python 和 Numpy 的新功能,尝试创建 263 维数组。 I need so much dimensions for Machine Learning model.我需要很多机器学习模型的维度。 Of course one way is using numpy.zeros or numpy.ones and writing code as below :当然,一种方法是使用 numpy.zeros 或 numpy.ones 并编写如下代码:

x=np.zeros((1,1,1,1,1,1,1,1,1,1,1))   #and more 1,1,1,1

Is there an easier way to create arrays with many dimensions?有没有更简单的方法来创建多维数组?

You don't need 263-dimensions .你不需要 263-dimensions If every dimension had only size 2, you'd still have 2 ** 263 elements, which are: 14821387422376473014217086081112052205218558037201992197050570753012880593911808如果每个维度只有大小 2,你仍然有2 ** 263元素,它们是: 148213874223764730142170860811120522052185580372019921970505707530983981

You wouldn't be able to do anything with such a matrix : not even initializing on Google servers.您将无法使用这样的矩阵做任何事情:甚至无法在 Google 服务器上进行初始化。

You either need an array with 263 values :您要么需要一个包含 263 个值的数组:

>>> np.zeros(263)
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.])

or a matrix with 263 vectors of M elements (let's say 3):或具有 M 个元素的 263 个向量的矩阵(假设为 3 个):

>>> np.zeros((263, 3))
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       ...
       ...
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

There are many advanced research centers that are perfectly happy with vanilla Numpy.有许多先进的研究中心对香草 Numpy 非常满意。 Having to use less than 32 dimensions doesn't seem to bother them much for quantum mechanics or machine learning.对于量子力学或机器学习来说,必须使用少于 32 维似乎并没有太大的困扰。

Let's start with the numpy documentation, help(np.zeros) gives让我们从numpy文档开始, help(np.zeros)给出

zeros(shape, dtype=float, order='C')

Return a new array of given shape and type, filled with zeros.

Parameters
----------
shape : int or sequence of ints
    Shape of the new array, e.g., ``(2, 3)`` or ``2``.
...
Returns
-------
out : ndarray
    Array of zeros with the given shape, dtype, and order.
...

The shape argument is just a list of the size of each dimension (but you probably knew that). shape 参数只是每个维度大小的列表(但您可能知道)。 There are lots of ways to easily create such a list in python, one quick way is有很多方法可以在 python 中轻松创建这样的列表,一种快速的方法是

 np.zeros(np.ones(263, dtype=int))

But, as others have mentioned, numpy has a somewhat arbitrary limitation of 32 dimensions.但是,正如其他人所提到的, numpy有 32 维的任意限制。 In my experience, you can get similar and more flexible behavior by keeping an index array showing which "dimension" each row belongs to.根据我的经验,通过保留一个显示每行属于哪个“维度”的索引数组,您可以获得类似且更灵活的行为。

Most likely, for ML applications you don't actually want this:很可能,对于 ML 应用程序,您实际上并不想要这样:

shape = np.random.randint(1,10,(263,))
arr = np.zeros(shape)  # causes a ValueError anyway

You actually want something sparse你实际上想要一些稀疏的东西

for i, value in enumerate(nonzero_values):
    arr[idx[i]] = value

idx in this case is a (num_samples, 263) array and nonzero_values is a (num_samples,) array.在这种情况下, idx是一个(num_samples, 263)数组, nonzero_values是一个(num_samples,)数组。

ML algorithms usually work on these idx and value arrays (usually called X and Y ) since the actual arrays would be enormous otherwise. ML 算法通常在这些idxvalue数组(通常称为XY )上工作,因为否则实际数组将是巨大的。

Sometimes you need a "one-hot" array of your dimensions, which will make idx.shape == (num_samples, shape.sum()) , with idx containting only 0 or 1 values.有时您需要一个维度的“one-hot”数组,这将使idx.shape == (num_samples, shape.sum()) ,其中idx仅包含 0 或 1 个值。 But that's still smaller than any sort of high-dimetnsional array.但这仍然比任何类型的高维数组都要小。

There is a new package called DimPy which can create multi-dimensional arrays in python very easily.有一个名为 DimPy 的新包,它可以很容易地在 python 中创建多维数组。 To install use安装使用
pip install dimpy Use example pip install dimpy使用示例

from dimpy import *
a=dim(4,5,6) # This is a 3 dimensional array of 4x5x6 elements. Use any number of dimensions within '( ) ' separated by comma
print(a)

By default every element will be zero.默认情况下,每个元素都为零。 To change it use dfv(a, 'New value') To express it into numpy style array, use a=npary(a) See in more details here: https://www.respt.in/p/python-package-dimpy.html?m=1要更改它,请使用dfv(a, 'New value')要将其表示为 numpy 样式数组,请使用a=npary(a)在此处查看更多详细信息: https : a=npary(a) dimpy.html?m=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM