-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstratifiedsampling.py
More file actions
153 lines (120 loc) · 4.39 KB
/
stratifiedsampling.py
File metadata and controls
153 lines (120 loc) · 4.39 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
"""StratifiedSampling.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1MOtAiIXdFBlCjQB_qX5IgEAvWWn0hWF9
"""
import pandas as pd
import numpy as np
from google.colab import files
uploaded=files.upload()
df = pd.read_csv('player_injury_age_weather_cleaned (1).csv')
print(df)
df = df.drop('gsis_id', axis=1)
df=df.drop('Birthday', axis=1)
df=df.drop('first_name',axis=1)
df=df.drop('last_name', axis=1)
df=df.drop('full_name', axis=1)
df=df.drop('report_status', axis=1)
df=df.drop('game_type', axis=1)
df.to_csv('final_df_.csv', index=False)
print(df)
def replace_values(value):
value = value.replace('achilles', 'ankle')
value = value.replace('pectoral', 'chest')
value= value.replace('collarbone', 'chest')
value= value.replace('stinger', 'chest')
value= value.replace('oblique', 'core')
value= value.replace('arm', 'upper-arm')
value= value.replace('elbow', 'upper-arm')
value= value.replace('tricep', 'upper-arm')
value= value.replace('bicep', 'upper-arm')
return value
# Apply the custom function to the 'report_primary_injury' column
df['report_primary_injury'] = df['report_primary_injury'].apply(replace_values)
import matplotlib.pyplot as plt
columns_to_plot = ['report_primary_injury']
# Plot histograms for each column
for column in columns_to_plot:
plt.figure(figsize=(8, 6))
df[column].value_counts().sort_index().plot(kind='bar', color='skyblue')
plt.title(f'Histogram for {column}')
plt.xlabel(column)
plt.ylabel('Count')
plt.show()
def replace_values(value):
value = value.replace('leg', 'upper-leg')
value = value.replace('quad', 'upper-leg')
value= value.replace('hamstring', 'upper-leg')
value= value.replace('thigh', 'upper-leg')
value= value.replace('calf', 'lower-leg')
value= value.replace('shin', 'lower-leg')
return value
# Apply the custom function to the 'report_primary_injury' column
df['report_primary_injury'] = df['report_primary_injury'].apply(replace_values)
columns_to_plot = ['report_primary_injury']
# Plot histograms for each column
for column in columns_to_plot:
plt.figure(figsize=(8, 6))
df[column].value_counts().sort_index().plot(kind='bar', color='skyblue')
plt.title(f'Histogram for {column}')
plt.xlabel(column)
plt.ylabel('Count')
plt.show()
def replace_values(value):
value = value.replace('face', 'head')
value = value.replace('glute', 'other')
return value
# Apply the custom function to the 'report_primary_injury' column
df['report_primary_injury'] = df['report_primary_injury'].apply(replace_values)
columns_to_plot = ['report_primary_injury']
# Plot histograms for each column
for column in columns_to_plot:
plt.figure(figsize=(8, 6))
df[column].value_counts().sort_index().plot(kind='bar', color='skyblue')
plt.title(f'Histogram for {column}')
plt.xlabel(column)
plt.ylabel('Count')
plt.show()
from sklearn.model_selection import StratifiedShuffleSplit
# Mapping injury types to broader categories
injury_type_mapping = {
'ankle': 0,
'back': 1,
'chest': 2,
'concussion': 3,
'core':4,
'foot':5,
'groin':6,
'hand':7,
'head':8,
"hip":9,
'knee':10,
'lower-leg':11,
'neck':12,
'other':13,
'rib':14,
'shoulder':15,
'upper-arm':16,
'upper-leg':17,
'wrist':18,
# Add more mappings as needed based on your data
}
# Create the numeric injury categories
df["numeric_injury_category"] = df["report_primary_injury"].map(injury_type_mapping)
# Perform stratified sampling based on multiple features
split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)
# Pass a list of arrays for the features you want to consider
for train_index, test_index in split.split(df, df["numeric_injury_category"]):
strat_train_set = df.loc[train_index]
strat_test_set = df.loc[test_index]
# Drop the temporary 'numeric_injury_category' column if you don't need it
strat_train_set = strat_train_set.drop(["numeric_injury_category"], axis=1)
strat_test_set = strat_test_set.drop(["numeric_injury_category"], axis=1)
print(df)
# Save the stratified training set to a CSV file
strat_train_set.to_csv('stratified_train_set.csv', index=False)
# Save the stratified testing set to a CSV file
strat_test_set.to_csv('stratified_test_set.csv', index=False)
print(strat_test_set)
print(strat_train_set)