您能否找出这个关于线性回归正常方程实现的程序有什么问题

1.我得到了包含很大数字的theta值,无法使用。
2.您能确定它有什么问题吗?

import pandas as pdimport matplotlib.pyplot as pltdata=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 thetaprint(theta)#i return theata values large number which includes e also``` 

回答:

import pandas as pdimport matplotlib.pyplot as pltdata=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 instancetheta_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 instancey_predict = X_new_b.dot(theta_best)y_predict

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注