Comments (8)
It compiles but the indexing is wrong. Should be: vec2d[row][col] = loaded_data[row*ncols+col];
.
from cnpy.
We've integrated cnpy into xtensor
& xtensor-io
by the way, if you want to use a "NumPy-like" container directly in C++ without needing to resort to inefficient vector-of-vector constructs.
- https://github.com/QuantStack/xtensor (NPY loading)
- https://github.com/QuantStack/xtensor-io (NPZ loading)
from cnpy.
It wouldn't be as efficient as indexing a single vector yourself, but if you truly needed the data in a vector of vectors, you could do the following:
`
cnpy::NpyArray arr = cnpy::npy_load("arr1.npy");
double* loaded_data = arr.data();
size_t nrows = arr.shape[0];
size_t ncols = arr.shape[1];
std::vector<std::vector> vec2d;
vec2d.reserve(nrows);
for(size_t row = 0; row < nrows;row++) {
vec2d.emplace_back(ncols);
for(size_t col = 0;col < ncols;col++) {
vec2d[row][col] = loaded_data[row*nrows+col];
}
}
`
from cnpy.
Just for the sake of visibility..
cnpy::NpyArray arr = cnpy::npy_load("arr1.npy");
double* loaded_data = arr.data();
size_t nrows = arr.shape[0];
size_t ncols = arr.shape[1];
std::vector<std::vector> vec2d;
vec2d.reserve(nrows);
for(size_t row = 0; row < nrows;row++) {
vec2d.emplace_back(ncols);
for(size_t col = 0;col < ncols;col++) {
vec2d[row][col] = loaded_data[row*nrows+col];
}
}
from cnpy.
Some code edits that I felt were necessary while compiling the code...
cnpy::NpyArray arr = cnpy::npy_load("arr1.npy");
double* loaded_data = arr.data<double>();
size_t nrows = arr.shape[0];
size_t ncols = arr.shape[1];
std::vector<std::vector<double> > vec2d;
vec2d.reserve(nrows);
for(size_t row = 0; row < nrows;row++) {
vec2d.emplace_back(ncols);
for(size_t col = 0;col < ncols;col++) {
vec2d[row][col] = loaded_data[row*nrows+col];
}
}
from cnpy.
It compiles but the indexing is wrong. Should be:
vec2d[row][col] = loaded_data[row*ncols+col];
.
It depends on how you are counting. If the matrix is row-major then what you have specified is correct. However, the code I wrote is for the matrix that has been stored in column-major format.
from cnpy.
Sorry, I need to disagree. Two things:
- The matrices that are stored with cnpy are always row-major. If you want to store matrices in column-major format, this should be reflected in the header information of the npy file (currently, cnpy writes a constant
fortran_order: False
and duringnpy_load
it checksassert(!fortran_order)
, i.e. all matrices are row-major). If you ignore this, you will get different results when loading the same matrix with numpy and cnpy. - Even if you want to read the matrix in column-major format your indexing is wrong. It should be
vec2d[row][col] = loaded_data[col*nrows+row]
. Your proposed indexing coincidentally works for square matrices but not for arbitrary ones.
Please correct me if I'm missing something.
from cnpy.
I was going to ask if cnpy could automatically load data type/shapes. But seems like one should use xtensor instead? @wolfv, Should this be in readme file?
from cnpy.
Related Issues (20)
- Remove zlib dependency, MSVC compatbilty HOT 3
- No makefile in the "make" step HOT 1
- Undefined symbols for architecture x86_64 HOT 2
- Unable to read large npy
- Cannot read npz file
- cannot create std::vector larger than max_size()
- Would cnpy work with a vector of structs? HOT 3
- Not sure it's a issue or not. How could I parse std::string by using CNPY? HOT 2
- Undefined Symbol Error When Making
- Why is the data type not stored in NpyArray? HOT 1
- Why forcing little endian?
- cnpy does not support large numpy headers
- not support dict?
- Does it support uint32 type saving?
- "undefined symbol: inflate" for cnpy-static
- Feature request: support writing to memory streams
- Undefined reference to `cnpy::npy_load(std::string)' HOT 3
- Error with AddressSanitizer (container-overflow)
- How to save a opencv cv::Mat into .npy file
- npy file size limit HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cnpy.