linearregression().predict函数
时间: 2023-05-30 10:03:38 浏览: 323
linearregression().predict函数是用来预测线性回归模型中的因变量的函数。它接受一个numpy数组作为输入,并返回一个numpy数组,其中包含了每个输入数据点对应的预测值。预测值是通过对线性回归模型的系数和截距进行计算得到的。
相关问题
写一个linearregression.fit()函数代码的例子
好的,以下是一个简单的线性回归模型的fit函数实现示例:
```python
import numpy as np
class LinearRegression:
def __init__(self, lr=0.01, n_iters=1000):
self.lr = lr
self.n_iters = n_iters
self.weights = None
self.bias = None
def fit(self, X, y):
# 初始化权重和偏差
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
# 梯度下降训练模型
for _ in range(self.n_iters):
y_predicted = np.dot(X, self.weights) + self.bias
dw = (1 / n_samples) * np.dot(X.T, (y_predicted - y))
db = (1 / n_samples) * np.sum(y_predicted - y)
self.weights -= self.lr * dw
self.bias -= self.lr * db
def predict(self, X):
y_predicted = np.dot(X, self.weights) + self.bias
return y_predicted
```
其中,fit函数使用梯度下降法更新权重和偏差,训练模型。X是输入样本的特征矩阵,y是对应的目标值。在这个例子中,我们只考虑一个单变量的线性回归模型。
给出各拟合曲线的误差MSE:import numpy as np import pandas as pd import matplotlib.pyplot as plt from scipy.stats import zscore import numpy as np from sklearn import linear_model from sklearn.preprocessing import PolynomialFeatures data = np.loadtxt('tb.txt', delimiter=',') # a=data[:,0] area = data[:, 0] price = data[:, 1] length = len(area) area = np.array(area).reshape([length, 1]) price = np.array(price) minx = min(area) maxx = max(area) x = np.arange(minx, maxx).reshape([-1, 1]) poly=PolynomialFeatures(degree=2) poly3=PolynomialFeatures(degree=3) poly4=PolynomialFeatures(degree=4) #poly5=PolynomialFeatures(degree=5) area_poly=poly.fit_transform(area) area_poly3=poly3.fit_transform(area) area_poly4=poly4.fit_transform(area) linear2 = linear_model.LinearRegression() linear2.fit(area_poly, price) linear3 = linear_model.LinearRegression() linear3.fit(area_poly3, price) linear4 = linear_model.LinearRegression() linear4.fit(area_poly4, price) #查看回归方程系数 print('Cofficients:',linear4.coef_) #查看回归方程截距 print('intercept',linear4.intercept_) plt.scatter(area, price, color='red') plt.plot(x, linear2.predict(poly.fit_transform(x)), color='blue') plt.plot(x, linear3.predict(poly3.fit_transform(x)), linestyle='--') plt.plot(x, linear4.predict(poly4.fit_transform(x)), linestyle='-.') plt.legend(['degree=0','degree=2','degree=3','degree=4']) plt.xlabel('Year') plt.ylabel('Price') plt.show() # 2022 year_2022 = np.array([[2022]]) area_2022_poly = poly.transform(year_2022) area_2022_poly3 = poly3.transform(year_2022) area_2022_poly4 = poly4.transform(year_2022) price_2022_degree2 = linear2.predict(area_2022_poly) price_2022_degree3 = linear3.predict(area_2022_poly3) price_2022_degree4 = linear4.predict(area_2022_poly4) print("Predicted price in 2022 (degree=2):", price_2022_degree2[0]) print("Predicted price in 2022 (degree=3):", price_2022_degree3[0]) print("Predicted price in 2022 (degree=4):", price_2022_degree4[0]) # 2023 year_2023 = np.array([[2023]]) area_2023_poly = poly.transform(year_2023) area_2023_poly3 = poly3.transform(year_2023) area_2023_poly4 = poly4.transform(year_2023) price_2023_degree2 = linear2.predict(area_2023_poly) price_2023_degree3 = linear3.predict(area_2023_poly3) price_2023_degree4 = linear4.predict(area_2023_poly4) print("Predicted price in 2023 (degree=2):", price_2023_degree2[0]) print("Predicted price in 2023 (degree=3):", price_2023_degree3[0]) print("Predicted price in 2023 (degree=4):", price_2023_degree4[0])
在该代码中,我们使用了不同次数的多项式进行回归拟合,并使用均方误差(MSE)来评估拟合的误差。具体来说,我们可以使用sklearn中的mean_squared_error函数来计算MSE。以下是代码中的修改和MSE的计算:
```
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import zscore
import numpy as np
from sklearn import linear_model
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error
data = np.loadtxt('tb.txt', delimiter=',')
area = data[:, 0]
price = data[:, 1]
length = len(area)
area = np.array(area).reshape([length, 1])
price = np.array(price)
minx = min(area)
maxx = max(area)
x = np.arange(minx, maxx).reshape([-1, 1])
poly=PolynomialFeatures(degree=2)
poly3=PolynomialFeatures(degree=3)
poly4=PolynomialFeatures(degree=4)
area_poly=poly.fit_transform(area)
area_poly3=poly3.fit_transform(area)
area_poly4=poly4.fit_transform(area)
linear2 = linear_model.LinearRegression()
linear2.fit(area_poly, price)
linear3 = linear_model.LinearRegression()
linear3.fit(area_poly3, price)
linear4 = linear_model.LinearRegression()
linear4.fit(area_poly4, price)
# 评估拟合误差
mse2 = mean_squared_error(price, linear2.predict(area_poly))
mse3 = mean_squared_error(price, linear3.predict(area_poly3))
mse4 = mean_squared_error(price, linear4.predict(area_poly4))
print("MSE (degree=2):", mse2)
print("MSE (degree=3):", mse3)
print("MSE (degree=4):", mse4)
plt.scatter(area, price, color='red')
plt.plot(x, linear2.predict(poly.fit_transform(x)), color='blue')
plt.plot(x, linear3.predict(poly3.fit_transform(x)), linestyle='--')
plt.plot(x, linear4.predict(poly4.fit_transform(x)), linestyle='-.')
plt.legend(['degree=0','degree=2','degree=3','degree=4'])
plt.xlabel('Year')
plt.ylabel('Price')
plt.show()
# 2022
year_2022 = np.array([[2022]])
area_2022_poly = poly.transform(year_2022)
area_2022_poly3 = poly3.transform(year_2022)
area_2022_poly4 = poly4.transform(year_2022)
price_2022_degree2 = linear2.predict(area_2022_poly)
price_2022_degree3 = linear3.predict(area_2022_poly3)
price_2022_degree4 = linear4.predict(area_2022_poly4)
print("Predicted price in 2022 (degree=2):", price_2022_degree2[0])
print("Predicted price in 2022 (degree=3):", price_2022_degree3[0])
print("Predicted price in 2022 (degree=4):", price_2022_degree4[0])
# 2023
year_2023 = np.array([[2023]])
area_2023_poly = poly.transform(year_2023)
area_2023_poly3 = poly3.transform(year_2023)
area_2023_poly4 = poly4.transform(year_2023)
price_2023_degree2 = linear2.predict(area_2023_poly)
price_2023_degree3 = linear3.predict(area_2023_poly3)
price_2023_degree4 = linear4.predict(area_2023_poly4)
print("Predicted price in 2023 (degree=2):", price_2023_degree2[0])
print("Predicted price in 2023 (degree=3):", price_2023_degree3[0])
print("Predicted price in 2023 (degree=4):", price_2023_degree4[0])
```
输出结果中,MSE值越小,表示拟合误差越小,模型拟合效果越好。
阅读全文