python-3.x - 逻辑回归成本 = nan

标签 python-3.x numpy machine-learning scikit-learn logistic-regression

我正在尝试实现逻辑回归模型,但不断获得“nan”值作为成本。我用多个数据集尝试过,但给出了相同的结果。不同的来源给出的梯度下降的实现略有不同,所以我不确定这里梯度的实现是否正确。这是完整的代码:

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split

class LogisticRegression:
    def __init__(self, lr=0.001, n_iter=8000):
        self.lr = lr
        self.n_iter = n_iter
        self.weights = None

    """
    z is dot product of features and weights, which is then mapped to discrete values, such as between 0 and 1
    """
    def sigmoid(self, z):
        return 1.0/(1+np.exp(-z))

    def predict(self, x_features, weights):
        """Returns 1d array of probabilities that the class label == 1"""
        z = np.dot(x_features, weights)
        return self.sigmoid(z)

    def cost(self, x_features, labels, weights):
        """
        Using Mean Absolute Error

        Cost = (labels*log(predictions) + (1-labels)*log(1-predictions) ) / len(labels) 
        """
        observation = len(labels)
        predictions = self.predict(x_features, weights)
        #take the error when label = 1
        class1_cost = -labels*np.log(predictions)
        #take the error when label = 0
        class2_cost = (1-labels)*np.log(1-predictions)
        #take sum of both the cost
        cost = class1_cost+class2_cost
        #take the average cost
        cost = cost.sum()/observation
        return cost

    def update_weight(self, x_features, labels, weights):
        """
        Vectorized Gradient Descent
        """
        N = len(x_features)
        #get predictions (approximation of y)
        predictions = self.predict(x_features, weights)
        gradient = np.dot(x_features.T, predictions-labels)
        #take the average cost of derivative for each feature
        gradient /= N
        #multiply gradients by our learning rate
        gradient *= self.lr
        #subtract from our weights to minimize cost
        weights -= gradient
        return weights

    def give_predictions(self, x_features, weights):
        linear_model_prediction =  self.predict(x_features, weights)
        y_predicted_cls = [1 if i>0.5 else 0 for i in linear_model_prediction]
        return y_predicted_cls

    def train(self, features, labels):
        n_samples, n_features = features.shape
        self.weights = np.zeros((n_features,1)) #initialize the weight matrix
        cost_history = []
        for i in range(self.n_iter):
            self.weights = self.update_weight(features, labels, self.weights)
            #calculate error for auditing purposes
            cost = self.cost(features, labels, self.weights)
            cost_history.append(cost)
            #Log process
            if i%1000 == 0:
                print("iter: {}, cost: {}".format(str(i),str(cost)))

        return self.weights, cost_history

def generate_data():
    bc = datasets.load_breast_cancer()
    x_features, labels = bc.data, bc.target

    x_train, x_test, y_train, y_test = train_test_split(x_features, labels, test_size=0.2, random_state=1234)
    return x_train, x_test, y_train, y_test

x_train, x_test, y_train, y_test = generate_data()

model = LogisticRegression()
model.train(x_train, y_train)

最佳答案

在训练模型之前,我必须对 x_train 应用特征缩放。我使用了 sklearn StandardScaler 库

from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
x_train = sc_X.fit_transform(x_train)

关于python-3.x - 逻辑回归成本 = nan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59850219/

相关文章:

python-3.x - 如何在 Python 3.8 中使用 TLS 1.0?

python - 将像素强度范围从 [0,255] 更改为 [0,1]

machine-learning - 处理文本数据以进行分类

matlab - PCA 中的矢量化

python 3 : How to call function from another file and pass arguments to that function ?

python - 如何删除嵌套列表中的重复项?

python-3.x - python3模拟成员变量多次获取

python - SciPy optimize.fmin ValueError : zero-size array to reduction operation maximum which has no identity

python - 如何在 Python 中创建矩阵或将二维数组转换为矩阵?

python - Tfidf内存错误: How to avoid this issue?