回归方程拟合度评价

  • SSTSST:总偏差平方和:因变量实际值与因变量平均值的差的平方和,反应因变量取值的总体波动情况
  • SSRSSR:回归平方和:因变量回归值与其均值的差的平方和,反映了因变量偏差值由X的关系所引起的偏差,可以由回归曲线进行解释。
  • SSESSE:残差平方和:因变量实际值与回归值的差的平方和,反映了因变量偏差值由X的关系以外的其他关系所引起的偏差,不可以由回归曲线进行解释。

SST=SSR+SSESST=SSR+SSE

定义:

R2=SSR/SSTR2=SSR/SST

其取值范围为010\sim 1越接近于1说明拟合效果越好。

线性回归实战

sklearn通过linear_model模块(广义线性模型)来进行线性回归预测,其模型原理为:

y(w,x)=w0+w1x1+w2x2++wpxpy(w,x)=w_0+w_1x_1+w_2x_2+\cdots+w_px_p

数据结构

为了更好地刻画参数,有必要将工艺数据参数化设计成一个数组。

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
class MaterialProperties:
def __init__(self, product_id, supplier, steel_type, thickness, width, temperatures, speed, fan_frequency, rolling_before_thickness, rolling_after_thickness, treatment_before_hardness, target_hardness_upper_limit, target_hardness_lower_limit):
self.product_id = product_id # 产品唯一编号
self.supplier = supplier # 原料商
self.steel_type = steel_type # 钢种
self.thickness = thickness # 厚度
self.width = width # 宽度
self.temperatures = temperatures # 温度数组
self.speed = speed # 速度
self.fan_frequency = fan_frequency # 风机频率
self.rolling_before_thickness = rolling_before_thickness # 轧制前厚度
self.rolling_after_thickness = rolling_after_thickness # 轧制后厚度
self.treatment_before_hardness = treatment_before_hardness # 处理前硬度
self.target_hardness_upper_limit = target_hardness_upper_limit # 目标硬度上限
self.target_hardness_lower_limit = target_hardness_lower_limit # 目标硬度下限

# 计算TV和前制程压下率
self.TV = self.calculate_TV()
self.pre_process_reduction_rate = self.calculate_pre_process_reduction_rate()

def calculate_TV(self):
"""计算TV值"""
return self.thickness * self.speed

def calculate_pre_process_reduction_rate(self):
"""计算前制程压下率"""
if self.rolling_before_thickness == 0:
# 如果轧制前厚度为0,表示一轧一退料,压下率为0
return 0
else:
return (self.rolling_before_thickness - self.rolling_after_thickness) / self.rolling_before_thickness

def get_property(self, property_name):
"""通过产品编号和属性名获取属性值"""
return getattr(self, property_name, None)

# 示例数据
product_id = "A001FG"
supplier = "供应商A"
steel_type = "钢种B"
thickness = 10.5
width = 1.2
temperatures = [20, 30, 40, 50, 60] # 温度区间数组
speed = 100
fan_frequency = 50
rolling_before_thickness = 0 # 轧制前厚度为0,表示一轧一退料
rolling_after_thickness = 10.0
treatment_before_hardness = [30, 40, 50, 60] # 处理前硬度数组
target_hardness_upper_limit = 50 # 示例目标硬度上限值
target_hardness_lower_limit = 30 # 示例目标硬度下限值

# 创建实例
material = MaterialProperties(product_id, supplier, steel_type, thickness, width, temperatures, speed, fan_frequency, rolling_before_thickness, rolling_after_thickness, treatment_before_hardness, target_hardness_upper_limit, target_hardness_lower_limit)

# 打印实例数据
print(f"{material.product_id}.supplier: {material.supplier}")
print(f"{material.product_id}.steel_type: {material.steel_type}")
print(f"{material.product_id}.thickness: {material.thickness}")
print(f"{material.product_id}.width: {material.width}")
print(f"{material.product_id}.temperatures: {material.temperatures}")
print(f"{material.product_id}.speed: {material.speed}")
print(f"{material.product_id}.fan_frequency: {material.fan_frequency}")
print(f"{material.product_id}.rolling_before_thickness: {material.rolling_before_thickness}")
print(f"{material.product_id}.rolling_after_thickness: {material.rolling_after_thickness}")
print(f"{material.product_id}.treatment_before_hardness: {material.treatment_before_hardness}")
print(f"{material.product_id}.TV: {material.TV}")
print(f"{material.product_id}.pre_process_reduction_rate: {material.pre_process_reduction_rate}")
print(f"{material.product_id}.target_hardness_upper_limit: {material.target_hardness_upper_limit}")
print(f"{material.product_id}.target_hardness_lower_limit: {material.target_hardness_lower_limit}")

一元线性回归