优化fun_data, 使数据更加逼真, 宽度越大、厚度越大、重量越大、车间温度越高, 温度随时间衰减的速度越慢, 退火类型(O态比H2态更慢)、冷却类型(自然冷却比单面风机更慢, 单面风机比双面风
  X5zJxoD00Cah 2023年11月02日 50 0
# 导入所需的库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import train_test_split

# 指定支持中文的字体,例如SimHei或者Microsoft YaHei
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

# 生成数据
def fun_data():
    # 定义物料数量
    num_materials = 40

    # 定义每个物料的温度测量次数和时间间隔
    num_measurements = 10
    time_interval_hours = 6

    # 创建一个时间数组,模拟测量时间点
    measurement_times = np.arange(0, num_measurements * time_interval_hours, time_interval_hours)

    # 创建一个空的DataFrame来存储数据
    data = pd.DataFrame(columns=['Material_ID', 'Measurement_Time', 'Width', 'Thickness', 'Weight', 'Workshop_Temperature',
                                 'Annealing_Type', 'Cooling_Type', 'Temperature'])

    # 模拟每个物料的数据
    for material_id in range(1, num_materials + 1):
        # 生成物料特征数据(宽度、厚度、重量、车间温度、退火类型、冷却类型)
        width = np.random.uniform(5, 20)  # 宽度范围在5到20之间
        thickness = np.random.uniform(1, 5)  # 厚度范围在1到5之间
        weight = np.random.uniform(10, 100)  # 重量范围在10到100之间
        workshop_temperature = np.random.uniform(20, 30)  # 车间温度范围在20到30之间
        annealing_type = np.random.choice(['O态', 'H2态'])  # 随机选择退火类型
        cooling_type = np.random.choice(['自然冷却', '单面风机', '双面风机'])  # 随机选择冷却类型

        # 模拟温度数据(指数衰减)
        initial_temperature = np.random.uniform(100, 200)  # 初始温度范围在100到200之间
        decay_rate = np.random.uniform(0.01, 0.1)  # 衰减速率范围在0.01到0.1之间
        temperature_data = initial_temperature * np.exp(-decay_rate * measurement_times)

        # 创建一个临时DataFrame来存储物料的数据
        material_data = pd.DataFrame({
            'Material_ID': [material_id] * num_measurements,
            'Measurement_Time': measurement_times,
            'Width': [width] * num_measurements,
            'Thickness': [thickness] * num_measurements,
            'Weight': [weight] * num_measurements,
            'Workshop_Temperature': [workshop_temperature] * num_measurements,
            'Annealing_Type': [annealing_type] * num_measurements,
            'Cooling_Type': [cooling_type] * num_measurements,
            'Temperature': temperature_data
        })

        # 将物料数据添加到总体数据中
        data = pd.concat([data, material_data], ignore_index=True)
        
        # 修改数据类型
        data['Measurement_Time'] = data['Measurement_Time'].astype("float64")
        
    return data

# 导入数据
data = fun_data()

# 温度与其他变量的关系呈指数回归, 且温度随时间的增长呈指数衰减趋势, 设计模型如下
Temperature = np.exe(
    k1 + k2 * Measurement_Time + \
    k3 * Width + k4 * Thickness + k5 * Weight + k6 * Workshop_Temperature + \
    k7 * Annealing_Type_O态 + k8 * Annealing_Type_H2态 + \
    k9 * Cooling_Type_自然冷却 + k10 * Cooling_Type_单面风机 + k11 * Cooling_Type_双面风机
)

# 定义自变量和因变量
# 对类别型的自变量进行编码,使用独热编码(one-hot encoding),其他默认为数值型自变量
X = data[['Measurement_Time', 'Width', 'Thickness', 'Weight', 'Workshop_Temperature', 'Annealing_Type', 'Cooling_Type']]
X = pd.get_dummies(X, columns=['Annealing_Type', 'Cooling_Type']) # 将退火类型和冷却类型转换为哑变量
y = data['Temperature'] # 选择温度作为因变量

# 划分训练集和测试集,使用8:2的比例
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 设置随机种子为42

# 对因变量进行对数变换
y_train_log = np.log(y_train)
y_test_log = np.log(y_test)

# 创建线性回归模型对象
model = LinearRegression()

# 拟合训练数据
model.fit(X_train, y_train_log)

# 预测测试数据
y_pred_log = model.predict(X_test)

# 计算均方误差(MSE)和决定系数(R2)
mse = mean_squared_error(y_test_log, y_pred_log)
r2 = r2_score(y_test_log, y_pred_log)

# 打印模型的参数和评估指标
print("Intercept:", model.intercept_)
print("Coefficients:", model.coef_)
print("MSE:", mse)
print("R2:", r2)

# 将模型的函数表示为指数形式
def exp_model(x):
    # x是一个包含所有自变量的数组
    # 返回一个预测的温度值
    log_y = model.intercept_ + np.dot(model.coef_, x)
    y = np.exp(log_y)
    return y

# 打印模型的函数
print("The exponential regression model is:")
print("Temperature = exp({:.4f} + {:.4f} * Measurement_Time + {:.4f} * Width + {:.4f} * Thickness + {:.4f} * Weight + {:.4f} * Workshop_Temperature + {:.4f} * Annealing_Type_O态 + {:.4f} * Annealing_Type_H2态 + {:.4f} * Cooling_Type_自然冷却 + {:.4f} * Cooling_Type_单面风机 + {:.4f} * Cooling_Type_双面风机)".format(model.intercept_, *model.coef_))


优化fun_data, 使数据更加逼真,
宽度越大、厚度越大、重量越大、车间温度越高, 温度随时间衰减的速度越慢,
退火类型(O态比H2态更慢)、冷却类型(自然冷却比单面风机更慢, 单面风机比双面风机更慢)
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  gBkHYLY8jvYd   2023年12月09日   30   0   0 cii++数据
X5zJxoD00Cah