【问题标题】:Python Logistic regression and future samplesPython 逻辑回归和未来样本
【发布时间】:2018-04-04 12:10:23
【问题描述】:

我正在学习 Python 中的逻辑回归,并设法将模型拟合到我现有的数据(股市数据)中,并且预测产生了不错的结果。

但我不知道如何转换该预测模型,以便将其应用于未来数据。即有没有可以用来输入未来样本的 y=ax+b 算法?如何使用“模型”?如何将预测用于后续数据?还是我在这里偏离了轨道-逻辑回归不是以这种方式应用的吗?

【问题讨论】:

  • "...预测产生了不错的结果。"你是如何得到这些预测的?
  • 我想我现在可以理解这个过程了——Bill,我将样本分为训练和测试,“好的结果”是测试样本上的混淆矩阵所指示的结果。但是我认为,如果我要获取一些新数据并在 newdata = logmodel.predict(new_data) 中引用该数据,那么“newdata”应该包含对该新数据的预测。

标签: python logistic-regression


【解决方案1】:

当您训练逻辑回归时,您会学习y = ax + b 中的参数ab。所以,经过训练,ab 是已知的,可以用来解方程y = ax + b

我不知道你用什么确切的 Python 包来训练你的模型以及你有多少类,但如果是这样的话,比如说numpy2 classes,预测函数可能看起来像这样:

import numpy as np

def sigmoid(z):
    """
    Compute the sigmoid of z.

    Arguments:
    z: a scalar or numpy array

    Return:
    s: sigmoid(z)
    """

    s = 1 / (1 + np.exp(-z))

    return s

def predict(w, b, X):
     '''
     Predict whether the label is 0 or 1 using learned logistic 
     regression parameters (w, b).

     Arguments:
     w: weights
     b: bias
     X: data to predict

    Returns:
    Y_pred: a numpy array (vector) containing all predictions (0/1) 
    for the examples in X
    '''

    m = X.shape[1] # number of instances in X
    Y_pred = np.zeros((1,m))
    w = w.reshape(X.shape[0], 1)

    # Apply the same activation function which you applied during 
    # training, in this case it is a sigmoid 
    A = sigmoid(np.dot(w.T, X) + b)


    for i in range(A.shape[1]):

        # Convert probabilities A[0,i] to actual predictions p[0,i]
        if A[0, i] > 0.5:
            Y_pred[0, i] = 1
        else:
            Y_pred[0, i] = 0

        return Y_pred

【讨论】:

  • 感谢您的回答。我正在使用 scikit learn 包,我有 29 个参数。我有一个“预测”数组,但在整个过程中我没有看到任何包含参数的数组。
猜你喜欢
  • 2022-11-27
  • 2018-07-26
  • 2016-03-06
  • 2020-03-22
  • 2016-12-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 2016-03-09
相关资源
最近更新 更多