fork download
  1. import numpy as np
  2. import math
  3.  
  4. print("=" * 60)
  5. print("🚀 Ideone.com 线性回归实验")
  6. print("=" * 60)
  7.  
  8. # 设置随机种子保证结果可重现
  9. np.random.seed(42)
  10.  
  11. # 生成模拟数据:城市人口(万)与利润(万美元)
  12. population = np.random.normal(8, 2.5, 50)
  13. profit = 2.5 * population + 1.0 + np.random.normal(0, 1.5, 50)
  14.  
  15. print("📊 生成数据统计信息:")
  16. print(f"样本数量: {len(population)}")
  17. print(f"人口 - 均值: {np.mean(population):.2f}万, 标准差: {np.std(population):.2f}")
  18. print(f"利润 - 均值: {np.mean(profit):.2f}万美元, 标准差: {np.std(profit):.2f}")
  19. print(f"人口与利润相关系数: {np.corrcoef(population, profit)[0,1]:.3f}")
  20.  
  21. print("\n📈 数据前10行:")
  22. print("人口(万)\t利润(万美元)")
  23. print("-" * 30)
  24. for i in range(min(10, len(population))):
  25. print(f"{population[i]:6.2f}\t{profit[i]:8.2f}")
Success #stdin #stdout 0.78s 41336KB
stdin
Standard input is empty
stdout
============================================================
🚀 Ideone.com 线性回归实验
============================================================
📊 生成数据统计信息:
样本数量: 50
人口 - 均值: 7.44万, 标准差: 2.31
利润 - 均值: 19.62万美元, 标准差: 6.06
人口与利润相关系数: 0.977

📈 数据前10行:
人口(万)	利润(万美元)
------------------------------
  9.24	   24.59
  7.65	   19.56
  9.62	   24.03
 11.81	   31.44
  7.41	   21.08
  7.41	   20.93
 11.95	   29.61
  9.92	   25.33
  6.83	   18.56
  9.36	   25.85