簡體   English   中英

python中的N維數組

[英]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 算法通常在這些idxvalue數組(通常稱為XY )上工作,因為否則實際數組將是巨大的。

有時您需要一個維度的“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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM