-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalary-yearly.py
129 lines (102 loc) · 4.73 KB
/
salary-yearly.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
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
import pandas as pd
import pycountry_convert as pc
import requests
import datetime
import hopsworks
def fetch_data():
year = datetime.date.today().year - 1
data = pd.read_csv("https://ai-jobs.net/salaries/download/salaries.csv")
data = data[data["work_year"] == year]
return data
def prepare_data(dataset: pd.DataFrame):
## We need to apply the same transformations used during the EDA
quantile = dataset['salary_in_usd'].quantile(0.99)
dataset = dataset[dataset['salary_in_usd'] < quantile]
dataset['salary'] = dataset['salary_in_usd']
dataset.drop(columns=['salary_in_usd','employee_residence', 'remote_ratio'], inplace=True)
salary_currency_list = [
'USD',
'EUR',
'GBP'
]
dataset['salary_currency'] = dataset['salary_currency'].map(lambda x: x if x in salary_currency_list else 'other_currency')
dataset = pd.get_dummies(dataset, columns=['salary_currency'],prefix='', prefix_sep='', sparse=False).drop(columns=['other_currency'])
experience_level_map = {
'EN': 0,
'MI': 1,
'SE': 2,
'EX': 3
}
dataset['experience_level'] = dataset['experience_level'].map(experience_level_map)
jobs = dataset['job_title'].value_counts()
jobs = jobs[jobs.cumsum() < len(dataset) * 0.9]
jobs = jobs.index.tolist()
dataset = dataset[dataset['job_title'].isin(jobs)]
dataset = dataset[dataset['employment_type'] == 'FT'].drop(columns=['employment_type'])
dataset['Engineer'] = dataset['job_title'].apply(lambda x: 1 if 'engineer' in x.lower() else 0)
dataset['Scientist'] = dataset['job_title'].apply(lambda x: 1 if 'data scientist' in x.lower() else 0)
dataset['Research'] = dataset['job_title'].apply(lambda x: 1 if 'research' in x.lower() else 0)
dataset['Analyst'] = dataset['job_title'].apply(lambda x: 1 if 'analyst' in x.lower() else 0)
dataset = pd.get_dummies(dataset, columns=['job_title'],prefix='', prefix_sep='', sparse=False)
company_size_map = {
'S': 0,
'M': 1,
'L': 2,
}
dataset['company_size'] = dataset['company_size'].map(company_size_map)
dataset['company_continent'] = dataset['company_location'].apply(lambda x: pc.country_alpha2_to_continent_code(x))
dataset = dataset[dataset['company_continent'].isin(['EU', 'NA'])]
country_CPI = dataset[['work_year','company_location']].drop_duplicates()
country_CPI['CPI'] = country_CPI.apply(lambda x: get_gdp_by_country_code(x['company_location'], x['work_year']), axis=1)
country_GDP = dataset[['work_year','company_location']].drop_duplicates()
country_GDP['GDP'] = country_CPI.apply(lambda x: get_gdp_by_country_code(x['company_location'], x['work_year'], index='NY.GDP.MKTP.CD'), axis=1)
dataset_final = dataset.merge(country_GDP, on=['company_location','work_year'], how='left')
dataset_final = dataset_final.merge(country_CPI, on=['company_location','work_year'], how='left')
dataset_final = dataset_final.drop(columns=['company_location', 'company_continent'])
salary_bins = pd.qcut(dataset_final['salary'], q=3, labels=False)
dataset_final['salary_bins'] = salary_bins
return dataset_final
def upload_data(df: pd.DataFrame):
project = hopsworks.login()
fs = project.get_feature_store()
df.columns = df.columns.str.lower()
df.columns = df.columns.str.replace(' ', '_')
primary_key = df.columns[df.columns != 'salary_bins'].values
salary_fg = fs.get_or_create_feature_group(
name="salary",
version=1,
primary_key=primary_key,
description="salary dataset")
salary_fg.insert(df)
return
def get_gdp_by_country_code(country_code, year=2023, index='FP.CPI.TOTL'):
# World Bank API endpoint for GDP data
api_url = f'http://api.worldbank.org/v2/country/{country_code}/indicator/{index}?data={year}&format=json'
# Make a GET request to the API
response = requests.get(api_url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Extract the GDP value from the response
gdp_value = data[1][0]['value'] if data[1] else None
return gdp_value
else:
# If the request was not successful, print an error message
print(f"Error: Unable to fetch data. Status code: {response.status_code}")
return None
def main():
print("Fetching data...")
data = fetch_data()
print(f"Done. (Fetched {len(data)} rows)")
if len(data) <= 0:
print("No new data fetched.")
return
print("Preparing data...")
data = prepare_data(data)
print("Done.")
print("Uploading data...")
upload_data(data)
print("Done.")
if __name__ == "__main__":
main()