Let's say I have a file with the following values:
1 2 3 4 11 12 13 14
and I want to read them as an numx 2x2x2 array. The standard np.loadtxt('testfile') reads them as a set of vectors ignoring spaces (4x1x8). I suppose I could iterate over them and stack them correctly, but my actual data files are quite large and would rather not have too many loops if possible. Is there a good way to do this on a numpy system?
np.loadtxt('testfile')
Thank you for your help!
Use reshape .
>>> import numpy >>> a = numpy.loadtxt('testfile') >>> a array([[ 1., 2.], [ 3., 4.], [ 11., 12.], [ 13., 14.]]) >>> a.reshape((2, 2, 2)) array([[[ 1., 2.], [ 3., 4.]], [[ 11., 12.], [ 13., 14.]]])
Source: https://habr.com/ru/post/1491148/More articles:Enumeration failed? - linqHow to create an image based on text and CSS? - javascriptButton and touch freeze effect (WPF) - windows-8How to add text to canvas - textRACSignal: how to reduce on an arbitrarily large combine - objective-cProgram for debugging memory leaks in armv4 architecture - cGrammar of expressions using the exponentiation operator using Boost Spirit - c ++Automatically expire and delete znodes in Zookeeper? - apache-zookeeperHow does the IFTTT application perform background synchronization? - iphoneProblem with command line arguments that got spaces in it - javaAll Articles