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

featurization.py 2.1 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
  1. import pandas as pd
  2. import numpy as np
  3. import scipy.sparse as sparse
  4. from sklearn.feature_extraction.text import CountVectorizer
  5. from sklearn.feature_extraction.text import TfidfTransformer
  6. import conf
  7. try: import cPickle as pickle # python2
  8. except: import pickle # python3
  9. np.set_printoptions(suppress=True)
  10. import sys
  11. try: #python2
  12. reload(sys)
  13. sys.setdefaultencoding('utf-8')
  14. except: pass
  15. train_input = conf.train_tsv
  16. test_input = conf.test_tsv
  17. train_output = conf.train_matrix
  18. test_output = conf.test_matrix
  19. def get_df(input):
  20. df = pd.read_csv(
  21. input,
  22. encoding='utf-8',
  23. header=None,
  24. delimiter='\t',
  25. names=['id', 'label', 'text']
  26. )
  27. sys.stderr.write('The input data frame {} size is {}\n'.format(input, df.shape))
  28. return df
  29. def save_matrix(df, matrix, output):
  30. id_matrix = sparse.csr_matrix(df.id.astype(np.int64)).T
  31. label_matrix = sparse.csr_matrix(df.label.astype(np.int64)).T
  32. result = sparse.hstack([id_matrix, label_matrix, matrix], format='csr')
  33. msg = 'The output matrix {} size is {} and data type is {}\n'
  34. sys.stderr.write(msg.format(output, result.shape, result.dtype))
  35. with open(output, 'wb') as fd:
  36. pickle.dump(result, fd, pickle.HIGHEST_PROTOCOL)
  37. pass
  38. df_train = get_df(train_input)
  39. train_words = np.array(df_train.text.str.lower().values.astype('U'))
  40. bag_of_words = CountVectorizer(stop_words='english',
  41. max_features=5000,
  42. ngram_range=(1, 2))
  43. bag_of_words.fit(train_words)
  44. train_words_binary_matrix = bag_of_words.transform(train_words)
  45. tfidf = TfidfTransformer(smooth_idf=False)
  46. tfidf.fit(train_words_binary_matrix)
  47. train_words_tfidf_matrix = tfidf.transform(train_words_binary_matrix)
  48. save_matrix(df_train, train_words_tfidf_matrix, train_output)
  49. del df_train
  50. df_test = get_df(test_input)
  51. test_words = np.array(df_test.text.str.lower().values.astype('U'))
  52. test_words_binary_matrix = bag_of_words.transform(test_words)
  53. test_words_tfidf_matrix = tfidf.transform(test_words_binary_matrix)
  54. save_matrix(df_test, test_words_tfidf_matrix, test_output)
Tip!

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

Comments

Loading...