forked from ASPP/ASPP-2018-numpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input-output.py
34 lines (26 loc) · 940 Bytes
/
input-output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -----------------------------------------------------------------------------
# Copyright (C) 2018 Nicolas P. Rougier
# Distributed under the terms of the BSD License.
# -----------------------------------------------------------------------------
import numpy as np
# Create our own dtype
dtype = np.dtype([('rank', 'i8'),
('lemma', 'S8'),
('frequency', 'i8'),
('dispersion', 'f8')])
# Load file using our own dtype
data = np.loadtxt('data.txt', comments='%', dtype=dtype)
# Extract words only
print(data["lemma"])
# Extract the 3rd row
print(data[2])
# Print all words with rank < 30
print(data[data["rank"] < 30])
# Sort the data according to frequency
sorted = np.sort(data, order="frequency")
print(sorted)
# Save unsorted and sorted array
np.savez("sorted.npz", data=data, sorted=sorted)
# Load saved array
out = np.load("sorted.npz")
print(out["sorted"])