Giter Site home page Giter Site logo

Loading 2D npz double arrays about cnpy HOT 8 OPEN

gospodnetic avatar gospodnetic commented on August 16, 2024
Loading 2D npz double arrays

from cnpy.

Comments (8)

clemense avatar clemense commented on August 16, 2024 1

It compiles but the indexing is wrong. Should be: vec2d[row][col] = loaded_data[row*ncols+col];.

from cnpy.

wolfv avatar wolfv commented on August 16, 2024 1

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.

from cnpy.

rogersce avatar rogersce commented on August 16, 2024

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.

chraibi avatar chraibi commented on August 16, 2024

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.

ranka47 avatar ranka47 commented on August 16, 2024

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.

ranka47 avatar ranka47 commented on August 16, 2024

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.

clemense avatar clemense commented on August 16, 2024

Sorry, I need to disagree. Two things:

  1. 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 during npy_load it checks assert(!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.
  2. 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.

vmiheer avatar vmiheer commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.