随机森林回归代码 python
时间: 2023-11-16 11:01:13 浏览: 50
以下是基于Python的随机森林回归的代码示例:
```python
# 导入必要的库
from sklearn.ensemble import RandomForestRegressor
import numpy as np
# 创建随机森林回归模型
rf = RandomForestRegressor(n_estimators=1000, random_state=42)
# 准备数据
X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
y = np.array([10, 20, 30])
# 训练模型
rf.fit(X, y)
# 预测新数据
new_X = np.array([[2, 3, 4], [5, 6, 7]])
predictions = rf.predict(new_X)
# 输出预测结果
print(predictions)
```
这段代码创建了一个包含1000个决策树的随机森林回归模型,并使用3个特征和1个目标变量训练了模型。然后,使用模型对新数据进行了预测,并输出了预测结果。
相关问题
随机森林回归代码python
随机森林回归是一种常用的机器学习算法,可以用于回归和分类问题。下面是使用Python语言实现随机森林回归的代码:
```python
# 导入必要的库
from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
# 建立回归数据集
X, y = make_regression(n_features=4, n_informative=2, random_state=0, shuffle=False)
# 将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# 建立随机森林回归模型
regr = RandomForestRegressor(max_depth=2, random_state=0)
# 训练模型
regr.fit(X_train, y_train)
# 对测试集进行预测
y_pred = regr.predict(X_test)
```
在这段代码中,我们首先导入了需要用到的库,然后使用`make_regression`函数生成了一个回归数据集。接着使用`train_test_split`函数将数据集分为训练集和测试集。然后建立了随机森林回归模型,并使用`fit`函数训练模型。最后使用`predict`函数对测试集进行预测。
相关问题:
--相关问题--:
高光谱随机森林回归代码python代码
以下是一份高光谱随机森林回归的 Python 代码示例:
```python
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import r2_score, mean_squared_error
# 读取数据
data = pd.read_csv('spectra.csv', header=None)
# 提取 X 和 y
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# 将数据分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 建立随机森林回归模型
rf = RandomForestRegressor(n_estimators=100, random_state=42)
# 模型训练
rf.fit(X_train, y_train)
# 预测测试集结果
y_pred = rf.predict(X_test)
# 计算模型评估指标
r2 = r2_score(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
# 打印结果
print('R2 Score:', r2)
print('MSE:', mse)
```
其中,数据集 `spectra.csv` 包含了高光谱数据,每一行是一个样本,每一列是一个波段的光谱值,最后一列是该样本的目标变量。我们首先读取数据,并将其分为训练集和测试集。然后,我们建立了一个包含 100 个决策树的随机森林回归模型,并用训练集进行训练。接着,我们用测试集进行预测,并计算了模型的 R2 分数和均方误差。最后,我们打印了这些指标。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)