写一篇基于随机森林模型的沪深300指数价格预测研究的python代码
时间: 2024-11-03 22:22:40 浏览: 9
基于python随机森林模型的锂离子电池剩余寿命预测
在Python中,我们可以使用scikit-learn库中的RandomForestRegressor来进行沪深300指数价格的预测。这里是一个基础示例,假设我们已经有了历史数据作为特征集(X)和收盘价作为目标变量(y):
```python
# 导入所需库
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import datetime
# 加载数据(假设数据存储在CSV文件中)
data = pd.read_csv('stock_data.csv') # 请替换为实际数据文件路径
# 数据预处理
data['Date'] = pd.to_datetime(data['Date']) # 将日期列转换为datetime类型
data.set_index('Date', inplace=True) # 设置日期为索引
data.sort_index(inplace=True)
# 特征选择和数据切分
features = ['feature1', 'feature2', ...] # 根据实际情况填写特征列名
X = data[features]
y = data['Close']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 特征缩放
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# 创建并训练随机森林模型
rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
rf_model.fit(X_train_scaled, y_train)
# 预测
predictions = rf_model.predict(X_test_scaled)
# 打印预测结果
print("Predicted closing prices for the test set:")
print(predictions)
# 评估性能
from sklearn.metrics import mean_squared_error, r2_score
mse = mean_squared_error(y_test, predictions)
r2 = r2_score(y_test, predictions)
print(f"Mean Squared Error: {mse}, R-squared: {r2}")
#
阅读全文