Guest User

Untitled

a guest
May 21st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. import numpy as np
  2. import csv
  3. from sklearn.feature_extraction.text import TfidfVectorizer
  4. from sklearn.svm import LinearSVC
  5. from sklearn.pipeline import Pipeline
  6.  
  7. label_list = []
  8.  
  9. def label_map_target(label):
  10. ''' map chinese feature name to integer '''
  11. try:
  12. idx = label_list.index(label)
  13. except ValueError:
  14. idx = len(label_list)
  15. label_list.append(label)
  16.  
  17. return idx
  18.  
  19.  
  20. c1_list = []
  21. c2_list = []
  22. title_list = []
  23. with open(csv_file, 'r') as f:
  24. # row_from_csv is for shorting this example
  25. for row in row_from_csv(f):
  26. c1_list.append(label_map_target(row[0])
  27. c2_list.append(label_map_target(row[1])
  28. title_list.append(row[2])
  29.  
  30. data = np.array(title_list)
  31. target = np.array([c1_list, c2_list])
  32. print target.shape
  33. # (2, 4405)
  34. target = target.reshape(4405,2)
  35. print target.shape
  36. # (4405, 2)
  37.  
  38. docs_train, docs_test, y_train, y_test = train_test_split(
  39. data, target, test_size=0.25, random_state=None)
  40.  
  41. # vect = TfidfVectorizer(tokenizer=jieba_tokenizer, min_df=3, max_df=0.95)
  42. # use custom chinese tokenizer get same error
  43. vect = TfidfVectorizer(min_df=3, max_df=0.95)
  44. docs_train= vect.fit_transform(docs_train)
  45.  
  46. clf = LinearSVC()
  47. clf.fit(docs_train, y_train)
  48.  
  49. ---------------------------------------------------------------------------
  50. ValueError Traceback (most recent call last)
  51. <ipython-input-24-904eb9af02cd> in <module>()
  52. 1 clf = LinearSVC()
  53. ----> 2 clf.fit(docs_train, y_train)
  54.  
  55. C:Python27libsite-packagessklearnsvmclasses.pyc in fit(self, X, y)
  56. 198
  57. 199 X, y = check_X_y(X, y, accept_sparse='csr',
  58. --> 200 dtype=np.float64, order="C")
  59. 201 self.classes_ = np.unique(y)
  60. 202
  61.  
  62. C:Python27libsite-packagessklearnutilsvalidation.pyc in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric)
  63. 447 dtype=None)
  64. 448 else:
  65. --> 449 y = column_or_1d(y, warn=True)
  66. 450 _assert_all_finite(y)
  67. 451 if y_numeric and y.dtype.kind == 'O':
  68.  
  69. C:Python27libsite-packagessklearnutilsvalidation.pyc in column_or_1d(y, warn)
  70. 483 return np.ravel(y)
  71. 484
  72. --> 485 raise ValueError("bad input shape {0}".format(shape))
  73. 486
  74. 487
  75.  
  76. ValueError: bad input shape (3303, 2)
  77.  
  78. y.shape
Add Comment
Please, Sign In to add comment