Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.datasets import load_iris
- import pandas as pd
- iris = load_iris()
- data = pd.DataFrame(iris.data, columns=iris.feature_names)
- length = data.iloc[:,0] #length = data["sepal length (cm)"]
- width = data.iloc[:,1]
- label = iris.target
- def graph():
- #graph
- label = iris.target
- x = []
- y = []
- for i in range(2):
- index = np.where(label == i)[0]
- x.append(length[index])
- y.append(width[index])
- plt.scatter(x[i], y[i], label=iris.target_names[i])
- plt.legend()
- index = np.where(label < 2)[0]
- label = label[index]
- x = np.matrix(data.iloc[index,:2])
- one = np.matrix(np.ones((len(label)))).T
- x = np.hstack((one,x))
- w = np.matrix([0,0,0], dtype=np.float32).T
- def sigmoid(x):
- return 1/(1+np.exp(-x))
- eta = 0.1
- for j in range(20):
- per = np.random.permutation(100)
- E = 0
- for i in range(100):
- da = x[per][i].T
- y = sigmoid(w.T * da)
- t = label[per][i]
- E += t * np.log(y) + (1-t) * np.log(1-y)
- dE = (y[0,0]-t) * da
- w += -eta * dE
- print("w is:", w)
- print("error:", E[0,0])
- # w0 + w1*x + w2*y = 0
- gridX = np.linspace(4,7.5,100)
- gridY = np.linspace(1.5,4.5,100)
- Y = (-w[0,0]-w[1,0]*gridX)/w[2,0]
- plt.plot(gridX, Y)
- graph()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement