gpt4 book ai didi

python - 你能找出这个关于线性回归的正规方程实现的程序有什么问题吗

转载 作者:行者123 更新时间:2023-11-30 10:00:32 25 4
gpt4 key购买 nike

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```

最佳答案

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

关于python - 你能找出这个关于线性回归的正规方程实现的程序有什么问题吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59207895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com