Logistic回归模型不学习

问题描述 投票:1回答:2

我写了使用带有9点的属性以及标签的一个矢量数据回归算法,但它不是训练。 我想我有更新的权重,但不知道什么时候转一些投入,尝试了一下试验和错误,但没有运气的。 如果有人可以帮助表示感谢。

class logistic_regression(neural_network):
    def __init__(self,data):

        self.data = data   # to store the the data location in a varable 
        self.data1 = load_data(self.data) # load the data 
        self.weights =  np.random.normal(0,1,self.data1.shape[1] -1)   # use the number of attributes to get the number of weights
        self.bias = np.random.randn(1) #  set the bias to a random number 
        self.x = self.data1.iloc[:,0:9] # split the xs and ys
        self.y = self.data1.iloc[:,9:10]
        self.x = np.array(self.x)
        self.y = np.array(self.y)

        print(self.weights)
        print(np.dot(self.x[0].T,self.weights))
    def load_data(self,file):
        data = pd.read_csv(file)
        return data
    def sigmoid(self,x): # acivation function to limit the value to 0 and 1
        return 1 / (1 + np.exp(-x))
    def sigmoid_prime(self,x):
        return self.sigmoid(x) * (1 - self.sigmoid(x))
    def train(self):
        error = 0 # init the error to zero
        learning_rate = 0.01
        for interation in range(100):
            for i in range(len(self.x)): # loop though all the data
                pred = np.dot(self.x[i].T,self.weights) + self.bias # calculate the output
                pred1 = self.sigmoid(pred)
                error = (pred1 - self.y[i])**2 # check the accuracy of the network

                self.bias -= learning_rate * pred1 - self.y[i] * self.sigmoid_prime(pred1)
                self.weights -= learning_rate * (pred1 - self.y[i]) * self.sigmoid_prime(pred1) *  self.x[i]

            print(str(pred1)+"pred")
            print(str(error) + "error")  # print the result
            print(pred1[0] - self.y[i][0])
    def test(self):
python arrays numpy machine-learning logistic-regression
2个回答
1
投票

只使用一个标签,你可以不训练的任何机器学习模型。由此产生的模型将只有一个响应,无论使用什么样的测试数据 - 而培训所提供的标签。


1
投票

残破的衍生品

你必须在self.bias调整中的错误,遗漏周围PRED1-self.y [I]括号。

此外,你计算从错误的变量的导数 - 它似乎不是self.sigmoid_prime(PRED1)你需要self.sigmoid_prime(预解码)。

在玩具例子测试

对于任何这样的代码,我建议您首先测试它在一个非常简单的功能之一,是微不足道的打印出所有的中间值,并验证它们在纸面上。例如,布尔AND和OR功能。这会告诉你是否已经得到了更新的公式是正确的,从您的实际学习任务的特殊性隔离学习对码。

© www.soinside.com 2019 - 2024. All rights reserved.