您能否确定此程序有关线性回归的法线方程实现的问题

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

1。在这里,我得到的theta值输出的数字很大,无法使用2.你能确定它有什么问题

import pandas as pd
import matplotlib.pyplot as plt
data=pd.read_csv("headbrain.csv")
data.head()
x=np.array(data["Head Size(cm^3)"].values)
y=np.array(data["Brain Weight(grams)"].values)
print(x.shape
x1=np.ones(len(y))
X=np.array([x,x1])
X.shape

#normal equation creating (x.transpose*x)*(x.transpose*y)
first=np.matmul(X,X.transpose())     #first part in normal equation(x.transpose*x)
second=np.matmul(X,y)                #second part in nornal equation(x.transpose*y)
theta=np.matmul(first,second)         #normal equation for theta
print(theta)

#i return theata values large number which includes e also``` 
python machine-learning regression linear-regression data-science
1个回答
0
投票
import pandas as pd
import matplotlib.pyplot as plt
data=pd.read_csv("headbrain.csv")
data.head()
x=np.array(data["Head Size(cm^3)"].values)
y=np.array(data["Brain Weight(grams)"].values)
print(x.shape)
x1=np.ones(len(y))
X=np.array([x,x1])



X_b = np.c_[np.ones((100, 1)), X]  # add x0 = 1 to each instance
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)

X_new = np.array([[0], [2]])
X_new_b = np.c_[np.ones((2, 1)), X_new]  # add x0 = 1 to each instance
y_predict = X_new_b.dot(theta_best)
y_predict
© www.soinside.com 2019 - 2024. All rights reserved.