国科大数据挖掘实训——线性回归

  • Post author:
  • Post category:其他



  1. import pandas as pd

  2. from sklearn.linear_model import LinearRegression

  3. from sklearn.model_selection import train_test_split

  4. def data_astype(x):

  5. """数据类型转换"""

  6. x['hour'] = x['hour'].astype('float32')

  7. x['DEWP'] = x['DEWP'].astype('float32')

  8. x['TEMP'] = x['TEMP'].astype('float32')

  9. x['PRES'] = x['PRES'].astype('float32')

  10. x['Iws'] = x['Iws'].astype('float32')

  11. x['Is'] = x['Is'].astype('float32')

  12. x['Ir'] = x['Ir'].astype('float32')

  13. x['cbwd_NE'] = x['cbwd_NE'].astype('float32')

  14. x['cbwd_NW'] = x['cbwd_NW'].astype('float32')

  15. x['cbwd_SE'] = x['cbwd_SE'].astype('float32')

  16. x['cbwd_cv'] = x['cbwd_cv'].astype('float32')

  17. x['year'] = x['year'].astype('float32')

  18. x['month'] = x['month'].astype('float32')

  19. x['day'] = x['day'].astype('float32')

  20. x['week'] = x['week'].astype('float32')

  21. if __name__ == "__main__":

  22. # 加载数据

  23. x = pd.read_csv('src/step1/data/pm25_data.csv', encoding='utf-8')

  24. data_astype(x)

  25. # 加载标签

  26. y = pd.read_csv('src/step1/data/result.csv')['pm2.5']

  27. # 对数据进行线性回归预测

  28. #####Begin#####

  29. reg = LinearRegression().fit(x, y)

  30. #####End#####

  31. # 加载未来18天的数据

  32. x_val = pd.read_csv('src/step1/data/test_1.csv')

  33. # 预测

  34. y_val_pre = reg.predict(x_val)

  35. print('未来18天的pm25预测值:', y_val_pre)



版权声明:本文为qq_44398212原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。