Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Python
Posted by: Levente Endre Nádai
Added: Jan 7, 2020 9:41 PM
Modified: Jan 7, 2020 9:43 PM
Views: 11
  1. # Regression Template
  2.  
  3. # Importing the libraries
  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('Position_Salaries.csv')
  10. X = dataset.iloc[:, 1:2].values
  11. y = dataset.iloc[:, 2].values
  12.  
  13. # Splitting the dataset into the Training set and Test set
  14. """from sklearn.cross_validation import train_test_split
  15. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)"""
  16.  
  17. # Feature Scaling
  18. """from sklearn.preprocessing import StandardScaler
  19. sc_X = StandardScaler()
  20. X_train = sc_X.fit_transform(X_train)
  21. X_test = sc_X.transform(X_test)
  22. sc_y = StandardScaler()
  23. y_train = sc_y.fit_transform(y_train)"""
  24.  
  25. # Fitting the Regression Model to the dataset
  26. # Create your regressor here
  27.  
  28. # Predicting a new result
  29. y_pred = regressor.predict(6.5)
  30.  
  31. # Visualising the Regression results
  32. plt.scatter(X, y, color = 'red')
  33. plt.plot(X, regressor.predict(X), color = 'blue')
  34. plt.title('Truth or Bluff (Regression Model)')
  35. plt.xlabel('Position level')
  36. plt.ylabel('Salary')
  37. plt.show()
  38.  
  39. # Visualising the Regression results (for higher resolution and smoother curve)
  40. X_grid = np.arange(min(X), max(X), 0.1)
  41. X_grid = X_grid.reshape((len(X_grid), 1))
  42. plt.scatter(X, y, color = 'red')
  43. plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
  44. plt.title('Truth or Bluff (Regression Model)')
  45. plt.xlabel('Position level')
  46. plt.ylabel('Salary')
  47. plt.show()