Repository navigation
Expand file tree
/
Copy pathprepare_data.py
More file actions
200 lines (153 loc) · 6 KB
/
Copy pathprepare_data.py
File metadata and controls
200 lines (153 loc) · 6 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
import zipfile
from pymapd import connect
import pygeohash as pgh
import numpy as np
import pandas as pd
import geopandas
import glob
import swifter
import time
from sklearn.datasets import make_regression
from sklearn.datasets import make_blobs
from sklearn.datasets import make_low_rank_matrix
import random
from shapely.geometry import Point
def generateGeohashesNew(row):
geohash = pgh.encode(row['Latitude'], row['Longitude'], precision=8)
return pd.Series(
[
geohash[0:1],
geohash[0:2],
geohash[0:3],
geohash[0:4],
geohash[0:5],
geohash[0:6],
geohash[0:7],
geohash
])
def generateGeohashes(df):
g1s, g2s, g3s, g4s, g5s, g6s, g7s, g8s = [], [], [], [], [], [], [], []
for index, row in df.iterrows():
geohash = pgh.encode(row['Latitude'], row['Longitude'], precision=8)
g1s.append(geohash[0:1])
g2s.append(geohash[0:2])
g3s.append(geohash[0:3])
g4s.append(geohash[0:4])
g5s.append(geohash[0:5])
g6s.append(geohash[0:6])
g7s.append(geohash[0:7])
g8s.append(geohash[0:8])
if index % 100000 == 0:
print(f"generateGeohashes {index}")
df_result = pd.DataFrame({'geohash_1': g1s,
'geohash_2': g2s,
'geohash_3': g3s,
'geohash_4': g4s,
'geohash_5': g5s,
'geohash_6': g6s,
'geohash_7': g7s,
'geohash_8': g8s,
})
return df_result
def read_csv(file):
df = pd.read_csv(file, sep=';',
encoding="utf-8",
usecols=['ID',
'CountryCode',
"Latitude",
"Longitude",
"Income Group", "TSI Group", "Sum Insured", "Has Losses", "Losses", "Building Type", "Sample Rating"],
dtype={
'ID': np.int,
'Latitude': np.float,
'Longitude': np.float,
'Income Group': np.int32,
'Sum Insured': np.float,
'Has Losses': np.bool,
'Losses': np.float,
'CountryCode': np.str
}
)
df.rename(columns={
'Income Group': 'Income_Group',
'TSI Group': 'TSI_Group',
'Sum Insured': 'Sum_Insured',
'Has Losses': 'Has_Losses',
'Building Type': 'Building_Type',
'Sample Rating': 'Sample_Rating'
}, inplace=True)
return df
def gen_array(size, parts):
result = np.array([], dtype=np.int16)
for index, p in enumerate(parts):
count = int(size / 100 * p)
a = np.full(count, index + 1, dtype=np.int16)
result = np.append(result, a)
# add 10 procent
count = int(size / 100 * 10)
a = np.full(count, 1, dtype=np.int16)
result = np.append(result, a)
result = result[:size]
# np.random.shuffle(result)
print(result)
print(len(result))
print(type(result))
return result
for filename in glob.glob('data/portfolios/*100k*.csv'):
print(f'read file {filename}')
df = read_csv(filename)
print(f'dataset has: {df.size}')
# print(df.head())
# print(df.dtypes)
portfolioName = filename.split('/')[-1]
df.insert(0, 'PortfolioName', portfolioName)
df.insert(1, 'LayerId', np.random.randint(1, 21, size=len(df.index)))
# df.insert(2, 'parent_layer_id', np.random.randint(1, 21, size=len(df.index)))
# df.loc[df['LayerId'] == 1, 'parent_layer_id'] = -1
# r = Point(150, 60)
# print(r)
# coors: pd.Series = df.swifter.apply(lambda row: Point(row['Longitude'], row['Latitude']), axis=1)
# test = coors.to_list()
# s = geopandas.GeoSeries(test)
# print(s)
# #import shapely
# print(coors.head())
# df.insert(6, 'coor', s)
print('calculate geohash')
start = time.time()
df[[
'geohash_1',
'geohash_2',
'geohash_3',
'geohash_4',
'geohash_5',
'geohash_6',
'geohash_7',
'geohash_8'
]] = df.swifter.apply(lambda row: generateGeohashesNew(row), axis=1)
print(f"Duration: {time.time() - start}")
print(df.dtypes)
print(df.head())
# df['geohash_8'] = df.swifter.apply(lambda row: pgh.encode(row['Latitude'], row['Longitude'], precision=8), axis=1)
# df['geohash_2'] = df[['geohash_8']].apply(lambda l8: l8[0:2], axis=1)
# df['geohash_1'] = df.swifter.apply(lambda row: row['geohash_8'][0:1], axis=1)
# df['geohash_2'] = df.swifter.apply(lambda row: row['geohash_8'][0:2], axis=1)
# df['geohash_3'] = df.swifter.apply(lambda row: row['geohash_8'][0:3], axis=1)
# df['geohash_4'] = df.swifter.apply(lambda row: row['geohash_8'][0:4], axis=1)
# df['geohash_5'] = df.swifter.apply(lambda row: row['geohash_8'][0:5], axis=1)
# df['geohash_6'] = df.swifter.apply(lambda row: row['geohash_8'][0:6], axis=1)
# df['geohash_7'] = df.swifter.apply(lambda row: row['geohash_8'][0:7], axis=1)
# df['earthquake'] = np.random.randint(
# 1, 5, size=len(df.index), dtype=np.int16)
df['earthquake'] = gen_array(len(df.index), [25, 50, 5, 20])
#df['hail'] = np.random.randint(1, 7, size=len(df.index), dtype=np.int16)
df['hail'] = gen_array(len(df.index), [35, 15, 8, 2, 10, 30])
# df['heat_wave'] = np.random.randint(
# 1, 4, size=len(df.index), dtype=np.int16)
df['heat_wave'] = gen_array(len(df.index), [60, 30, 10])
#df['tornado'] = np.random.randint(1, 6, size=len(df.index), dtype=np.int16)
df['tornado'] = gen_array(len(df.index), [10, 30, 40, 5, 15])
print('save data to file')
df.to_feather(f"data/portfolios/{portfolioName}_geohashes.feather")
#con.load_table("portfolios2", df)