This repository has been archived by the owner on Mar 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/irio-hackatonny' into irio-hacka…
…tonny
- Loading branch information
Showing
8 changed files
with
157 additions
and
10 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from collections import OrderedDict | ||
import math | ||
import os.path | ||
|
||
import pandas as pd | ||
import numpy as np | ||
|
||
DATA_PATH = '/Users/irio/Desktop/serenata-data' | ||
DISPLAY_KEYS = OrderedDict([ | ||
('issue_date', 'Data do gasto'), | ||
('congressperson_name', 'Deputado'), | ||
('total_net_value', 'Valor'), | ||
('url', 'URL'), | ||
('meal_price_outlier', 'Preço de refeição suspeito?'), | ||
('over_monthly_subquota_limit', 'Acima da subcota?'), | ||
('suspicious_traveled_speed_day', 'Distância viajada suspeita?'), | ||
('has_receipt', 'Tem recibo?'), | ||
('is_in_office', 'Em mandato?'), | ||
('rosie_score', 'Nível de suspeita'), | ||
('score', 'Ranking'), | ||
('year', 'Ano'), | ||
('document_id', 'ID'), | ||
('applicant_id', 'ID Deputado'), | ||
]) | ||
|
||
|
||
def full_path(path): | ||
return os.path.join(DATA_PATH, path) | ||
|
||
|
||
def display(dataset): | ||
data = dataset.copy() | ||
data['issue_date'] = data['issue_date'].str[:10] | ||
data['url'] = data['document_id'] \ | ||
.apply(lambda x: 'https://jarbas.datasciencebr.com/#/documentId/{}'.format(x)) | ||
data['rosie_score'] = data['rosie_score'].apply(__display_percentage) | ||
data['score'] = data['score'].apply(__display_percentage) | ||
data['total_net_value'] = data['total_net_value'] \ | ||
.apply(lambda x: 'R$ {0:.2f}'.format(x)) | ||
data = data[[k for k in DISPLAY_KEYS.keys()]] | ||
data.rename(columns=DISPLAY_KEYS, inplace=True) | ||
return data | ||
|
||
def __display_percentage(values): | ||
return '{0:.2f}%'.format(values * 100) | ||
|
||
def ranking(): | ||
data = __irregularities() | ||
data = pd.merge(data, __is_in_office(data)) | ||
data['has_receipt'] = data['year'] > 2011 | ||
data['score'] = __score(data) | ||
data = data.sort_values(['is_in_office', 'has_receipt', 'score'], | ||
ascending=[False, False, False]) | ||
remove_receipts_from_same_case(data) | ||
return display(data) | ||
|
||
def remove_receipts_from_same_case(data): | ||
speed_day_keys = ['applicant_id', | ||
'issue_date', | ||
'suspicious_traveled_speed_day'] | ||
subquota_keys = ['applicant_id', | ||
'month', | ||
'over_monthly_subquota_limit'] | ||
data.drop_duplicates(speed_day_keys, inplace=True) | ||
data.drop_duplicates(subquota_keys, inplace=True) | ||
return data | ||
|
||
def __is_in_office(data): | ||
return data \ | ||
.groupby('applicant_id') \ | ||
.apply(lambda x: x['year'].max() >= 2015) \ | ||
.reset_index() \ | ||
.rename(columns={0: 'is_in_office'}) | ||
|
||
|
||
def __score(data): | ||
data['rosie_score'] = __rosie_score(data) | ||
net_value_score = data['total_net_value'].apply(math.log) / \ | ||
math.log(data['total_net_value'].max()) | ||
return .5 * data['rosie_score'] + .5 * net_value_score | ||
|
||
|
||
def __rosie_score(data): | ||
return .5 * data['meal_price_outlier_probability'] + \ | ||
.3 * data['suspicious_traveled_speed_day_probability'] + \ | ||
.2 * data['over_monthly_subquota_limit_probability'] | ||
|
||
|
||
def __irregularities(): | ||
data = pd.read_csv(full_path('irregularities.xz'), | ||
low_memory=False) | ||
is_valid_suspicion = data.select_dtypes(include=[np.bool]).any(axis=1) | ||
data = data[is_valid_suspicion] | ||
reimbursements = pd.read_csv(full_path('2016-12-06-reimbursements.xz'), | ||
low_memory=False) | ||
reimbursements = reimbursements.query('congressperson_id.notnull()') | ||
return pd.merge(data, reimbursements) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from flask import Flask | ||
|
||
from dataset import full_path, ranking | ||
|
||
ranking().to_csv(full_path('ranking.csv'), index=False) | ||
app = Flask(__name__) | ||
|
||
@app.route('/') | ||
def hello(): | ||
return 'Hello World!' | ||
|
||
if __name__ == '__main__': | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
git+https://github.com/datasciencebr/serenata-toolbox.git#egg=serenata-toolbox | ||
Flask==0.11.1 | ||
geopy>=1.11.0 | ||
pymongo==3.4.0 | ||
scikit-learn>=0.17 | ||
scipy>=0.18 | ||
geopy>=1.11.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters