Repository navigation
Expand file tree
/
Copy pathbuild_neg_data.py
More file actions
85 lines (67 loc) · 2.7 KB
/
Copy pathbuild_neg_data.py
File metadata and controls
85 lines (67 loc) · 2.7 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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 9 21:35:35 2019
"""
import pandas as pd, numpy as np,random
from geopy.geocoders import Nominatim
from geopy.distance import geodesic
def Nearest_Gas_Station(Data,Gas_Stations):
lat=Data['Latitude'].tolist()
long=Data['Longitude'].tolist()
new_Location=[]
for k in range(0,len(lat)):
street = (lat[k], long[k])
store_dist=[]
for j in Gas_Stations:
gas_st=(j.latitude,j.longitude)
store_dist.append(geodesic(street, gas_st).miles)
shortest=min(store_dist)
i=store_dist.index(shortest)
new_Location.append(Gas_Stations[i].address)
Data['Location']=new_Location
return Data
Data=pd.read_csv('Traffic_Violations_Features.csv')
Data=Data.loc[pd.isnull(Data['Latitude'])==False] ##Select only data that has coordinates
Data=Data.reset_index(drop=True)
Data=Data.iloc[0:20000,0:len(Data.columns)]
geolocator = Nominatim(user_agent="Enter your email")
Gas_Stations = geolocator.geocode("Gas Stations in Montgomery County Maryland ",exactly_one=False,timeout=10,limit=100)
Data=Nearest_Gas_Station(Data,Gas_Stations)
# Assign all current data as a positive sample
numRows = Data.shape[0]
Labels= [1] * numRows
n_onelabels=len(Labels) #Balance out labels
n_zerolabels=0
# Flesh out negative samples
initial_rows = numRows*3
# Randomly Select a row and a column
while (n_zerolabels < 5 * n_onelabels):
print(n_zerolabels)
randRow1 = random.randint(0, numRows - 1)
randRow2 = random.randint(0, numRows - 1)
randCol = random.randint(1, Data.shape[1] - 1) # First column seems to be an index, we don't want that
newSample = Data.iloc[randRow1,:]
newSample[randCol] = Data.iloc[randRow2,randCol]
Data.loc[Data.shape[0]] = newSample
Data.duplicated(keep='first')
if(Data.shape[0] != numRows):
Labels.append(0)
n_zerolabels+=1
numRows = numRows + 1
else:
print("There is a repeat")
Data['Label'] = Labels
# geolocator = Nominatim(user_agent="Enter your email")
# Gas_Stations = geolocator.geocode("Gas Stations in Montgomery County Maryland ",exactly_one=False,timeout=10,limit=100)
# Data=Nearest_Gas_Station(Data,Gas_Stations)
feature_columns = ['Label',
'SubAgency',
'Gender',
'Race',
'Date Of Stop',
'Time Of Stop',
'Location',
'Year']
classification_df = Data[feature_columns]
classification_df.to_csv("Traffic_Violations_With_5_Times_Negatives.csv", sep='\t')