我有一些数据:
x_data = 0.603 + np.array(range(1,5))
y_data = np.array([22.8,78.6,129.7,181.3,])3
现在我想为线性回归创建自己的函数:
import numpy as np
import sympy as sp
def linear_fit(xi,yi):
a = sp.Symbol("a")
b = sp.Symbol("b")
data = np.transpose(np.array([xi,yi]))
res_sum = sum(np.array([(a * i + b - j)**2 for i, j in data]))
我不知道如何推导出这个和,然后如何求解“a”和“b”的方程式。 我想知道是否有更好的方法来定义线性回归而不是使用sympy。
答案 0 :(得分:1)
我喜欢这种精神来创建自己的回归模型,而不是使用其他库。 您可以尝试我编写的这段代码,然后根据需要对其进行更新。
import numpy as np
import time
"""
m is the coeficient and b0 is the y intercrept(the y obstacle in the y-axis) and
x_pred is the predicting data
in this model we predict the y by x so we should feed some xs to the data and some ys
and then we predict the y to the given x
"""
class Linear:
m = 0;#the coef
b0 = 0;#the y-intercrept
def train(x,y):#train data set simply we do is fit the xs to the ys
global b0;
global m;
pred = [];
c = 0;
m_x = np.mean(x);#mean of x
m_y = np.mean(y);#mean of y
s = len(x); #length of x
num,den = 0,0;#the denominator and the numerator
for i in range(s):
num += (x[i]-m_x)*(y[i]-m_y);#numerator #find m; m= (x[i]m_x(y[i]m_y)/(x[i]-m_x)**2 -->do this for s number of times(the number of xs in the data set) do this for number of xs in the data set
den += (x[i]-m_x)**2;#denominator
m = num/den;#find m; m= (x[i]-m_x)*(y[i]-m_y)/(x[i]-m_x)**2
b0 = m_y-(m*m_x);#find b0(the y-intercrept)
return pred;
def predict(x_pred):#predict model
global b0,m;
x_test = m*x_pred+b0;#y = m*x+b is the predicting equation
print("y={}*{}+{}".format(m,x,b0));
return x_test;
y = [3,6,9];
x = [5,10,15];
train = Linear.train(x,y);
p1 = Linear.predict(5);
print("y is:",p1);
这就是您的全部!!!! -从TOEKNEEHARSH
答案 1 :(得分:1)
简而言之,您需要实现用于计算假设,成本和梯度的所有功能,然后将它们组合以创建模型。
您可以查看使用NumPy
从头实现的this笔记本。
尽管此笔记本实现的是Logistic回归而不是线性回归,但您可以对如何实现线性回归有一个清晰的认识。