Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

Load a sparse data array in python

I have multiple files composed of sampled data, each file contains x,y coordinates and the value of the variable. How can I possibly load this data as a sparse array (or matrix) where each sample is located at its position in the array and there are empty cells in between?

e.g., enter image description here

question from:https://stackoverflow.com/questions/65849543/load-a-sparse-data-array-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The inputs are a little unclear ( I think you've mixed up 0 based indexing a little), but you can probably coerce them into this form and get what you're after.

import numpy as np
from scipy.sparse import coo_matrix

row  = np.array([0, 1, 2, 1])
col  = np.array([0, 1, 1, 3])
data = np.array([1, 2, 0.5, 3])
coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
array([[1. , 0. , 0. , 0. ],
       [0. , 2. , 0. , 3. ],
       [0. , 0.5, 0. , 0. ],
       [0. , 0. , 0. , 0. ]])

See https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.coo_matrix.html#scipy.sparse.coo_matrix for more examples


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...