随机森林的python代码,不要调sklearn包,不要用类
时间: 2023-07-14 10:12:36 浏览: 178
以下是一个简单的随机森林分类器的 Python 代码,不使用 sklearn 包和类:
```python
import numpy as np
# 随机森林分类器
class RandomForestClassifier:
def __init__(self, max_depth, n_trees, n_features):
self.max_depth = max_depth
self.n_trees = n_trees
self.n_features = n_features
self.trees = []
# 训练随机森林
def fit(self, X, y):
for i in range(self.n_trees):
# 随机选择特征
features = np.random.choice(X.shape[1], self.n_features, replace=False)
# 以 features 为特征集训练决策树
tree = DecisionTreeClassifier(self.max_depth)
tree.fit(X[:, features], y)
self.trees.append((tree, features))
# 预测
def predict(self, X):
y_pred = []
for tree, features in self.trees:
# 使用每个决策树预测结果并汇总
y_pred.append(tree.predict(X[:, features]))
# 返回多数投票结果
return np.round(np.mean(y_pred, axis=0))
# 决策树分类器
class DecisionTreeClassifier:
def __init__(self, max_depth):
self.max_depth = max_depth
# 训练决策树
def fit(self, X, y):
self.tree = self.build_tree(X, y, 0)
# 构建决策树
def build_tree(self, X, y, depth):
n_samples, n_features = X.shape
# 如果深度达到最大或样本全部属于同一类别,则返回叶子节点
if depth >= self.max_depth or np.unique(y).size == 1:
return LeafNode(y)
# 随机选择特征
feature = np.random.randint(n_features)
# 随机选择分割点
threshold = np.random.uniform(X[:, feature].min(), X[:, feature].max())
# 分割数据集
left_idxs = X[:, feature] < threshold
right_idxs = X[:, feature] >= threshold
# 如果分割后的数据不合法,则返回叶子节点
if np.sum(left_idxs) == 0 or np.sum(right_idxs) == 0:
return LeafNode(y)
# 递归构建左右子树
left_tree = self.build_tree(X[left_idxs], y[left_idxs], depth+1)
right_tree = self.build_tree(X[right_idxs], y[right_idxs], depth+1)
# 返回决策节点
return DecisionNode(feature, threshold, left_tree, right_tree)
# 预测
def predict(self, X):
return np.array([self.tree.predict(x) for x in X])
# 叶子节点
class LeafNode:
def __init__(self, y):
self.y = y
def predict(self, x):
return np.mean(self.y)
# 决策节点
class DecisionNode:
def __init__(self, feature, threshold, left, right):
self.feature = feature
self.threshold = threshold
self.left = left
self.right = right
def predict(self, x):
if x[self.feature] < self.threshold:
return self.left.predict(x)
else:
return self.right.predict(x)
```
使用示例:
```python
# 生成随机数据
X = np.random.rand(100, 10)
y = np.random.randint(0, 2, 100)
# 训练随机森林
rf = RandomForestClassifier(max_depth=5, n_trees=10, n_features=3)
rf.fit(X, y)
# 预测
y_pred = rf.predict(X)
```
阅读全文