【发布时间】:2019-09-13 18:43:38
【问题描述】:
我是大一和初学者, 我在制作逻辑回归算法时遇到了麻烦。 我在我的教科书中附上了代码。我应该填写什么代码? 4~5行以内就好了。 非常感谢
from sklearn import datasets
import numpy as np
from sklearn.metrics import accuracy_score
X, 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 = 100
learning_rate = 0.1
threshold = 0.5
for _ in range(max_iter):
# fill in the blanks
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)))
填空
【问题讨论】:
标签: python machine-learning logistic-regression