Difference Between numpy.genfromtxt and numpy.loadtxt, and Unpack Difference Between numpy.genfromtxt and numpy.loadtxt, and Unpack numpy numpy

Difference Between numpy.genfromtxt and numpy.loadtxt, and Unpack


You are correct. Using np.genfromtxt gives you some options like the parameters missing_values, filling_values that can help you dealing with an incomplete csv. Example:

1,2,,,56,,8,,11,,,,

Could be read with:

filling_values = (111, 222, 333, 444, 555) # one for each columnnp.genfromtxt(filename, delimiter=',', filling_values=filling_values) #array([[   1.,    2.,  333.,  444.,    5.],#       [   6.,  222.,    8.,  444.,  555.],#       [  11.,  222.,  333.,  444.,  555.]])

The parameter unpack is useful when you want to put each column of the text file in a different variable. Example, you have the text file with columns x, y, z, then:

x, y, z = np.loadtxt(filename, unpack=True)

Note that this works the same as

x, y, z = np.loadtxt(filename).T

By default iterating over a 2-D array means iterating over the lines, that's why you have to transpose or use unpack=True in this example.