Twist_Nemo

regression (anime).py

Apr 16th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. import math
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import pandas as pd
  5. data = pd.read_csv('anime.csv')
  6. print(data['members'].isnull().values.any()) # Prints False
  7. print(data['rating'].isnull().values.any()) # Prints True
  8. members = [] # Corresponding fan club size for row
  9. ratings = [] # Corresponding rating for row
  10. for row in data.iterrows():
  11.  if not math.isnan(row[1]['rating']): # Checks for Null ratings
  12.   members.append(row[1]['members'])
  13.   ratings.append(row[1]['rating'])
  14. members = np.asarray(members)
  15. ratings = np.asarray(ratings)
  16. theta0 = 0.00001 # Random guess
  17. theta1 = 0.00001 # Random guess
  18. error = 0 # Overall error of our model.
  19. def hypothesis(x, theta0, theta1):
  20.  return  theta0 + theta1 * x
  21.  fig = plt.figure(dpi=100, figsize=(5, 4))
  22. plt.scatter(members, ratings)
  23. line, = plt.plot(members, hypothesis(members,theta0, theta0))
  24. plt.xlabel('Members')
  25. plt.ylabel('Ratings')
  26. plt.xlim((0, max(members)))
  27. plt.ylim((0, max(ratings)))
  28. plt.show()
  29. def costFunction(x, y, theta0, theta1, m):
  30.  loss = 0
  31.  for i in range(m): # Represents summation
  32.   loss += (hypothesis(x[i], theta0, theta1) - y[i])**2
  33.  loss *= 1 / (2 * m) # Represents 1/2m
  34.  return loss
  35.  def gradientDescent(x, y, theta0, theta1, alpha, m,iterations=1500):
  36.  for i in range(iterations):
  37.   gradient0 = 0
  38.   gradient1 = 0
  39.   for j in range(m): # Represents summation
  40.    gradient0 += hypothesis(x[j], theta0, theta1) - y[j]
  41.    gradient1 += (hypothesis(x[j], theta0, theta1) - y[j]) * x[j]
  42.   gradient0 *= 1/m
  43.   gradient1 *= 1/m
  44.   temp0 = theta0 - alpha * gradient0
  45.   temp1 = theta1 - alpha * gradient1
  46.   theta0 = temp0
  47.   theta1 = temp1
  48.   error = costFunction(x, y, theta0, theta1, len(y))
  49.   print("Error is:", error)
  50.  return theta0, theta1
  51.  alpha = 0.000000000001 # Learning Rate
  52. m = len(ratings) # Size of the dataset
  53. print('Our final theta\'s', gradientDescent(members, ratings,    theta0, theta1, alpha, m))
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment