[Numpy-discussion] How to import input data to make ndarray for batch processing?

Dave Hirschfeld dave.hirschfeld@gmail....
Thu Nov 18 09:40:57 CST 2010


Venkat <dvr002 <at> gmail.com> writes:

> 
> Hi All,I am new to Numpy (also Scipy).I am trying to reshape my text data
which is in one single column (10,000 rows).I want the data to be in 100x100
array form.I have many files to convert like this. All of them are having file
names like 0, 1, 2, ....500. with out any extension.
> Actually, I renamed actual files so that I can import them in Matlab for batch
processing.Since Matlab also new for me, I thought I will try Numpy first. Can
any body help me in writing the script to do this for making batch processing.
Thanks in advance,Venkat

In [2]: dummy_data = np.random.randn(100,100)

In [3]: dummy_data.shape
Out[3]: (100, 100)

In [4]: dummy_data.flatten().shape
Out[4]: (10000,)

In [5]: np.savetxt('dummy_data.txt', dummy_data.flatten())

In [6]: !less dummy_data.txt
2.571031186906808100e-01
1.566790681796508500e+00
-6.846267829937818800e-01
3.271332705287631200e-01
-7.482409829656505600e-02
1.429095432454441600e-01
-6.888841694801869400e-01
-5.319842186383831900e-01
-4.047786844569442600e-01
-6.696045994533519300e-01
-4.895085917712171400e-01
6.969419814656118200e-01
6.656815445278644300e-01
7.455135053686292600e-01
...

In [7]: data = np.loadtxt('dummy_data.txt')

In [8]: data.shape
Out[8]: (10000,)

In [9]: data = data.reshape(100, 100)

In [10]: data.shape
Out[10]: (100, 100)

In [11]: np.allclose(dummy_data, data)
Out[11]: True

HTH,
Dave




More information about the NumPy-Discussion mailing list