Skip to content

Instantly share code, notes, and snippets.

@masaponto
Last active January 28, 2017 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masaponto/0f93520c3eee47476e839cc9487f2c98 to your computer and use it in GitHub Desktop.
Save masaponto/0f93520c3eee47476e839cc9487f2c98 to your computer and use it in GitHub Desktop.
Pythonのnumpy arrayをC++で読み込むサンプル (参考:https://gist.github.com/rezoo/5656056, http://mglab.blogspot.jp/2014/03/numpyhpp-npy.html)
#include <iostream>
#include "numpy.hpp"
int main(void) {
std::vector<int> s; // Vetor for matrix size
std::vector<int> int_data; // Vector for matrix element
aoba::LoadArrayFromNumpy("int_array.npy", s, int_data);
std::cout << s[0] << " " << s[1] << std::endl; // print matrix size (row * column)
std::cout << int_data.size() << std::endl; // print element size
for (int x : int_data) {
std::cout << x << " ";
}
std::cout << std::endl;
std::vector<float> float_data;
aoba::LoadArrayFromNumpy("float_array.npy", s, float_data);
std::cout << s[0] << " " << s[1] << std::endl;
std::cout << float_data.size() << std::endl;
for (float x : float_data) {
std::cout << x << " ";
}
}
#!/usr/bin/env python
import numpy as np
def main():
X = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.intc) # int array
np.save('./int_array', X)
X = np.array([[1.5, 2.2, 3.3], [4.5, 5.2, 6.8]], dtype=np.float32) # float arary
np.save('./float_array', X)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment