Advertisements
Advertisements
प्रश्न
Can you create a datafile and import data into multiple NumPy arrays column wise? (Hint: use the unpack parameter)
कोड लेखन
Advertisements
उत्तर
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
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
