-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data loading and saving in different sparse and dense format.
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import numpy as np | ||
|
||
from scipy.sparse import load_npz, save_npz | ||
|
||
|
||
def load_dense(features_path, transform=True): | ||
features = np.load(features_path) | ||
if transform: | ||
features = features.astype(np.float32) | ||
return features | ||
|
||
|
||
def load_sparse(features_path, transform=True): | ||
features = load_npz(features_path) | ||
if transform: | ||
features = np.asarray(features.todense()).astype(np.float32) | ||
return features | ||
|
||
|
||
def save_dense(features_path, features): | ||
np.save(features_path, features) | ||
|
||
|
||
def save_sparse(features_path, features): | ||
save_npz(features_path, features) | ||
|
||
|
||
loaders = { | ||
"dense": load_dense, | ||
"sparse": load_sparse, | ||
} | ||
|
||
savers = { | ||
"dense": save_dense, | ||
"sparse": save_sparse, | ||
} | ||
|
||
data_formats = loaders.keys() |