Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def rowaddmult(A, i, j, c):
  4. A[j] += A[i] * c
  5. return A
  6.  
  7. def backsub(U,v):
  8. Copy = U.copy()
  9. rows,cols = Copy.shape
  10. ret = np.ones((rows))
  11. for i in range(rows-1,-1,-1):
  12. x = (v[i] - U[i,i+1:rows]@ret[i+1:rows]) / U[i,i]
  13. ret[i] = x
  14. return ret
  15.  
  16. def rowred(A):
  17. Copy = A.copy()
  18. rows,cols = Copy.shape
  19. for a in range(rows):
  20. for b in range(rows):
  21. if (b > a and Copy[a,a] != 0.0):
  22. Copy = rowaddmult(Copy, a, b, (-1)*Copy[b,a]/Copy[a,a])
  23. #print(Copy)
  24. return Copy
  25.  
  26. def solve(Aug):
  27. Ud = rowred(Aug)
  28. rows, cols = Ud.shape
  29. return backsub(Ud[:,0:rows], Ud[:,rows])
  30.  
  31.  
  32.  
  33. A = np.zeros((200, 3))
  34. A[0:100,0] = (np.random.rand(100) * 5) + 5
  35. A[0:100,1] = (np.random.rand(100) * 5) + 5
  36. A[100:,0] = (np.random.rand(100) * 4) + 8
  37. A[100:,1] = (np.random.rand(100) * 4) + 4
  38. A[:,2] = np.ones(200)
  39.  
  40. v = np.ones(200)
  41. v[100:] = -1
  42. #v = np.zeros(200)
  43.  
  44. M = np.ones((3,4))
  45. M[:, 0:3] = A.T@A
  46. M[:, 3] = A.T@v
  47. x = solve(M)
  48.  
  49. def determinefalses(A, x):
  50. #"Is this point in the cluster on the top left?'
  51. #The probability of getting a false positive (point is on top left when correct is point is on bottom right.);
  52. #aka
  53. #The probability of getting a false negative (that is, a 'no' answer when the correct answer is 'yes'.).
  54. false_pos = []
  55. false_neg = []
  56.  
  57. for i in range(0, 100):
  58. fxy = (x[0] * A[i,0]) + (x[1] * A[i,1]) + x[2]
  59. if fxy < 0:#double check this!
  60. false_pos.append((A[i,0], A[i,1]))
  61.  
  62. for i in range(100, 200):
  63. fxy = (x[0] * A[i,0]) + (x[1] * A[i,1]) + x[2]
  64. if fxy > 0:#double check this!
  65. false_neg.append((A[i,0], A[i,1]))
  66.  
  67. return [len(false_pos) / 100, len(false_neg) / 100]
  68.  
  69. falses = determinefalses(A, x)
  70.  
  71. print(f'Probability of False Positive: {falses[0]}\nProbability of False Negative: {falses[1]}')
  72.  
  73.  
  74.  
  75. %matplotlib inline
  76. import matplotlib.pyplot as plt
  77. x1 = np.linspace(5,12,1000) # 1000 linearly spaced numbers - read up on the np.linspace() command!
  78. y1 = -xhat[0]*x1/xhat[1] - xhat[2]/xhat[1]
  79. plt.plot(x1, y1, 'b')
  80.  
  81. plt.scatter(A[0:100,0],A[0:100,1],c='b')
  82. plt.scatter(A[100:,0],A[100:,1],c='r')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement