如何编写逻辑回归(梯度下降法)代码?

我是一名新生和初学者,遇到编写逻辑回归算法的困难。我附上了教科书中的代码。请问我应该如何填写空白处?最好在4到5行内完成。非常感谢您。

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 blanksprobabilities = 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)))

填写空白处


回答:

这基本上非常简单。

定义假设函数:

theta0 = 0theta1 = 0def hyp(x): return theta0 + theta1*x

定义成本函数:

def cost(hyp, x, y):    total1 = 0    total2 = 0    for i in range(1, len(x)):        total1 += hyp(x[i]) - y[i]        total2 += (hyp(x[i]) - y[i]) * x[i]return total1 / len(x), total2 / len(x)

调用函数:

for i in range(50):    s1, s2 = cost(hyp, x, y)    theta1 = theta1 - alpha * s2    theta0 = theta0 - alpha * s1

学习参数将会更新。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注