写一个光伏预测的python代码
时间: 2023-02-18 16:49:54 浏览: 119
# 基于Python的光伏预测代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据
df = pd.read_csv('data.csv')
# 可视化数据
plt.plot(df['time'], df['power'])
plt.xlabel('时间')
plt.ylabel('功率')
plt.show()
# 将数据分成训练集和测试集
X_train = df['time'][:int(len(df)*0.8)]
y_train = df['power'][:int(len(df)*0.8)]
X_test = df['time'][int(len(df)*0.8):]
y_test = df['power'][int(len(df)*0.8):]
# 使用线性回归模型进行训练
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train.values.reshape(-1,1), y_train)
# 预测
y_pred = model.predict(X_test.values.reshape(-1,1))
# 可视化预测结果
plt.plot(X_test, y_test, label='真实值')
plt.plot(X_test, y_pred, label='预测值')
plt.xlabel('时间')
plt.ylabel('功率')
plt.legend()
plt.show()
阅读全文