English

Can you create a datafile and import data into multiple NumPy arrays column wise? (Hint: use the unpack parameter)

Advertisements
Advertisements

Question

Can you create a datafile and import data into multiple NumPy arrays column wise? (Hint: use the unpack parameter)

Code Writing
Advertisements

Solution

Yes, we can create a data file and import its data into multiple NumPy arrays column-wise by using the unpack=True parameter of np.loadtxt().

Suppose the file data.txt contains:

10 20 30
40 50 60
70 80 90

The NumPy command is:

import numpy as np
a, b, c = np.loadtxt('data.txt', unpack=True)
print(a)
print(b)
print(c)

Output:

[10. 20. 30.]
[40. 50. 60.]
[70. 80. 90.]

Here, unpack=True transposes the data, so each column is stored in a separate NumPy array (a, b, and c).

shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Introduction to NumPy - Intext Questions [Page 111]

APPEARS IN

NCERT Informatics Practices [English] Class 11
Chapter 6 Introduction to NumPy
Intext Questions | Q 3. | Page 111
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×