0

i have 1D array with shape is 777599. i want to make matrix diagonal of my data 1D array be 2D array matrix diagonal. but i have a problem. this is my code:

import numpy as np
a = np.linspace(0, 2000, 777599)
b = np.diag(a)
print(b.shape)

and the response is:

Traceback (most recent call last):
  File "/home/willi/PycharmProjects/006_TA/017_gravkorCG5.py", line 29, in <module>
    b = np.diag(a)
  File "<__array_function__ internals>", line 6, in diag
  File "/home/willi/PycharmProjects/venv/lib/python3.5/site-packages/numpy/lib/twodim_base.py", line 275, in diag
    res = zeros((n, n), v.dtype)
MemoryError: Unable to allocate 4.40 TiB for an array with shape (777599, 777599) and data type float64
  • Try this post : https://stackoverflow.com/questions/57507832/unable-to-allocate-array-with-shape-and-data-type – Nadhir Mar 23 '20 at 10:54
  • A square array with those dimensions is too large for your memory. Why do you need this array? Most `numpy` operations on an array involve making one or more copies of the same size - either temporarily or as a result. – hpaulj Mar 23 '20 at 17:01

1 Answers1

1

An array with 777599x777599 (i.e. 604660204801) elements is huge. Sparse matrices to the rescue (requires pip install scipy):

import numpy as np
from scipy import sparse
a = np.linspace(0, 2000, 777599)
b = sparse.csc_matrix((a, (range(a.shape[0]), range(a.shape[0]))))

It will be slower than dense matrix... if a dense matrix could fit into memory. :)

Amadan
  • 191,408
  • 23
  • 240
  • 301