-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocessing.py
More file actions
68 lines (52 loc) · 1.95 KB
/
Copy pathpreprocessing.py
File metadata and controls
68 lines (52 loc) · 1.95 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
import pandas as pd
import numpy as np
from sklearn.neighbors import BallTree
COL_LAT = 'latitude'
COL_LON = 'longitude'
NAME = 'dummy_name'
ST_PLACE = 'Starting'
END_PLACE = 'Destination'
MR = 'Main'
SOC = 'Social'
REF = 'Referred'
def calculate_score(main_rating, social_ratings, referral_rating):
MAIN_WEIGHT = 0.5
REFERRAL_WEIGHT = 0.3
INTRO_WEIGHT = 0.2
main_score = (main_rating / 5) if main_rating is not None else 0
social_avg = (sum(social_ratings)/len(social_ratings)/5) if social_ratings else 0
ref_score = referral_rating / 5
return (main_score * MAIN_WEIGHT) + (social_avg * REFERRAL_WEIGHT) + (ref_score * INTRO_WEIGHT)
def preprocess_proximal_points(csv_path):
df = pd.read_csv(csv_path)
df['latitude_rad'] = np.radians(df[COL_LAT])
df['longitude_rad'] = np.radians(df[COL_LON])
tree = BallTree(
df[['latitude_rad', 'longitude_rad']].values,
metric='haversine'
)
distances, indices = tree.query(
df[['latitude_rad', 'longitude_rad']].values,
k=len(df)
)
earth_radius_miles = 3958.8
df['proximal_rank'] = [{df.iloc[i][NAME] : d*earth_radius_miles
for i, d in zip(row_indices, row_dists) }
for idx, (row_indices, row_dists) in enumerate(zip(indices, distances))
]
df.drop(columns=['latitude_rad', 'longitude_rad', COL_LAT, COL_LON], inplace=True)
df.set_index(NAME, inplace=True)
return df
def preprocess_users(csv_path):
df = pd.read_csv(csv_path)
scores = []
for idx,row in df.iterrows():
main_rating = row[MR]
social_ratings = row[SOC].split(',') if pd.notna(row[SOC]) else []
referral_rating = row[REF]
social_ratings = [float(rating) for rating in social_ratings]
score = calculate_score(main_rating, social_ratings, referral_rating)
scores.append(score)
df['Score'] = scores
df.drop(columns=[MR, SOC, REF], inplace=True)
return df