给出各拟合曲线的误差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])
时间: 2024-03-16 13:45:28 浏览: 80
在该代码中,我们使用了不同次数的多项式进行回归拟合,并使用均方误差(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值越小,表示拟合误差越小,模型拟合效果越好。
阅读全文