Forums
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt
import numpy as np
file_path = r"D:\表型图片\999.xlsx"
df = pd.read_excel(file_path)
print("Data Overview:")
print(df.head())
X = df[['length', 'width']]
y = df['area']
# 设置随机种子以保证结果可复现
np.random.seed(42)
num_iterations = 10
fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(20, 8))
axes = axes.flatten()
for i in range(num_iterations):
# 不放回随机抽取5个数据点
indices = np.random.choice(X.index, 5, replace=False)
X_sample = X.loc[indices]
y_sample = y.loc[indices]
model = LinearRegression()
model.fit(X_sample, y_sample)
y_pred = model.predict(X_sample)
mse = mean_squared_error(y_sample, y_pred)
r2 = r2_score(y_sample, y_pred)
print(f"Iteration {i+1}:")
print(f"Coefficients: {model.coef_}")
print(f"Intercept: {model.intercept_}")
print(f"Mean Squared Error: {mse}")
print(f"R^2 Score: {r2}\n")
ax = axes[i]
ax.scatter(y_sample, y_pred, color='blue')
ax.plot([y_sample.min(), y_sample.max()], [y_sample.min(), y_sample.max()], '--r', linewidth=2)
ax.set_xlabel('Actual Area')
ax.set_ylabel('Predicted Area')
ax.set_title(f'Iteration {i+1}')
plt.tight_layout()
plt.show()