python - 我如何从头开始编写代码以按目标变量进行分层抽样?
问题描述
所有,我正在尝试从头开始创建(不使用 sklearn 库)来创建 5 个样本(df / 5 的 len),以便每个样本具有与原始数据集相同比例的目标变量(1)。例如,原始有 5% 的癌症患者我希望我的 5 个样本中的每一个也有 5% 的目标变量。不确定如何执行此操作,
df_list=[]
n= round(len(df)/5)
for m in range(1,6):
m = m*n
print(df[:m])
df_list.append(df[:m])
这会创建我想要的每个块,但是我现在怎么做才能使目标变量与原始变量的百分比相同?
解决方案
解决方案:
import numpy as np
import math
def stratify(data, target='y', n=10):
array = data.values
y = data[target].values
unique, counts = np.unique(data[target].values, return_counts=True)
new_counts = counts * (n/sum(counts))
new_counts = fit_new_counts_to_n(new_counts, n)
selected_count = np.zeros(len(unique))
selected_row_indices = []
for i in range(array.shape[0]):
if sum(selected_count) == sum(new_counts):
break
cr_target_value = y[i]
cr_target_index = np.where(unique==cr_target_value)[0][0]
if selected_count[cr_target_index] < new_counts[cr_target_index]:
selected_row_indices.append(i)
selected_count[cr_target_index] += 1
row_indices_mask = np.array([x in selected_row_indices for x in np.arange(array.shape[0])])
return pd.DataFrame(array[row_indices_mask], columns=data.columns)
实用类:
def fit_new_counts_to_n(new_counts, n):
decimals = [math.modf(x)[0] for x in new_counts]
integers = [int(math.modf(x)[1]) for x in new_counts]
arg_max = np.array(map(np.argmax, decimals))
sorting_indices = np.argsort(decimals)[::-1][:n]
for i in sorting_indices:
if sum(integers) < n:
integers[i] += 1
else:
break
return integers
示例用法:
data = [[ 3, 0],
[ 54, 3],
[ 3, 1],
[ 64, 1],
[ 65, 0],
[ 34, 1],
[ 45, 2],
[534, 2],
[ 57, 1],
[ 64, 3],
[ 5, 1],
[ 45, 1],
[546, 1],
[ 4, 2],
[ 53, 3],
[345, 2],
[456, 2],
[435, 3],
[545, 1],
[ 45, 3]]
data = pd.DataFrame(data, columns=['X1', 'y'])
stratified_data = stratify(data, target='y', n=10)
结果:
[[ 3, 0],
[ 54, 3],
[ 3, 1],
[ 64, 1],
[ 34, 1],
[ 45, 2],
[534, 2],
[ 57, 1],
[ 64, 3],
[ 53, 3]]
推荐阅读
- sql - Postgresql 中值组合的唯一约束
- amazon-web-services - 防止对象更新的 AWS S3 存储桶策略
- reactjs - 在 React 中有条件地添加函数作为道具?
- java - 使用视图绑定膨胀类 ImageView 时出错
- javascript - 如何关闭反应模式窗口
- angular - 从 Angular Pipe 返回一个组件
- reporting-services - SSRS 报告:单击空日期/时间参数时出现不相关的值列表
- telegram - Telegram Bot 正在获取所有频道消息
- json - JQ - 使用带有空格的 bash 数组创建 JSON 数组
- react-native - 更新数组中的整个项目 - redux