我是一名新生和初学者。
我正在通过开放教程学习机器学习。
我在制作梯度下降算法时遇到了麻烦
我需要完成”for _ in range(max_iter):
“,但是,我对numpy
不太了解…所以我不知道应该添加什么代码。你能帮我填补空白吗?
我知道这种类型的问题很无礼…对不起,但我需要你的帮助 :(提前谢谢你。
from sklearn import datasetsimport numpy as npfrom sklearn.metrics import accuracy_scoreX, y = datasets.make_classification(n_samples = 200, n_features = 2, random_state = 333,n_informative =2, n_redundant = 0 , n_clusters_per_class= 1)def sigmoid(s): return 1 / (1 + np.exp(-s))def loss(y, h): return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()def gradient(X, y, w): return -(y * X) / (1 + np.exp(-y * np.dot(X, w)))X_bias = np.append(np.ones((X.shape[0], 1)), X, axis=1)y = np.array([[1] if label == 0 else [0] for label in y])w = np.array([[random.uniform(-1, 1)] for _ in range(X.shape[1]+1)])max_iter = 100learning_rate = 0.1threshold = 0.5for _ in range(max_iter):#fill in the blankwhat code should i add ????probabilities = sigmoid(np.dot(X_bias, w))predictions = [[1] if p > threshold else [0] for p in probabilities]print("loss: %.2f, accuracy: %.2f" %(loss(y, probabilities), accuracy_score(y, predictions)))
回答:
在for
循环内部,我们首先需要计算概率。然后找到梯度,最后更新权重。
要计算概率,你可以使用下面的代码
probs=sigmoid(np.dot(X_bias,w))
np.dot
是numpy用于矩阵乘法的命令。然后我们将计算损失及其梯度。
J=loss(y,probs)dJ=gradient(X_bias,y,w)
现在我们将更新权重。
w=w-learning_rate*dJ
所以最终的代码将是
from sklearn import datasetsimport numpy as npfrom sklearn.metrics import accuracy_scoreX, y = datasets.make_classification(n_samples = 200, n_features = 2, random_state = 333,n_informative =2, n_redundant = 0 , n_clusters_per_class= 1)def sigmoid(s): return 1 / (1 + np.exp(-s))def loss(y, h): return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()def gradient(X, y, w): return -(y * X) / (1 + np.exp(-y * np.dot(X, w)))X_bias = np.append(np.ones((X.shape[0], 1)), X, axis=1)y = np.array([[1] if label == 0 else [0] for label in y])w = np.array([[np.random.uniform(-1, 1)] for _ in range(X.shape[1]+1)])max_iter = 100learning_rate = 0.1threshold = 0.5for _ in range(max_iter): probs=sigmoid(np.dot(X_bias,w)) J=loss(y,probs) dJ=gradient(X_bias,y,w) w=w-learning_rate*dJprobabilities = sigmoid(np.dot(X_bias, w))predictions = [[1] if p > threshold else [0] for p in probabilities]print("loss: %.2f, accuracy: %.2f" %(loss(y, probabilities), accuracy_score(y, predictions)))
注意:在for
循环中,没有必要计算probs和loss,因为我们只需要梯度来更新权重。我这样做是为了便于理解。