Guest User

Untitled

a guest
Aug 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. # Data Preprocessing
  2.  
  3. # Importing the Library
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. import pandas as pd
  7.  
  8. # Importing the dataset
  9. dataset= pd.read_csv('Data.csv')
  10. X = dataset.iloc[: , [2, 3]].values
  11. Y = dataset.iloc[: , 4].values
  12.  
  13. # Feature Scaling
  14. from sklearn.preprocessing import StandardScaler
  15. sc_X = StandardScaler()
  16. X_train = sc_X.fit_transform(X_train)
  17. X_test = sc_X.transform(X_test)
  18.  
  19. # Fitting Logistic Regression to the Training set
  20. from sklearn.linear_model import LogisticRegression
  21. classifier = LogisticRegression(random_state = 0)
  22. classifier.fit(X_train, y_train)
  23.  
  24. # Predicting the Test set results
  25. y_pred = classifier.predict(X_test)
  26.  
  27. # Making the Confusion Matrix
  28. fromsklearn.metrics import confusion_matrix
  29. cm = confusion_matrix(y_test, y_pred)
  30.  
  31. # Visualising the Training set results
  32. from matplotlib.colors import ListedColormap
  33. X_set, Y_set = X_train, y_train
  34. X1, X2 = np.meshgrid(np.arrange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
  35. np.arrange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)
  36. plt.contourf(X1, X2, classifier.predict(np.array([X1.rave(), X2.ravel()]).T).reshape(X1.shape),
  37. alpha = 0.75, cmap = ListedColormap(('red', 'Green')))
  38. plt.xlim(X1.min(), X1.max())
  39. plt.ylim(X1.min(), X1.max())
  40. for i, j in emunerate(np.unique(y_set)):
  41. plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1]
  42. c = ListedColormap(('red', 'green'))(i), label = j)
  43. plt.title('Logistic Regression (Training set)')
  44. plt.xlabel('Age')
  45. plt.ylabel('Estimated Salary')
  46. plt.legend()
  47. plt.show()
  48.  
  49. # Visualising the Test set results
  50. from matplotlib.colors import ListedColormap
  51. X_set, Y_set = X_train, y_train
  52. X1, X2 = np.meshgrid(np.arrange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
  53. np.arrange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)
  54. plt.contourf(X1, X2, classifier.predict(np.array([X1.rave(), X2.ravel()]).T).reshape(X1.shape),
  55. alpha = 0.75, cmap = ListedColormap(('red', 'Green')))
  56. plt.xlim(X1.min(), X1.max())
  57. plt.ylim(X1.min(), X1.max())
  58. for i, j in emunerate(np.unique(y_set)):
  59. plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1]
  60. c = ListedColormap(('red', 'green'))(i), label = j)
  61. plt.title('Logistic Regression (Test set)')
  62. plt.xlabel('Age')
  63. plt.ylabel('Estimated Salary')
  64. plt.legend()
  65. plt.show()
Add Comment
Please, Sign In to add comment