Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- alpha=0.01
- niters=1000
- def h(X, theta):
- return 1 / (1 + np.e ** -(X.dot(theta.T)))
- def J(X, y, theta):
- m = X.shape[0]
- y_hat = h(X, theta)
- erro = (-y * np.log(y_hat) - (1-y) * np.log(1-y_hat)).sum(0)
- return erro / m
- def GD(X, y, theta, alpha, niters):
- m = X.shape[0]
- cost = np.zeros((niters,1))
- print('iteração:')
- for k in range(0, niters):
- print(' ',k,end='')
- y_hat = h(X, theta)
- erro = ((y_hat-y) *X).sum(0)/m
- theta -= (alpha * (erro))
- cost[k] = J(X, y, theta)
- print('\r\r\r\r\r\r',end='')
- return (cost, theta)
- def featureScaling(X):
- X=X-np.min(X,0)
- den=np.max(X,0)-np.min(X,0)
- return X/den
- def getClass(y):
- lem=((np.max(y)-np.min(y))/2 + np.min(y))
- y = y >= lem
- return y
- def getAll(y1,y2):
- trueNegative = 0
- falseNegative = 0
- truePositive = 0
- falsePositive = 0
- k = 0
- for i in y1:
- if i == 0 and i == y2[k]:
- trueNegative+= 1
- elif i == 1 and i == y2[k]:
- truePositive+= 1
- elif i == 0 and i != y2[k]:
- falseNegative+= 1
- elif i == 1 and i != y2[k]:
- falsePositive+= 1
- k+=1
- return trueNegative, truePositive, falseNegative, falsePositive
- f=open('Skin_NonSkin.txt')
- ## delete \n at the end of the string
- data=[d[:-1] for d in f.readlines()]
- ## split the string into the values
- data=[d.split('\t') for d in data]
- np.random.shuffle(data)
- # now data is a list of lists
- X=[d[:-1] for d in data]
- X=np.array(X,dtype=int)
- #### Inicio da área que é permita alguma mudança
- X=featureScaling(X)
- X=np.insert(X,0,1,axis=1)
- #### Fim da área que é permitida alguma mudança
- y=[d[-1] for d in data]
- y=np.reshape(np.array(y,dtype=int),(len(y),1))
- y[y==5]=0
- y[y==6]=1
- Theta=np.zeros((1,X.shape[1]))
- nsize=int(X.shape[0]*.7)
- Xtr=X[:nsize,:]
- Xte=X[nsize:,:]
- ytr=y[:nsize]
- yte=y[nsize:]
- c,t=GD(Xtr,ytr,Theta,alpha,niters)
- y_hat=getClass(h(Xtr,t))
- print('Taxa de acerto (treino):', np.mean(y_hat==ytr))
- y_hat=getClass(h(Xte,t))
- print('Taxa de acerto (teste):', np.mean(y_hat==yte))
- print('Blaaa\n', y_hat)
- y_hat[y_hat==False] = 0
- y_hat[y_hat==True] = 1
- print('Blaaa22222222\n', y_hat)
- print('bleeeee\n', yte)
- yte[yte==2] = 0
- yte[yte==1] = 1
- print('bleeeee2222\n', yte)
- trueNegative, truePositive, falseNegative, falsePositive=getAll(y_hat,yte)
- print(trueNegative, truePositive, falseNegative, falsePositive)
- print('precisao -> ' + str(truePositive / (truePositive + falsePositive)))
- print('recall -> ' + str(truePositive / (truePositive + falseNegative)))
- print('acuracia -> ' + str((truePositive + trueNegative) / (truePositive + falsePositive + trueNegative + falseNegative)))
- ##plt.plot(c)
- ##plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement