Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn.datasets import load_iris
  4. import pandas as pd
  5.  
  6. iris = load_iris()
  7.  
  8. data = pd.DataFrame(iris.data, columns=iris.feature_names)
  9. length = data.iloc[:,0] #length = data["sepal length (cm)"]
  10. width = data.iloc[:,1]
  11. label = iris.target
  12.  
  13.  
  14. def graph():
  15. #graph
  16. label = iris.target
  17. x = []
  18. y = []
  19. for i in range(2):
  20. index = np.where(label == i)[0]
  21. x.append(length[index])
  22. y.append(width[index])
  23. plt.scatter(x[i], y[i], label=iris.target_names[i])
  24. plt.legend()
  25.  
  26.  
  27.  
  28. index = np.where(label < 2)[0]
  29. label = label[index]
  30. x = np.matrix(data.iloc[index,:2])
  31. one = np.matrix(np.ones((len(label)))).T
  32. x = np.hstack((one,x))
  33.  
  34. w = np.matrix([0,0,0], dtype=np.float32).T
  35. def sigmoid(x):
  36. return 1/(1+np.exp(-x))
  37. eta = 0.1
  38.  
  39. for j in range(20):
  40. per = np.random.permutation(100)
  41. E = 0
  42. for i in range(100):
  43. da = x[per][i].T
  44. y = sigmoid(w.T * da)
  45. t = label[per][i]
  46. E += t * np.log(y) + (1-t) * np.log(1-y)
  47.  
  48. dE = (y[0,0]-t) * da
  49. w += -eta * dE
  50. print("w is:", w)
  51. print("error:", E[0,0])
  52. # w0 + w1*x + w2*y = 0
  53. gridX = np.linspace(4,7.5,100)
  54. gridY = np.linspace(1.5,4.5,100)
  55.  
  56. Y = (-w[0,0]-w[1,0]*gridX)/w[2,0]
  57. plt.plot(gridX, Y)
  58. graph()
  59. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement