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

stage_02_featurization.py 2.8 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
  1. import argparse
  2. import os
  3. import shutil
  4. from tqdm import tqdm
  5. import logging
  6. from src.utils.all_utils import read_yaml, create_directory, get_df
  7. from src.utils.featurize import save_matrix
  8. import numpy as np
  9. from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
  10. STAGE = "Two"
  11. logging.basicConfig(
  12. filename=os.path.join("logs", 'running_logs.log'),
  13. level=logging.INFO,
  14. format="[%(asctime)s: %(levelname)s: %(module)s]: %(message)s",
  15. filemode="a"
  16. )
  17. def main(config_path, params_path):
  18. config = read_yaml(config_path)
  19. params = read_yaml(params_path)
  20. artifacts = config["artifacts"]
  21. prepared_data_dir_path = os.path.join(artifacts["ARTIFACTS_DIR"], artifacts["PREPARED_DATA"])
  22. train_data_path = os.path.join(prepared_data_dir_path, artifacts["TRAIN_DATA"])
  23. test_data_path = os.path.join(prepared_data_dir_path, artifacts["TEST_DATA"])
  24. featurized_data_dir_path = os.path.join(artifacts["ARTIFACTS_DIR"], artifacts["FEATURIZED_DATA"])
  25. create_directory([featurized_data_dir_path])
  26. featurized_train_data_path = os.path.join(featurized_data_dir_path, artifacts["FEATURIZED_OUT_TRAIN"])
  27. featurized_test_data_path = os.path.join(featurized_data_dir_path, artifacts["FEATURIZED_OUT_TEST"])
  28. max_features = params["featurize"]["max_features"]
  29. ngrams = params["featurize"]["ngrams"]
  30. df_train = get_df(train_data_path)
  31. train_words = np.array(df_train.text.str.lower().values.astype("U"))
  32. bag_of_words = CountVectorizer(
  33. stop_words="english", max_features=max_features, ngram_range=(1, ngrams)
  34. )
  35. bag_of_words.fit(train_words)
  36. train_words_binary_matrix = bag_of_words.transform(train_words)
  37. tfidf = TfidfTransformer(smooth_idf=False)
  38. tfidf.fit(train_words_binary_matrix)
  39. train_words_tfidf_matrix = tfidf.transform(train_words_binary_matrix)
  40. save_matrix(df_train, train_words_tfidf_matrix, featurized_train_data_path)
  41. df_test = get_df(test_data_path)
  42. test_words = np.array(df_test.text.str.lower().values.astype('U'))
  43. test_words_binary_matrix = bag_of_words.transform(test_words)
  44. test_words_tfidf_matrix = tfidf.transform(test_words_binary_matrix)
  45. save_matrix(df_test, test_words_tfidf_matrix, featurized_test_data_path)
  46. if __name__ == '__main__':
  47. args = argparse.ArgumentParser()
  48. args.add_argument("--config", "-c", default="config/config.yaml")
  49. args.add_argument("--params", "-p", default="params.yaml")
  50. parsed_args = args.parse_args()
  51. try:
  52. logging.info("\n********************")
  53. logging.info(f">>>>> stage {STAGE} started <<<<<")
  54. main(config_path=parsed_args.config, params_path=parsed_args.params)
  55. logging.info(f">>>>> stage {STAGE} completed!<<<<<\n")
  56. except Exception as e:
  57. logging.exception(e)
  58. raise e
Tip!

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

Comments

Loading...