Advertisement
EXTREMEXPLOIT

Students Linear Model

Jun 6th, 2019
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. import sklearn
  2. from sklearn.utils import shuffle
  3. from sklearn.neighbors import KNeighborsClassifier
  4. import pandas as pd
  5. import numpy as np
  6. from sklearn import linear_model, preprocessing
  7. import matplotlib.pyplot as plt
  8. from matplotlib import style
  9. from mpl_toolkits.mplot3d import Axes3D
  10. import matplotlib.pyplot as plt
  11.  
  12. def GetAccuracity(Accuracity):
  13.     Accuracity = str(round(Accuracity*100,2)) + str("%")
  14.     print("- Accuracity:",Accuracity)
  15.  
  16. def Get_2D_Plot(XLabel, YLabel, Prediction, Data, Style):
  17.     style.use(Style)
  18.     plt.scatter(Data[XLabel], Data[Prediction])
  19.     plt.xlabel(XLabel)
  20.     plt.ylabel(YLabel)
  21.     plt.show()
  22.  
  23. def Get_3D_Plot(XLabel, YLabel, ZLabel, Data, Style):
  24.     style.use(Style)
  25.     Graph = plt.figure()
  26.     ax = Graph.add_subplot(111, projection='3d')
  27.  
  28.     x = Data[XLabel]
  29.     y = Data[YLabel]
  30.     z = Data[ZLabel]
  31.     ax.scatter(x, y, z, c='r', marker='o')
  32.  
  33.     ax.set_xlabel(XLabel)
  34.     ax.set_ylabel(YLabel)
  35.     ax.set_zlabel(ZLabel)
  36.  
  37.     plt.show()
  38.  
  39. Data = pd.read_csv("student-mat.csv", sep=";")
  40. Data = Data[["G1", "G2", "G3", "studytime", "failures", "absences"]]
  41. MyPrediction = "G3"
  42.  
  43. Features = np.array(Data.drop([MyPrediction], 1))
  44. CA = np.array(Data[MyPrediction])
  45.  
  46. # Linear Model.
  47. x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(Features, CA, test_size = 0.1)
  48. LinearModel = linear_model.LinearRegression()
  49. LinearModel.fit(x_train, y_train)
  50. Accuracity = LinearModel.score(x_test, y_test)
  51.  
  52. XLabel = "G1"
  53. YLabel = "G2"
  54. ZLabel = "G3"
  55. Get_3D_Plot(XLabel, YLabel, ZLabel, Data, "ggplot")
  56. Get_2D_Plot(XLabel, YLabel, MyPrediction, Data, "ggplot")
  57. GetAccuracity(Accuracity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement