多项式拟合曲线

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# import pandas as pd
# import numpy as np
# from sklearn.preprocessing import PolynomialFeatures
# from sklearn.linear_model import LinearRegression
# import matplotlib.pyplot as plt
 
# # Read the data from the Excel file
# data = pd.read_excel(r'E:\\孙晓宇\\测试钞A1~E7共62开\\测试钞_磁扫特征统计结果\\combine.xlsx')
 
# # print(data.iloc[0])
 
# # Define the dependent variable
# y= data['平均值']
 
# # Define the independent variables
# X = data[['油墨类型','BLOCK_width', 'width_ratio', 'BLOCK_depth','BLOCK_carval','BLOCK_angle','类型']]
 
 
# # generate polynomial features of degree 2
# poly = PolynomialFeatures(degree=2, include_bias=True)
# X_poly = poly.fit_transform(X)
 
# # fit a linear regression model on the polynomial features
# model = LinearRegression().fit(X_poly, y)
 
 
# # use the model to make predictions for the new set of independent variables
# predictions = model.predict(X_poly)
 
# # print the predicted values
# print(predictions)
 
 
 
 
import pandas as pd
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
 
# Read the data from the Excel file
data = pd.read_excel(r'E:\\孙晓宇\\测试钞A1~E7共62开\\测试钞_磁扫特征统计结果\\combine.xlsx')
 
# print(data.iloc[0])
 
# Define the dependent variable
y= data['标准差']
 
# Define the independent variables
X = data[['油墨类型','BLOCK_width', 'width_ratio', 'BLOCK_depth','BLOCK_carval','BLOCK_angle','类型']]
 
 
# generate polynomial features of degree 2
poly = PolynomialFeatures(degree=10, include_bias=True)
X_poly = poly.fit_transform(X)
 
# fit a linear regression model on the polynomial features
model = LinearRegression().fit(X_poly, y)
 
 
 
# use the model to make predictions for the new set of independent variables
predictions = model.predict(X_poly)
 
 
# print the predicted values
print(predictions)
 
 
 
# calculate the residual errors
residuals = y - predictions
 
# plot the predicted values against the actual values
plt.scatter(y, predictions)
 
# plot a horizontal line at y=0 to show the line of perfect prediction
plt.plot([min(y), max(y)], [min(y), max(y)], 'k--', lw=2)
 
# plot the residual errors as a scatter plot
plt.scatter(y, residuals, c='r', s=10, alpha=0.5)
 
# add axis labels and a title
plt.xlabel('Actual values')
plt.ylabel('Predicted values')
plt.title('Prediction error')
 
# show the plot
plt.show()

  

posted @   水木清扬  阅读(75)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2019-04-18 tf调试函数
点击右上角即可分享
微信分享提示