Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

preprocess.py 2.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. import gcsfs
  2. import os
  3. import pandas as pd
  4. from sklearn.model_selection import train_test_split
  5. import yaml
  6. import reddit_utils
  7. with open(r"./general_params.yml") as f:
  8. params = yaml.safe_load(f)
  9. CHUNK_SIZE = params["chunk_size"]
  10. TARGET_LABEL = params["target_col"]
  11. UNIQUE_FLAIRS = [
  12. "Discussion",
  13. "Project",
  14. "Research",
  15. "None",
  16. "News",
  17. "Shameless Self Promo",
  18. "Inaccurate",
  19. "Misleading",
  20. "Clickbait",
  21. ]
  22. def load_and_process_data(remote_wfs, random_state=42):
  23. fs = gcsfs.GCSFileSystem(
  24. project=reddit_utils.PROJECT_NAME, token=os.environ[reddit_utils.GCLOUD_CRED_ENV_VAR]
  25. )
  26. with fs.open(os.path.join(remote_wfs, reddit_utils.TRAIN_DF_PATH), "a") as train_f, fs.open(
  27. os.path.join(remote_wfs, reddit_utils.TEST_DF_PATH), "a"
  28. ) as test_f:
  29. print("Loading data in chuncks...")
  30. for i, chunk in enumerate(
  31. pd.read_csv(os.path.join(remote_wfs, reddit_utils.RAW_DF_PATH), chunksize=CHUNK_SIZE)
  32. ):
  33. print(f"Processing chunk {i+1}...")
  34. processed_data = process(chunk)
  35. print("Splitting into train and test data...")
  36. train_chunk, test_chunk = train_test_split(
  37. processed_data,
  38. random_state=random_state,
  39. stratify=processed_data[TARGET_LABEL],
  40. )
  41. print("Saving to cloud...")
  42. save_data(train_chunk, train_f, test_chunk, test_f, i)
  43. def process(chunk):
  44. df = chunk.copy()
  45. df = df.drop(columns=["id", "author"])
  46. df = df.rename(columns={"selftext": "body", "link_flair_text": "flair"})
  47. df["title_len"] = df.title.str.len()
  48. df["body_len"] = df.body.str.len()
  49. df["has_thumbnail"] = [
  50. 0 if (x == "self" or x == "default") else 1 for x in df["thumbnail"]
  51. ]
  52. df = df.fillna({"body": "", "flair": "None", "body_len": 0})
  53. df["flair"] = ["Discussion" if (x == "Discusssion") else x for x in df["flair"]]
  54. df = pd.concat([df, pd.get_dummies(df["flair"], prefix="flair")], axis=1).drop(
  55. ["flair"], axis=1
  56. )
  57. for flair in UNIQUE_FLAIRS:
  58. flair_with_prefix = "flair_" + flair
  59. if flair_with_prefix not in df.columns:
  60. df[flair_with_prefix] = 0
  61. df = df[df["title"] != "[deleted by user]"]
  62. df = df[df["body"] != "[deleted]"]
  63. df = df[df["body"] != "[removed]"]
  64. df["title_and_body"] = (df["title"] + " " + df["body"]).astype(str)
  65. return df
  66. def save_data(train_chunk, train_f, test_chunk, test_f, i):
  67. # TODO: Saving is kinda slow now. Try to improve performance
  68. # We want to write the headers only once
  69. header = True if i == 0 else False
  70. train_chunk.to_csv(train_f, header=header, mode="a")
  71. test_chunk.to_csv(test_f, header=header, mode="a")
  72. if __name__ == "__main__":
  73. remote_wfs = reddit_utils.get_remote_gs_wfs()
  74. load_and_process_data(remote_wfs)
  75. print("Loading and processing done!")
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...