预测函数-

# 预测:以模型(theta)单一样本(xi)为输入,返回预测标签
def predict(xi, theta, bias=0):
    label = np.sign(xi @ theta + bias)
    return label

np.sum(predict(X_test, model.coef_[0], model.intercept_[0]) == y_test)/X_test.shape[0]
def predict(xi, theta, bias=0):
    label = np.sign(xi @ theta + bias)
    return label
  1. Function Definition: This defines a function named predict that takes three parameters - xi (a single sample), theta (model parameters), and an optional bias (default value is 0).

  2. Prediction Formula: The core of the function is the line label = np.sign(xi @ theta + bias). Here's the breakdown:

    • xi @ theta: This represents the dot product of the input sample xi and the model parameters theta.
    • xi @ theta + bias: This is the linear combination of the dot product and the bias term.
    • np.sign(...): This function returns the sign of the result. If the result is positive, it returns 1; if it's negative, it returns -1; and if it's zero, it returns 0.
    • label: The final predicted label for the input sample.
np.sum(predict(X_test, model.coef_[0], model.intercept_[0]) == y_test)/X_test.shape[0]
  1. Testing and Evaluation:
    • predict(X_test, model.coef_[0], model.intercept_[0]): This predicts labels for all samples in X_test using the model parameters model.coef_[0] (coefficients) and model.intercept_[0] (bias).
    • == y_test: This checks where the predicted labels match the actual labels in y_test, resulting in a boolean array of True and False.
    • np.sum(...): This calculates the total number of True values, which represent correct predictions.
    • /X_test.shape[0]: This normalizes the sum by dividing it by the total number of samples in X_test, giving the accuracy of the model on the test data.
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值