pip install numpy pandas scikit-learn matplotlib

import numpy as np
import pandas as pd
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

# 定义指数衰减模型函数
def exponential_decay(t, T0, k):
return T0 * np.exp(-k * t)

# 生成模拟数据
np.random.seed(0)
n_samples = 100
time = np.linspace(0, 10, n_samples)  # 时间数据

# 创建一个包含时间和冷却方式的DataFrame
data = pd.DataFrame({
'Time': time,
'Cooling_Method': np.random.choice(['Natural', 'Fan1', 'Fan2'], size=n_samples)
})

# 根据不同的冷却方式生成温度数据
data['Temperature'] = 500  # 初始温度
data.loc[data['Cooling_Method'] == 'Natural', 'Temperature'] = data['Temperature'] * np.exp(-0.1 * data['Time'])
data.loc[data['Cooling_Method'] == 'Fan1', 'Temperature'] = data['Temperature'] * np.exp(-0.2 * data['Time'])
data.loc[data['Cooling_Method'] == 'Fan2', 'Temperature'] = data['Temperature'] * np.exp(-0.15 * data['Time'])

# 拟合指数衰减模型
fit_parameters = {}
for method in data['Cooling_Method'].unique():
subset = data[data['Cooling_Method'] == method]
params, _ = curve_fit(exponential_decay, subset['Time'], subset['Temperature'])
fit_parameters[method] = params

# 绘制拟合曲线
plt.figure(figsize=(10, 6))
for method, params in fit_parameters.items():
fit_time = np.linspace(0, 10, 100)
fit_temperature = exponential_decay(fit_time, *params)
plt.plot(fit_time, fit_temperature, label=f'{method}拟合')

# 绘制原始数据
for method, subset in data.groupby('Cooling_Method'):
plt.scatter(subset['Time'], subset['Temperature'], label=method)

plt.xlabel('时间')
plt.ylabel('温度')
plt.legend()
plt.title('不同冷却方式下的温度趋势拟合')
plt.show()

pip install numpy pandas scikit-learn

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression

# 准备数据
data = pd.DataFrame({
'Time': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],  # 时间数据
'Temperature': [500, 450, 410, 380, 355, 330, 310, 295, 280, 265],  # 温度数据
'Cooling_Method': ['Natural', 'Fan1', 'Fan2', 'Natural', 'Fan1', 'Fan2', 'Natural', 'Fan1', 'Fan2', 'Natural']  # 冷却方式
})

# 使用独热编码将分类变量转换为虚拟变量
data_encoded = pd.get_dummies(data, columns=['Cooling_Method'], drop_first=True)

# 准备自变量X和因变量y
X = data_encoded[['Time', 'Cooling_Method_Fan1', 'Cooling_Method_Fan2']]
y = data_encoded['Temperature']

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

# 拟合模型
model.fit(X, y)

# 打印模型参数
intercept = model.intercept_
coefficients = model.coef_

print("截距 (Intercept):", intercept)
print("系数 (Coefficients):", coefficients)