[英]Linear Regression without Sklearn
I am trying to write a class LinearModel, representing a linear regression model. it gives an error for def predict我正在尝试编写一个 class LinearModel,表示线性回归 model。它给出了 def 预测的错误
class LinearModel:
def __init__(self, X, y):
self.X = X
self.y = y
#X columns of 1s appended on its left,
X = np.vstack((np.ones((X.shape[0], )), X.T)).T
if len(self.X)!=len(y):
raise ("they are not similar")
def fit(self):
# stores this coefficient vector in the object.
Xt = np.transpose(X)
XtX = np.dot(Xt,X)
Xty = np.dot(Xt,y)
beta = np.linalg.solve(XtX,Xty)
self.fit = beta
def coef(self):
# raise an error if called before the model has been fitted
if not self.fit:
raise ValueError("Need to call the fit function first")
#returns βˆ.
return self.fit
def predict(self,X0=None): # Takes an optional argument X0
if not self.fit:
raise ValueError("Need to call the fit function first")
if X0==None:
X0 = X
X0 = np.vstack((np.ones((X0.shape[0], )), X0.T)).T
# where X0 is X0 with column of 1s added on its left.
prediction = self.fit * X0 # method should return X0 * βˆ
return prediction
X = np.array([[-1.34164079, -1.25675744], [-0.4472136, -0.48336824],
[0.4472136, 0.29002095], [1.34164079, 1.45010473]])
y = np.array([1, 3, 4, 6])
model = LinearModel(X, y)
model.fit()
print(model.coef())
print(model.predict())
edits编辑
class LinearModel:
def __init__(self, X, y):
self.X = X
self.y = y
#X columns of 1s appended on its left,
X = np.vstack((np.ones((X.shape[0], )), X.T)).T
if len(self.X)!=len(y):
raise ("they are not similar")
self._is_fitted : bool = False
def fit(self):
Xt = np.transpose(X)
XtX = np.dot(Xt,X)
Xty = np.dot(Xt,y)
beta = np.linalg.solve(XtX,Xty)
beta_array = np.array(beta)
self.fit = beta_array
self._is_fitted = True
def coef(self):
# raise an error if called before the model has been fitted
if not self._is_fitted:
raise ValueError("Need to call the fit function first")
#returns βˆ
return self.fit
def predict(self,X0=None): # Takes an optional argument X0
if not self._is_fitted:
raise ValueError("Need to call the fit function first")
if X0==None:
X0 = X
X0 = np.vstack((np.ones((X0.shape[0], )), X0.T)).T
# where X0 is X0 with column of 1s added on its left.
prediction = np.multiply(X0, self.fit) # method should return X0 * βˆ
return prediction
X = np.array([[-1.34164079, -1.25675744], [-0.4472136, -0.48336824],
[0.4472136, 0.29002095], [1.34164079, 1.45010473]])
y = np.array([1, 3, 4, 6])
model = LinearModel(X, y)
model.fit()
print(model.coef())
print(model.predict())
it gives an error of ValueError: operands could not be broadcast together with shapes (4,3) (2,) meaning that I will have to to broadcasting to multiply 2 matrices.它给出了 ValueError 错误:操作数无法与形状 (4,3) (2,) 一起广播,这意味着我将不得不广播以乘以 2 个矩阵。 Can anymore suggest me how?可以再建议我怎么做吗?
You can't use a method name as an attribute.您不能将方法名称用作属性。 Is self.fit
supposed to be a method to fit? self.fit
应该是一种适合的方法吗? Or the coefficients?还是系数?
You can't test if that attribute (let's call it beta
) is assigned simply by saying if self.beta
or something similar.您不能简单地通过说出if self.beta
或类似的东西来测试是否分配了该属性(让我们称之为beta
)。 First of all, if it is not, you'll get an error for trying to read an unassigned variable.首先,如果不是,您将在尝试读取未分配的变量时遇到错误。 Secondly, and that is the error you get, if that beta
contains something that is neither True nor False, you'll get an error about Truth value.其次,这就是你得到的错误,如果那个beta
包含既不是真也不是假的东西,你会得到关于真值的错误。 So, assign a value to beta
in the __init__
method.因此,在__init__
方法中为beta
赋值。 For example self.beta=None
.例如self.beta=None
。 And then test if it is None
with if beta is None
.然后用if beta is None
None
All attributes access must be prefixed with self.
所有属性访问都必须以self.
. . You are using X
instead of self.X
more than once.您不止一次使用X
而不是self.X
Which means that those X
are the global one.这意味着那些X
是全局的。 The one to which you haven't added a column of 1
.您尚未添加1
列的那个。
self.beta * X0
is not doing what you expect to do. self.beta * X0
没有做你期望做的事。 *
is a member×member multiplication, not a matrix multiplication (and if it were a matrix multiplication it would be an illegal one). *
是 member×member 乘法,而不是矩阵乘法(如果它是矩阵乘法,它将是非法的)。 @
(or .dot
) is the matrix multiplication operator. @
(或.dot
)是矩阵乘法运算符。 And to multiply a 3×n matrix by a 3 elements vector, you need to do it the other way.要将 3×n 矩阵乘以 3 元素向量,您需要以另一种方式进行。 So X0 @ self.beta
所以X0 @ self.beta
[Non fatal] There is a np.hstack
function, rather than vstack + double transpose
. [非致命] 有一个np.hstack
function,而不是vstack + double transpose
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.