zwn22 commited on
Commit
6eb7b56
1 Parent(s): e8a9c5c

Update NC_Crime.py

Browse files
Files changed (1) hide show
  1. NC_Crime.py +0 -57
NC_Crime.py CHANGED
@@ -165,63 +165,6 @@ class NCCrimeDataset(datasets.GeneratorBasedBuilder):
165
 
166
  return Durham_new
167
 
168
- def _preprocess_chapel_hill(self, file_path):
169
- # Load the dataset
170
- Chapel = pd.read_csv(file_path, low_memory=False)
171
-
172
- # Replace specified values with None
173
- replace_values = {'<Null>': None, 'NONE': None}
174
- Chapel['Weapon_Description'] = Chapel['Weapon_Description'].replace(replace_values)
175
-
176
- # Define the category mapping
177
- category_mapping = {
178
- 'Theft': ['THEFT/LARCENY', 'LARCENY FROM AU', 'LARCENY FROM PE', 'LARCENY OF OTHE', 'LARCENY FROM BU', 'LARCENY OF BIKE', 'LARCENY FROM RE', 'LARCENY OF AUTO'],
179
- 'Assault': ['ASSAULT/SEXUAL', 'ASSAULT', 'STAB GUNSHOT PE', 'ACTIVE ASSAILAN'],
180
- 'Burglary': ['BURGLARY', 'BURGLARY ATTEMP', 'STRUCTURE COLLAPSE', 'ROBBERY/CARJACK'],
181
- 'Drugs': ['DRUGS'],
182
- 'Traffic Violations': ['TRAFFIC STOP', 'TRAFFIC/TRANSPO', 'TRAFFIC VIOLATI', 'MVC', 'MVC W INJURY', 'MVC W INJURY AB', 'MVC W INJURY DE', 'MVC ENTRAPMENT'],
183
- 'Disorderly Conduct': ['DISTURBANCE/NUI', 'DOMESTIC DISTUR', 'DISPUTE', 'DISTURBANCE', 'LOST PROPERTY', 'TRESPASSING/UNW', 'REFUSAL TO LEAV', 'SUSPICIOUS COND', 'STRUCTURE FIRE'],
184
- 'Fraud': ['FRAUD OR DECEPT'],
185
- 'Sexual Offenses': ['SEXUAL OFFENSE'],
186
- 'Homicide': ['SUICIDE ATTEMPT', 'ABUSE/ABANDOMEN', 'DECEASED PERSON'],
187
- 'Weapons Violations': ['WEAPON/FIREARMS'],
188
- 'Animal-related Offenses': ['ANIMAL BITE', 'ANIMAL', 'ANIMAL CALL'],
189
- 'Missing Person': ['MISSING PERSON'],
190
- 'Public Service': ['PUBLIC SERVICE', 'PUBLICE SERVICE'],
191
- 'Miscellaneous': ['<Null>', 'SUSPICIOUS/WANT', 'MISC OFFICER IN', 'INDECENCY/LEWDN', 'PUBLIC SERVICE', 'TRESPASSING', 'UNKNOWN PROBLEM', 'LOUD NOISE', 'ESCORT', 'ABDUCTION/CUSTO', 'THREATS', 'BURGLAR ALARM', 'DOMESTIC', 'PROPERTY FOUND', 'FIREWORKS', 'MISSING/RUNAWAY', 'MENTAL DISORDER', 'CHECK WELL BEIN', 'PSYCHIATRIC', 'OPEN DOOR', 'ABANDONED AUTO', 'HARASSMENT THRE', 'JUVENILE RELATE', 'ASSIST MOTORIST', 'HAZARDOUS DRIVI', 'MVC', 'GAS LEAK FIRE', 'ASSIST OTHER AG', 'DOMESTIC ASSIST', 'SUSPICIOUS VEHI', 'UNKNOWN LE', 'ALARMS', '911 HANGUP', 'BOMB/CBRN/PRODU', 'STATIONARY PATR', 'LITTERING', 'HOUSE CHECK', 'CARDIAC', 'CLOSE PATROL', 'BOMB FOUND/SUSP', 'INFO FOR ALL UN', 'UNCONCIOUS OR F', 'LIFTING ASSISTA', 'ATTEMPT TO LOCA', 'SICK PERSON', 'HEAT OR COLD EX', 'CONFINED SPACE', 'TRAUMATIC INJUR', 'DROWNING', 'CITY ORDINANCE']
192
- }
193
- # Function to categorize crime
194
- def categorize_crime(crime):
195
- for category, crimes in category_mapping.items():
196
- if crime in crimes:
197
- return category
198
- return 'Miscellaneous'
199
-
200
- # Create a new DataFrame with simplified crime categories
201
- Chapel_new = pd.DataFrame({
202
- "year": pd.to_datetime(Chapel['Date_of_Occurrence']).dt.year,
203
- "city": "Chapel Hill",
204
- "crime_major_category": Chapel['Reported_As'].apply(categorize_crime),
205
- "crime_detail": Chapel['Offense'].str.title(),
206
- "latitude": Chapel['X'].round(5).fillna(0),
207
- "longitude": Chapel['Y'].round(5).fillna(0),
208
- "occurance_time": pd.to_datetime(Chapel['Date_of_Occurrence'].str.replace(r'\+\d{2}$', '', regex=True)).dt.strftime('%Y/%m/%d %H:%M:%S'),
209
- "clear_status": None,
210
- "incident_address": Chapel['Street'].str.replace("@", " "),
211
- "notes": Chapel['Weapon_Description'].apply(lambda x: f"Weapon: {x}" if pd.notnull(x) else "Weapon: None").str.title()
212
- }).fillna("No Data")
213
-
214
- # Correct the latitude and longitude if necessary
215
- Chapel_new.loc[(Chapel_new['latitude'].between(-80, -70)) & (Chapel_new['longitude'].between(30, 40)), ['latitude', 'longitude']] = Chapel_new.loc[(Chapel_new['latitude'].between(-80, -70)) & (Chapel_new['longitude'].between(30, 40)), ['longitude', 'latitude']].values
216
-
217
- # Ensure latitude and longitude are in the expected range
218
- Chapel_new = Chapel_new.loc[(Chapel_new['latitude'].between(30, 40)) & (Chapel_new['longitude'].between(-80, -70))]
219
-
220
- # Filter for years 2015 and onwards
221
- Chapel_new = Chapel_new[Chapel_new['year'] >= 2015]
222
-
223
- return Chapel_new
224
-
225
  def _preprocess_cary(self, file_path):
226
  # Load the dataset
227
  df = pd.read_csv(file_path, low_memory=False).dropna(subset=['Year'])
 
165
 
166
  return Durham_new
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  def _preprocess_cary(self, file_path):
169
  # Load the dataset
170
  df = pd.read_csv(file_path, low_memory=False).dropna(subset=['Year'])