vacendak

Logistic Regression

Apr 7th, 2020
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. import math
  2. from random import uniform
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6. random1 = []
  7. random2 = []
  8. random3 = []
  9. random4 = []
  10. random5 = []
  11.  
  12. xlist = []
  13.  
  14. theta = [5, 2, 1, 0.5, 0.1]
  15.  
  16. for i in range(250):
  17.     x = uniform(-5, 5)
  18.     xlist.append(x)
  19.     logreg1 = 1 / (1 + math.exp(-(theta[0] * x)))
  20.     logreg2 = 1 / (1 + math.exp(-(theta[1] * x)))
  21.     logreg3 = 1 / (1 + math.exp(-(theta[2] * x)))
  22.     logreg4 = 1 / (1 + math.exp(-(theta[3] * x)))
  23.     logreg5 = 1 / (1 + math.exp(-(theta[4] * x)))
  24.  
  25.     random1.append(logreg1)
  26.     random2.append(logreg2)
  27.     random3.append(logreg3)
  28.     random4.append(logreg4)
  29.     random5.append(logreg5)
  30.  
  31. print(plt.rcParams.get('figure.figsize'))
  32. fig_size = plt.rcParams["figure.figsize"]
  33. fig_size[0] = 10
  34. fig_size[1] = 6
  35. plt.rcParams["figure.figsize"] = fig_size
  36. print(plt.rcParams.get('figure.figsize'))
  37.  
  38. plt.scatter(xlist, random1, marker='.', s=20, c='orange', alpha=0.3, label=r'$\theta = %3.1f$' % (theta[0]))
  39. plt.scatter(xlist, random2, c='magenta', marker='.', alpha=0.3, s=20, label=r'$\theta = %3.1f$' % (theta[1]))
  40. plt.scatter(xlist, random3, c='navy', marker='.', alpha=0.3, s=20, label=r'$\theta = %3.1f$' % (theta[2]))
  41. plt.scatter(xlist, random4, c='green', marker='.', alpha=0.3, s=20, label=r'$\theta = %3.1f$' % (theta[3]))
  42. plt.scatter(xlist, random5, c='red', marker='.', alpha=0.3, s=20, label=r'$\theta = %3.1f$' % (theta[4]))
  43.  
  44. plt.axhline(y=0.5, label='P=0.5')
  45. plt.ylabel(r'P', fontsize=12)
  46. plt.xlabel(r'$x$', fontsize=12)
  47. plt.legend(fontsize=8)
  48. plt.title(r'$\mathrm{P=}\frac{1}{1+e^{-(\theta \, x)}}$', fontsize=18, color='r')
  49. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment