下采样方法

.loc[],中括号里面是先行后列,以逗号分割,行和列分别是行标签和列标签(label)

.iloc[]与loc一样,中括号里面也是先行后列,行列标签用逗号分割,与loc不同的之处是,.iloc 是根据行数与列数来索引的

.ix上面两种用法都可以

 

 

X=data.loc[:,data.columns != 'Class'] #loc 通过行标签索引数据,
y=data.loc[:,data.columns == 'Class'] #取label

#number of data points in the minority class

number_records_fraud=len(data[data.Class==1]) #Class=1的数量
fraud_indices=np.array(data[data.Class==1].index) #取得其索引值

normal_indices=np.array(data[data.Class==0].index) # class为0的数据索引
 

random_normal_indices=np.random.choice(normal_indices,number_records_fraud,replace=False)  # 随机采样,并不对原始dataframe进行替换
random_normal_indices=np.array(random_normal_indices) # 矩阵转换成numpy的array格式
 

under_sample_indices=np.concatenate([fraud_indices,random_normal_indices]) # 合并class=1和class=0中随机选取的数据

under_sample_data = data.iloc[under_sample_indices,:] #定位到真正数据,iloc通过行号索引行数据

X_undersample=under_sample_data.loc[:,under_sample_data.columns!='Class']
y_undersample=under_sample_data.loc[:,under_sample_data.columns=='Class']
print(X_undersample)
print(y_undersample)


print("Percentage of normal transactions: ", len(under_sample_data[under_sample_data.Class == 0])/len(under_sample_data))
print("Percentage of fraud transactions: ", len(under_sample_data[under_sample_data.Class == 1])/len(under_sample_data))
print("Total number of transactions in resampled data: ", len(under_sample_data))

 

 

 

思路:大样本随机取小样本的数量A--》a

a和B再split成train和test


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Matlab中,可以使用下采样方法对图像进行降采样。下采样是指将图像的分辨率降低,减少图像中的像素数量。下采样方法可以通过缩小图像的尺寸来实现。 在给定的代码中,使用了以下方法对图像进行下采样: - p256=photo(1:2:end,1:2:end):将原图像按照2的步长进行采样,得到256x256的图像。 - p128=photo(1:4:end,1:4:end):将原图像按照4的步长进行采样,得到128x128的图像。 - p64=photo(1:8:end,1:8:end):将原图像按照8的步长进行采样,得到64x64的图像。 这些代码将原图像和经过不同采样率处理后的图像显示在同一个figure中,以便进行对比和分析。\[2\] 需要注意的是,下采样会导致图像的细节丢失,因此在选择采样率时需要权衡图像质量和计算效率的因素。 #### 引用[.reference_title] - *1* *2* [图像数字处理——使用matlab对图像进行采样和量化](https://blog.csdn.net/qq_45913371/article/details/120310917)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [图像复原方法(matlab)](https://blog.csdn.net/weixin_56260304/article/details/127461594)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值