Python随机森林回归自编实现
时间: 2023-11-29 13:45:58 浏览: 42
以下是Python随机森林回归的自编实现:
```python
import numpy as np
class RandomForestRegressor:
def __init__(self, n_estimators=100, max_depth=None, min_samples_split=2):
self.n_estimators = n_estimators
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.trees = []
def fit(self, X, y):
for i in range(self.n_estimators):
tree = DecisionTreeRegressor(max_depth=self.max_depth, min_samples_split=self.min_samples_split)
bootstrap_indices = np.random.choice(len(X), size=len(X), replace=True)
bootstrap_X = X[bootstrap_indices]
bootstrap_y = y[bootstrap_indices]
tree.fit(bootstrap_X, bootstrap_y)
self.trees.append(tree)
def predict(self, X):
predictions = np.zeros(len(X))
for tree in self.trees:
predictions += tree.predict(X)
return predictions / len(self.trees)
class DecisionTreeRegressor:
def __init__(self, max_depth=None, min_samples_split=2):
self.max_depth = max_depth
self.min_samples_split = min_samples_split
def fit(self, X, y):
self.tree = self._build_tree(X, y)
def predict(self, X):
return np.array([self._traverse_tree(x, self.tree) for x in X])
def _build_tree(self, X, y, depth=0):
n_samples, n_features = X.shape
variance = np.var(y)
best_split = {
'feature_index': None,
'threshold': None,
'left_X': None,
'left_y': None,
'right_X': None,
'right_y': None,
'mse': float('inf')
}
if variance == 0 or depth == self.max_depth or n_samples < self.min_samples_split:
return np.mean(y)
for feature_index in range(n_features):
thresholds = np.unique(X[:, feature_index])
for threshold in thresholds:
left_indices = X[:, feature_index] <= threshold
right_indices = X[:, feature_index] > threshold
if len(left_indices) == 0 or len(right_indices) == 0:
continue
left_X, left_y = X[left_indices], y[left_indices]
right_X, right_y = X[right_indices], y[right_indices]
mse = (len(left_y) / n_samples) * np.var(left_y) + (len(right_y) / n_samples) * np.var(right_y)
if mse < best_split['mse']:
best_split = {
'feature_index': feature_index,
'threshold': threshold,
'left_X': left_X,
'left_y': left_y,
'right_X': right_X,
'right_y': right_y,
'mse': mse
}
if best_split['feature_index'] is None:
return np.mean(y)
left_subtree = self._build_tree(best_split['left_X'], best_split['left_y'], depth+1)
right_subtree = self._build_tree(best_split['right_X'], best_split['right_y'], depth+1)
return {
'feature_index': best_split['feature_index'],
'threshold': best_split['threshold'],
'left': left_subtree,
'right': right_subtree
}
def _traverse_tree(self, x, node):
if isinstance(node, float):
return node
if x[node['feature_index']] <= node['threshold']:
return self._traverse_tree(x, node['left'])
else:
return self._traverse_tree(x, node['right'])
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)