[英]N dimensional array in python
Python 和 Numpy 的新功能,尝试创建 263 维数组。 我需要很多机器学习模型的维度。 当然,一种方法是使用 numpy.zeros 或 numpy.ones 并编写如下代码:
x=np.zeros((1,1,1,1,1,1,1,1,1,1,1)) #and more 1,1,1,1
有没有更简单的方法来创建多维数组?
你不需要 263-dimensions 。 如果每个维度只有大小 2,你仍然有2 ** 263
元素,它们是: 148213874223764730142170860811120522052185580372019921970505707530983981
您将无法使用这样的矩阵做任何事情:甚至无法在 Google 服务器上进行初始化。
您要么需要一个包含 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.])
或具有 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.]])
有许多先进的研究中心对香草 Numpy 非常满意。 对于量子力学或机器学习来说,必须使用少于 32 维似乎并没有太大的困扰。
让我们从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.
...
shape 参数只是每个维度大小的列表(但您可能知道)。 有很多方法可以在 python 中轻松创建这样的列表,一种快速的方法是
np.zeros(np.ones(263, dtype=int))
但是,正如其他人所提到的, numpy
有 32 维的任意限制。 根据我的经验,通过保留一个显示每行属于哪个“维度”的索引数组,您可以获得类似且更灵活的行为。
很可能,对于 ML 应用程序,您实际上并不想要这样:
shape = np.random.randint(1,10,(263,))
arr = np.zeros(shape) # causes a ValueError anyway
你实际上想要一些稀疏的东西
for i, value in enumerate(nonzero_values):
arr[idx[i]] = value
在这种情况下, idx
是一个(num_samples, 263)
数组, nonzero_values
是一个(num_samples,)
数组。
ML 算法通常在这些idx
和value
数组(通常称为X
和Y
)上工作,因为否则实际数组将是巨大的。
有时您需要一个维度的“one-hot”数组,这将使idx.shape == (num_samples, shape.sum())
,其中idx
仅包含 0 或 1 个值。 但这仍然比任何类型的高维数组都要小。
有一个名为 DimPy 的新包,它可以很容易地在 python 中创建多维数组。 安装使用
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)
默认情况下,每个元素都为零。 要更改它,请使用dfv(a, 'New value')
要将其表示为 numpy 样式数组,请使用a=npary(a)
在此处查看更多详细信息: https : a=npary(a)
dimpy.html?m=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.