Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. alpha=0.01
  5. niters=1000
  6.  
  7. def h(X, theta):
  8.     return 1 / (1 + np.e ** -(X.dot(theta.T)))
  9.  
  10.  
  11. def J(X, y, theta):
  12.     m = X.shape[0]
  13.     y_hat = h(X, theta)
  14.     erro = (-y * np.log(y_hat) - (1-y) * np.log(1-y_hat)).sum(0)
  15.     return erro / m
  16.  
  17.  
  18. def GD(X, y, theta, alpha, niters):
  19.     m = X.shape[0]
  20.     cost = np.zeros((niters,1))
  21.     print('iteração:')
  22.     for k in range(0, niters):
  23.         print(' ',k,end='')
  24.         y_hat = h(X, theta)
  25.         erro = ((y_hat-y) *X).sum(0)/m
  26.         theta -= (alpha * (erro))
  27.         cost[k] = J(X, y, theta)
  28.         print('\r\r\r\r\r\r',end='')
  29.     return (cost, theta)
  30.  
  31. def featureScaling(X):
  32.   X=X-np.min(X,0)
  33.   den=np.max(X,0)-np.min(X,0)
  34.   return X/den
  35.  
  36. def getClass(y):
  37.     lem=((np.max(y)-np.min(y))/2 + np.min(y))
  38.     y = y >= lem
  39.     return y
  40.  
  41. def getAll(y1,y2):
  42.     trueNegative = 0
  43.     falseNegative = 0
  44.     truePositive = 0
  45.     falsePositive = 0
  46.     k = 0
  47.     for i in y1:
  48.         if i == 0 and i == y2[k]:
  49.             trueNegative+= 1
  50.         elif i == 1 and i == y2[k]:
  51.             truePositive+= 1
  52.         elif i == 0 and i != y2[k]:
  53.             falseNegative+= 1
  54.         elif i == 1 and i != y2[k]:
  55.             falsePositive+= 1  
  56.         k+=1
  57.     return trueNegative, truePositive, falseNegative, falsePositive
  58.  
  59.  
  60. f=open('Skin_NonSkin.txt')
  61. ## delete \n at the end of the string
  62. data=[d[:-1] for d in f.readlines()]
  63. ## split the string into the values
  64. data=[d.split('\t') for d in data]
  65. np.random.shuffle(data)
  66. # now data is a list of lists
  67. X=[d[:-1] for d in data]
  68.  
  69. X=np.array(X,dtype=int)
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. #### Inicio da área que é permita alguma mudança
  80.  
  81. X=featureScaling(X)
  82. X=np.insert(X,0,1,axis=1)
  83.  
  84. #### Fim da área que é permitida alguma mudança
  85.  
  86.  
  87.  
  88.  
  89.  
  90. y=[d[-1] for d in data]
  91. y=np.reshape(np.array(y,dtype=int),(len(y),1))
  92. y[y==5]=0
  93. y[y==6]=1
  94.  
  95. Theta=np.zeros((1,X.shape[1]))
  96.  
  97. nsize=int(X.shape[0]*.7)
  98.  
  99. Xtr=X[:nsize,:]
  100. Xte=X[nsize:,:]
  101. ytr=y[:nsize]
  102. yte=y[nsize:]
  103.  
  104. c,t=GD(Xtr,ytr,Theta,alpha,niters)
  105.  
  106. y_hat=getClass(h(Xtr,t))
  107. print('Taxa de acerto (treino):', np.mean(y_hat==ytr))
  108. y_hat=getClass(h(Xte,t))
  109. print('Taxa de acerto (teste):', np.mean(y_hat==yte))
  110.  
  111.  
  112.  
  113. print('Blaaa\n', y_hat)
  114.  
  115. y_hat[y_hat==False] = 0
  116. y_hat[y_hat==True] = 1
  117.  
  118. print('Blaaa22222222\n', y_hat)
  119.  
  120. print('bleeeee\n', yte)
  121.  
  122. yte[yte==2] = 0
  123. yte[yte==1] = 1
  124.  
  125. print('bleeeee2222\n', yte)
  126.  
  127. trueNegative, truePositive, falseNegative, falsePositive=getAll(y_hat,yte)
  128.        
  129.  
  130. print(trueNegative, truePositive, falseNegative, falsePositive)
  131.  
  132. print('precisao -> ' + str(truePositive / (truePositive + falsePositive)))
  133. print('recall -> ' + str(truePositive / (truePositive + falseNegative)))
  134. print('acuracia -> ' + str((truePositive + trueNegative) / (truePositive + falsePositive + trueNegative + falseNegative)))
  135.  
  136. ##plt.plot(c)
  137. ##plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement