Guest User

Untitled

a guest
Jun 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. from sklearn import datasets
  2. from sklearn import svm
  3. from sklearn.neighbors import KNeighborsClassifier
  4. from sklearn.linear_model import LogisticRegression
  5. from sklearn.cross_validation import train_test_split
  6. from sklearn.preprocessing import LabelEncoder
  7.  
  8. import numpy as np
  9. import pandas as pd
  10. import matplotlib.pyplot as plt
  11. from collections import defaultdict
  12.  
  13. df_train = pd.read_csv('/Users/justinchristensen/Documents/Python_Education/SKLearn/Path_Training_Data.csv')
  14. df_test = pd.read_csv('/Users/justinchristensen/Documents/Python_Education/SKLearn/Path_Test_Data.csv')
  15.  
  16.  
  17. #Separate columns in training data set
  18. x_train = df_train.iloc[:,:-1]
  19. y_train = df_train.iloc[:,-1:]
  20.  
  21. #Separate columns in test data set
  22. x_test = df_test.iloc[:,:-1]
  23.  
  24. #Initiate classifier
  25. clf = svm.SVC(gamma=0.001, C=100)
  26. le = LabelEncoder()
  27.  
  28. #Transform strings into integers
  29. x_train_encoded = x_train.apply(LabelEncoder().fit_transform)
  30. y_train_encoded = y_train.apply(LabelEncoder().fit_transform)
  31. x_test_encoded = x_test.apply(LabelEncoder().fit_transform)
  32.  
  33. #Fit the model into the classifier
  34. clf.fit(x_train_encoded,y_train_encoded)
  35.  
  36. #Predict test values
  37. y_pred = clf.predict(x_test_encoded)
  38. ---------------------------------------------------------------------------
  39. NotFittedError Traceback (most recent call last)
  40. <ipython-input-38-09840b0071d5> in <module>()
  41. 1
  42. ----> 2 y_pred_inverse = le.inverse_transform(y_pred)
  43.  
  44. ~/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/label.py in inverse_transform(self, y)
  45. 146 y : numpy array of shape [n_samples]
  46. 147 """
  47. --> 148 check_is_fitted(self, 'classes_')
  48. 149
  49. 150 diff = np.setdiff1d(y, np.arange(len(self.classes_)))
  50.  
  51. ~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
  52. 766
  53. 767 if not all_or_any([hasattr(estimator, attr) for attr in attributes]):
  54. --> 768 raise NotFittedError(msg % {'name': type(estimator).__name__})
  55. 769
  56. 770
  57.  
  58. NotFittedError: This LabelEncoder instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
  59.  
  60. y_train_encoded = y_train.apply(le().fit_transform)
  61. y_test_encoded = y_test.apply(le().fit_transform)
Add Comment
Please, Sign In to add comment