Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from sklearn.utils import shuffle
  5.  
  6.  
  7. def get_w(X,y):
  8.  
  9. w = np.linalg.inv(X.T @ X) @ X.T @ y
  10. return w
  11.  
  12. def predict(w, input_data):
  13.  
  14. predict = input_data @ w
  15. return predict
  16.  
  17. df = pd.read_csv("diamonds.csv", index_col=0)
  18. df = shuffle(df)
  19.  
  20. cut_class_dict = {"Fair": 1, "Good": 2, "Very Good": 3, "Premium": 4, "Ideal": 5}
  21. clarity_dict = {"I3": 1, "I2": 2, "I1": 3, "SI2": 4, "SI1": 5, "VS2": 6, "VS1": 7, "VVS2": 8, "VVS1": 9, "IF": 10, "FL": 11}
  22. color_dict = {"J": 1,"I": 2,"H": 3,"G": 4,"F": 5,"E": 6,"D": 7}
  23.  
  24. df["cut"] = df["cut"].map(cut_class_dict)
  25. df["clarity"] = df["clarity"].map(clarity_dict)
  26. df["color"] = df["color"].map(color_dict)
  27.  
  28. X = np.array(df.drop(["price"],1))
  29. y = np.array(df["price"])
  30.  
  31. X = np.append(np.ones((X.shape[0],1)), X, axis=1)
  32.  
  33. w = get_w(X, y)
  34.  
  35. for i in range(20):
  36. print(f"Predicted {predict(w, X[i])} / Real {y[i]}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement