我用Numpy创建了一个逻辑回归算法。问题是当我计算它时,权重是一个矩阵而不是向量,所以它不会抛出任何错误,但是当我尝试预测一些输入,而不是值时,我得到一个矩阵(输入*权重矩阵)
我现在认为权重应该是一个标量,但不知何故,添加一个新维度修复了我遇到的许多错误,如果你能看看代码并有任何想法,那就太好了
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, X, 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
如下所示:它似乎运行良好(即没有错误)。考虑跳过当前正在做的整形,除非你想以后处理更高的维度。
相关问题 更多 >
编程相关推荐