Logistic回归返回矩阵

问题描述

我用Numpy创建了Logistic回归算法。问题在于,当我计算权重时,权重是一个矩阵而不是向量,因此它不会引发任何错误,但是当我尝试预测某个输入而不是值时,我得到一个矩阵(输入*权重矩阵)。

我现在的权重应该是一个标量,但是以某种方式添加一个新尺寸可以解决我遇到的许多错误,如果您可以看一下代码并有任何想法,那就太好了。

import pandas as pd
import numpy as np

dataset = pd.read_csv('dataset.csv')
dataset = dataset.dropna(axis=0)

y = np.array(dataset['Survived']).reshape(-1,1)
X = np.array(dataset['Age']).reshape(-1,1)

class LogisticRegression:
    def __init__(self,lr=0.01,num_iter=100000,fit_intercept=False,verbose=True):
        self.lr = lr
        self.num_iter = num_iter
        self.fit_intercept = fit_intercept
        self.verbose = verbose
    
    def add_intercept(self,X):
        intercept = np.ones((X.shape[0],1))
        return intercept,np.concatenate((intercept,X),axis=1)
    
    def __sigmoid(self,z):
        return 1 / (1 + np.exp(-z))
        
    def __loss(self,h,y):
        return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
    
    def fit(self,X,y):
        if self.fit_intercept:
            _,X = self.add_intercept(X)
        
        # weights initialization
        # Here,using np.zeros(X.shape[1]) will be correct but doesn't seem to work (different shapes)
        self.theta = np.zeros([X.shape[1],X.shape[0]])
        
        for i in range(self.num_iter):
            z = np.dot(X,self.theta)
            h = self.__sigmoid(z)
            gradient = np.dot(X.T,(h - y)) / y.size
            self.theta -= self.lr * gradient
            
            if(self.verbose == True and i % 10000 == 0):
                z = np.dot(X,self.theta)
                h = self.__sigmoid(z)
                print(f'loss: {self.__loss(h,y)} \t')
    
    def predict_prob(self,X):
        if self.fit_intercept:
            _,X = self.add_intercept(X)
    
        return self.__sigmoid(np.dot(X,self.theta))
    
    def predict(self,threshold):
        return self.predict_prob(X) >= threshold

model = LogisticRegression(lr=0.1,num_iter=3000)
model.fit(X,y)

pred = model.predict(X[12],y[12])

intercept,_ = model.add_intercept(X)
yhat = intercept + model.theta * X
# yhat is the regression line from the model

解决方法

我运行了您的代码,X,y如下:

X = np.arange(10)
y = 4 * X + np.random.normal(size=X.shape)

它似乎运行良好(即没有错误)。考虑跳过当前所做的重塑,除非您以后要处理更大的尺寸。