Guest User

Untitled

a guest
Apr 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. from sklearn.metrics import classification_report, accuracy_score
  2. from sklearn.ensemble import IsolationForest
  3. from sklearn.neighbors import LocalOutlierFactor
  4.  
  5. # define a random state
  6. state = 1
  7.  
  8. # define the outlier detection method
  9. classifiers = {
  10. "Isolation Forest": IsolationForest(max_samples=len(X),
  11. contamination=outlier_fraction,
  12. random_state=state),
  13. "Local Outlier Factor": LocalOutlierFactor(
  14. n_neighbors = 20,
  15. contamination = outlier_fraction)
  16. }
  17. from sklearn.externals import joblib
  18. # fit the model
  19. n_outliers = len(Fraud)
  20.  
  21. for i, (clf_name, clf) in enumerate(classifiers.items()):
  22.  
  23. # fit te data and tag outliers
  24. if clf_name == "Local Outlier Factor":
  25. y_pred = clf.fit_predict(X)
  26. # Export the classifier to a file
  27. joblib.dump(clf, 'model.joblib')
  28. scores_pred = clf.negative_outlier_factor_
  29. else:
  30. clf.fit(X)
  31. scores_pred = clf.decision_function(X)
  32. y_pred = clf.predict(X)
  33. # Export the classifier to a file
  34. joblib.dump(clf, 'model.joblib')
  35.  
  36. # Reshape the prediction values to 0 for valid and 1 for fraudulent
  37. y_pred[y_pred == 1] = 0
  38. y_pred[y_pred == -1] = 1
  39.  
  40. n_errors = (y_pred != Y).sum()
  41.  
  42. # run classification metrics
  43. print('{}:{}'.format(clf_name, n_errors))
  44. print(accuracy_score(Y, y_pred ))
  45. print(classification_report(Y, y_pred ))
  46.  
  47. import pickle
  48. with open('model.pkl', 'wb') as model_file:
  49. pickle.dump(classifier, model_file)
Add Comment
Please, Sign In to add comment