Numpy实现RandomForest(1)

最后

不知道你们用的什么环境,我一般都是用的Python3.6环境和pycharm解释器,没有软件,或者没有资料,没人解答问题,都可以免费领取(包括今天的代码),过几天我还会做个视频教程出来,有需要也可以领取~

给大家准备的学习资料包括但不限于:

Python 环境、pycharm编辑器/永久激活/翻译插件

python 零基础视频教程

Python 界面开发实战教程

Python 爬虫实战教程

Python 数据分析实战教程

python 游戏开发实战教程

Python 电子书100本

Python 学习路线规划

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

from future import division, print_function

import numpy as np

import math

import progressbar

Import helper functions

from mlfromscratch.utils import divide_on_feature, train_test_split, get_random_subsets, normalize

from mlfromscratch.utils import accuracy_score, calculate_entropy

from mlfromscratch.unsupervised_learning import PCA

from mlfromscratch.supervised_learning import ClassificationTree

from mlfromscratch.utils.misc import bar_widgets

from mlfromscratch.utils import Plot

class RandomForest():

“”"Random Forest classifier. Uses a collection of classification trees that

trains on random subsets of the data using a random subsets of the features.

Parameters:


n_estimators: int

The number of classification trees that are used.

max_features: int

The maximum number of features that the classification trees are allowed to

use.

min_samples_split: int

The minimum number of samples needed to make a split when building a tree.

min_gain: float

The minimum impurity required to split the tree further.

max_depth: int

The maximum depth of a tree.

“”"

def init(self, n_estimators=100, max_features=None, min_samples_split=2,

min_gain=0, max_depth=float(“inf”)):

self.n_estimators = n_estimators # Number of trees

self.max_features = max_features # Maxmimum number of features per tree

self.min_samples_split = min_samples_split

self.min_gain = min_gain # Minimum information gain req. to continue

self.max_depth = max_depth # Maximum depth for tree

self.progressbar = progressbar.ProgressBar(widgets=bar_widgets)

Initialize decision trees

self.trees = []

for _ in range(n_estimators):

self.trees.append(

ClassificationTree(

min_samples_split=self.min_samples_split,

min_impurity=min_gain,

max_depth=self.max_depth))

def fit(self, X, y):

n_features = np.shape(X)[1]

If max_features have not been defined => select it as

sqrt(n_features)

if not self.max_features:

self.max_features = int(math.sqrt(n_features))

Choose one random subset of the data for each tree

subsets = get_random_subsets(X, y, self.n_estimators)

for i in self.progressbar(range(self.n_estimators)):

最后

🍅 硬核资料:关注即可领取PPT模板、简历模板、行业经典书籍PDF。
🍅 技术互助:技术群大佬指点迷津,你的问题可能不是问题,求资源在群里喊一声。
🍅 面试题库:由技术群里的小伙伴们共同投稿,热乎的大厂面试真题,持续更新中。
🍅 知识体系:含编程语言、算法、大数据生态圈组件(Mysql、Hive、Spark、Flink)、数据仓库、Python、前端等等。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Extra Trees和Random Forest进行特征选择的Python示例代码。假设我们有一个包含特征和目标变量的数据集,并且我们要选择最重要的特征。 ```python import numpy as np import pandas as pd from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier # 读取数据集 data = pd.read_csv('dataset.csv') X = data.drop(columns=['target']) y = data['target'] # 创建Extra Trees分类器并拟合数据 et_clf = ExtraTreesClassifier() et_clf.fit(X, y) # 创建Random Forest分类器并拟合数据 rf_clf = RandomForestClassifier() rf_clf.fit(X, y) # 输出每个特征的重要性得分 print('Extra Trees feature importance scores:') print(et_clf.feature_importances_) print('Random Forest feature importance scores:') print(rf_clf.feature_importances_) # 选择最重要的特征 et_indices = np.argsort(et_clf.feature_importances_)[::-1] rf_indices = np.argsort(rf_clf.feature_importances_)[::-1] num_features = 10 # 选择前10个特征 et_selected_indices = et_indices[:num_features] rf_selected_indices = rf_indices[:num_features] et_selected_features = X.columns[et_selected_indices] rf_selected_features = X.columns[rf_selected_indices] print('Selected features using Extra Trees:', et_selected_features) print('Selected features using Random Forest:', rf_selected_features) ``` 上述代码将数据集加载到Pandas DataFrame中,然后使用`ExtraTreesClassifier`和`RandomForestClassifier`拟合数据,并输出每个特征的重要性得分。然后,我们选择最重要的前10个特征,并输出它们的名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值