python权重分类数组_从python中的权重数组中获取随机索引的快速方法

我经常发现自己处于需要对数组或列表进行随机索引的位置,其中索引的概率不是均匀分布的,而是根据某些正权重.什么是快速获得它们的方法?我知道我可以将权重传递给numpy.random.choice作为可选参数p,但是函数似乎很慢,并且构建一个传递它的范围也不理想.权重之和可以是任意正数,并且不保证为1,这使得该方法在(0,1)中生成随机数,然后减去权重条目,直到结果为0或更不可能.

虽然有关于如何以简单的方式实现类似的东西(主要不是获取数组索引,但相应的元素)的答案,例如Weighted choice short and simple,我正在寻找一个快速的解决方案,因为适当的函数经常执行.我的权重经常变化,因此构建类似别名掩码的开销(详细介绍可以在http://www.keithschwarz.com/darts-dice-coins/中找到)应该被视为计算时间的一部分.

解决方法:

累积求和和二等分

在任何一般情况下,似乎建议计算权重的累积和,并使用bisect模块中的bisect在结果排序数组中查找随机点

def weighted_choice(weights):

cs = numpy.cumsum(weights)

return bisect.bisect(cs, numpy.random.random() * cs[-1])

如果速度是一个问题.下面给出更详细的分析.

注意:如果数组不是平的,可以使用numpy.unravel_index将平面索引转换为形状索引,如https://stackoverflow.com/a/19760118/1274613所示

实验分析

使用numpy内置函数有四种或多或少的明显解决方案.使用timeit比较所有这些结果会得到以下结果:

import timeit

weighted_choice_functions = [

"""import numpy

wc = lambda weights: numpy.random.choice(

range(len(weights)),

p=weights/weights.sum())

""",

"""import numpy

# Adapted from https://stackoverflow.com/a/19760118/1274613

def wc(weights):

cs = numpy.cumsum(weights)

return cs.searchsorted(numpy.random.random() * cs[-1], 'right')

""",

"""import numpy, bisect

# Using bisect mentioned in https://stackoverflow.com/a/13052108/1274613

def wc(weights):

cs = numpy.cumsum(weights)

return bisect.bisect(cs, numpy.random.random() * cs[-1])

""",

"""import numpy

wc = lambda weights: numpy.random.multinomial(

1,

weights/weights.sum()).argmax()

"""]

for setup in weighted_choice_functions:

for ps in ["numpy.ones(40)",

"numpy.arange(10)",

"numpy.arange(200)",

"numpy.arange(199,-1,-1)",

"numpy.arange(4000)"]:

timeit.timeit("wc(%s)"%ps, setup=setup)

print()

结果输出是

178.45797914802097

161.72161589498864

223.53492237901082

224.80936180002755

1901.6298267539823

15.197789980040397

19.985687876993325

20.795070077001583

20.919113760988694

41.6509403079981

14.240949985047337

17.335801470966544

19.433710905024782

19.52205040602712

35.60536142199999

26.6195822560112

20.501282756973524

31.271995796996634

27.20013752405066

243.09768892999273

这意味着numpy.random.choice非常慢,甚至专用的numpy searchsorted方法也比类型天真的bisect变种慢. (这些结果是使用Python 3.3.5和numpy 1.8.1获得的,因此对于其他版本可能会有所不同.)基于numpy.random.multinomial的函数对于大权重的效率低于基于累积求和的方法.据推测,argmax必须迭代整个阵列并进行比较,每个步骤都起着重要作用,从增加和减少重量列表之间的四秒差异可以看出.

标签:python,algorithm,random

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
随机森林分类,如果想将某个因子的权重加大,可以通过修改训练数据的权重来实现。具体实现方法如下: 1. 首先,导入需要的库和数据: ```python import numpy as np import pandas as pd from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # 读取数据 data = pd.read_csv('data.csv') 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=1) ``` 2. 接下来,定义一个函数来修改数据权重: ```python def modify_weight(X_train, y_train, target_feature, weight): """ X_train: 训练数据集 y_train: 训练数据标签 target_feature: 目标特征列 weight: 目标特征的加权因子 """ # 获取目标特征的列索引 target_feature_index = np.argwhere(data.columns == target_feature)[0][0] # 将目标特征等于1的样本的权重乘以weight for i in range(len(y_train)): if y_train[i] == 1 and X_train[i, target_feature_index] == 1: weight[i] *= weight return weight ``` 3. 接下来,使用修改后的数据权重来训练随机森林模型: ```python # 初始化数据权重 weight = np.ones(len(y_train)) # 将特定的特征加权 target_feature = 'feature_name' weight = modify_weight(X_train, y_train, target_feature, weight) # 训练随机森林模型 rfc = RandomForestClassifier(n_estimators=100, random_state=1) rfc.fit(X_train, y_train, sample_weight=weight) ``` 4. 最后,使用训练好的模型对测试集进行预测,并计算准确率: ```python # 对测试集进行预测 y_pred = rfc.predict(X_test) # 计算准确率 accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy) ``` 需要注意的是,加权因子的大小需要根据实际情况进行调整,以达到最佳的分类效果。另外,在实际应用,也可以使用网格搜索等方法来确定最佳的加权因子。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值