1T Conte commited on
Commit
d13fb4b
1 Parent(s): 02d79b6

fix: adjusting configs

Browse files
Files changed (3) hide show
  1. convert.py +40 -3
  2. test.csv +0 -3
  3. train.csv +0 -3
convert.py CHANGED
@@ -70,6 +70,9 @@ def _clean_rank(rank):
70
 
71
 
72
  def run():
 
 
 
73
  rows = []
74
  categories = set()
75
 
@@ -109,14 +112,16 @@ def run():
109
  # Add a label to each row
110
  _categories = list(categories)
111
  _categories.sort()
112
-
113
  save_categories(_categories)
114
 
115
  for row in rows:
116
  row["label"] = _categories.index(row["category"])
117
 
118
  save_csv(rows)
 
119
  split_csv_train_test(test_size=0.2, random_state=42)
 
120
 
121
 
122
  def save_csv(rows, fname=OUTPUT_FILE):
@@ -137,8 +142,39 @@ def split_csv_train_test(**kwargs):
137
  """
138
  df = pd.read_csv(OUTPUT_FILE)
139
  train_df, test_df = train_test_split(df, **kwargs)
140
- train_df.to_csv(TRAIN_OUTPUT_FILE, index=False)
141
- test_df.to_csv(TEST_OUTPUT_FILE, index=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
 
144
  def save_categories(categories, fname="categories.txt"):
@@ -149,5 +185,6 @@ def save_categories(categories, fname="categories.txt"):
149
  for category in categories:
150
  f.write(category + os.linesep)
151
 
 
152
  if __name__ == "__main__":
153
  run()
 
70
 
71
 
72
  def run():
73
+ """
74
+ Run the conversion process.
75
+ """
76
  rows = []
77
  categories = set()
78
 
 
112
  # Add a label to each row
113
  _categories = list(categories)
114
  _categories.sort()
115
+
116
  save_categories(_categories)
117
 
118
  for row in rows:
119
  row["label"] = _categories.index(row["category"])
120
 
121
  save_csv(rows)
122
+
123
  split_csv_train_test(test_size=0.2, random_state=42)
124
+ filter_and_split_csv(["World", "Sports", "Business", "Sci/Tech"], "top4")
125
 
126
 
127
  def save_csv(rows, fname=OUTPUT_FILE):
 
142
  """
143
  df = pd.read_csv(OUTPUT_FILE)
144
  train_df, test_df = train_test_split(df, **kwargs)
145
+
146
+ os.makedirs("data/all", exist_ok=True)
147
+
148
+ train_df.to_csv(os.path.join("data", "all", TRAIN_OUTPUT_FILE), index=False)
149
+ test_df.to_csv(os.path.join("data", "all", TEST_OUTPUT_FILE), index=False)
150
+
151
+
152
+ def filter_and_split_csv(categories, config_name, is_balanced=True, **kwargs):
153
+ """
154
+ Filter the data by categories and split the data into training and testing
155
+ sets. If is_balanced is True, the data will be balanced to size of the
156
+ class with fewer examples.
157
+ """
158
+ df = pd.read_csv(OUTPUT_FILE)
159
+
160
+ if is_balanced:
161
+ dfs = []
162
+ for category in categories:
163
+ _df = df[df["category"] == category]
164
+ dfs.append(_df)
165
+
166
+ min_size = min([len(_df) for _df in dfs])
167
+
168
+ dfs = [df.sample(min_size) for df in dfs]
169
+ df = pd.concat(dfs)
170
+ else:
171
+ df = df[df["category"].isin(categories)]
172
+
173
+ os.makedirs(f"data/{config_name}", exist_ok=True)
174
+
175
+ train_df, test_df = train_test_split(df, **kwargs)
176
+ train_df.to_csv(os.path.join("data", config_name, TRAIN_OUTPUT_FILE), index=False)
177
+ test_df.to_csv(os.path.join("data", config_name, TEST_OUTPUT_FILE), index=False)
178
 
179
 
180
  def save_categories(categories, fname="categories.txt"):
 
185
  for category in categories:
186
  f.write(category + os.linesep)
187
 
188
+
189
  if __name__ == "__main__":
190
  run()
test.csv DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:9f59e985d4ad4a4999f93ddf182b77ec9399c75425c7496d92a2cfca3e31b639
3
- size 169961937
 
 
 
 
train.csv DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:3cb732b181495dbe39da430669d5b11d52da82de2dacf2d6296e35d80a40845a
3
- size 679752245