-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumpy_suite.py
83 lines (63 loc) · 2.81 KB
/
numpy_suite.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
def country_consumption(world, country="", year="1989"):
return world[:, 4][(world[:, 2] == country) & (world[:, 0] == year)].astype(float).sum()
def get_countries_consumption(data, year="1989"):
countries = np.unique(data[:, 2])
return {country: country_consumption(data, country) for country in countries}
world_alcohol = np.genfromtxt(
"donnees/world_alcohol.csv", delimiter=",", skip_header=True, dtype=str
)
print(world_alcohol)
countries_is_france = world_alcohol[:, 2] == "France"
print(countries_is_france)
countries_france = world_alcohol[countries_is_france]
print(countries_france)
print(countries_france.shape)
countries_is_algeria = world_alcohol[:, 2] == "Algeria"
countries_algeria = world_alcohol[countries_is_algeria]
print(countries_algeria)
print(countries_algeria.shape)
countries_is_1984 = world_alcohol[:, 0] == "1984"
countries_1984 = world_alcohol[countries_is_1984]
print(countries_1984)
print(countries_1984.shape)
is_algeria_1986 = countries_is_algeria & (world_alcohol[:, 0] == "1986")
countries_is_algeria_1986 = world_alcohol[is_algeria_1986]
print(countries_is_algeria_1986)
countries_is_canada = world_alcohol[:, 2] == "Canada"
is_algeria_or_canada = countries_is_algeria | countries_is_canada
print(world_alcohol[is_algeria_or_canada])
print(world_alcohol[is_algeria_or_canada].shape)
world_alcohol_copy = world_alcohol.copy()
#world_alcohol_copy[:, 0][world_alcohol_copy[:, 0] == "1986"] = "2018"
#print(world_alcohol_copy[world_alcohol_copy[:, 0] == "2018"])
is_value_empty = world_alcohol_copy[:, 4] == ""
world_alcohol_copy[:, 4][is_value_empty] = "0"
print(world_alcohol[is_value_empty][0])
print(world_alcohol_copy[is_value_empty][0])
alcohol_consumption = world_alcohol_copy[:, 4].astype(float)
print(alcohol_consumption)
print(alcohol_consumption.sum())
print(alcohol_consumption.mean())
is_1987 = world_alcohol_copy[:, 0] == "1987"
is_canada_1987 = countries_is_canada & is_1987
canada_1987_consumption = (
world_alcohol_copy[:, 4][is_canada_1987].astype(float)
)
print(canada_1987_consumption)
print(canada_1987_consumption.sum())
print(canada_1987_consumption.mean())
is_beer = world_alcohol_copy[:, 3] == "Beer"
countries_is_america = world_alcohol_copy[:, 1] == "Americas"
is_america_beer = countries_is_america & is_beer
america_beer_consumption = (
world_alcohol_copy[:, 4][is_america_beer].astype(float)
)
print(america_beer_consumption)
print(america_beer_consumption.sum())
print(america_beer_consumption.mean())
print(world_alcohol_copy[countries_is_canada & is_beer])
print(get_countries_consumption(world_alcohol_copy))
countries_consumption = np.array(list(get_countries_consumption(world_alcohol_copy).items()))
maximum = countries_consumption[:, 1].astype(float).max()
print(countries_consumption[countries_consumption[:, 1] == str(maximum)])