Search code examples
pythonmachine-learninglinear-regressiongradient-descent

TypeError: 'NoneType' object is not subscriptable (PYTHON)


I'm trying to write a python code for multivariate linear regression using mini-batch gradient descent. And there is an issue when running the predict function. xFeat is a nd-array with shape n x d. beta is an array of coefficients. yHat is the predicted value.

def predict(self, xFeat):
        X = np.array(xFeat)
        yHat = np.zeros(len(X))
        for i in range(X.shape[1]):
            yHat += X[:, i] * self.beta[i]  
        return yHat

Error:

    yHat += X[:, i] * self.beta[i] 
TypeError: 'NoneType' object is not subscriptable

I hope I gave enough information. Please let me know if more info is needed.


Solution

  • Your beta list is set to None in someplace. None always has no data and can not be subscriptable.

    The error means that you attempted to index an object whose type is None. NoneType is the type of the None object - it represents a lack of value.