需要重采样的数据文件(Libsvm format),如heart_scale

+1 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.419847 9:-1 10:-0.225806 12:1 13:-1
-1 1:0.583333 2:-1 3:0.333333 4:-0.603774 5:1 6:-1 7:1 8:0.358779 9:-1 10:-0.483871 12:-1 13:1
....

重采样后的数据保存文件(Libsvm format),这里heart_scale_balance.txt

Python code:

from sklearn.datasets import load_svmlight_file
from sklearn.datasets import dump_svmlight_file
import numpy as np
from sklearn.utils import check_random_state
from scipy.sparse import hstack,vstack def fit_sample(X, y):
"""Resample the dataset.
"""
label = np.unique(y)
stats_c_ = {}
maj_n = 0
for i in label:
nk = sum(y==i)
stats_c_[i] = nk
if nk > maj_n:
maj_n = nk
maj_c_ = i # Keep the samples from the majority class
X_resampled = X[y == maj_c_]
y_resampled = y[y == maj_c_]
# Loop over the other classes over picking at random
for key in stats_c_.keys(): # If this is the majority class, skip it
if key == maj_c_:
continue # Define the number of sample to create
num_samples = int(stats_c_[maj_c_] -stats_c_[key]) # Pick some elements at random
random_state = check_random_state(42)
indx = random_state.randint(low=0, high=stats_c_[key],size=num_samples) # Concatenate to the majority class
X_resampled = vstack([X_resampled,X[y == key],X[y == key][indx]])
print np.shape(y_resampled),np.shape(y[y == key]),np.shape(y[y == key][indx])
y_resampled = list(y_resampled)+list(y[y == key])+list(y[y == key][indx])
return X_resampled, y_resampled X_train, y_train = load_svmlight_file("heart_scale") # Apply the random over-sampling
X_train, y_train = fit_sample(X_train,y_train)
dump_svmlight_file(X_train, y_train,'heart_scale_balance.txt',zero_based=False)

Python 对不均衡数据进行Over sample(重抽样)的更多相关文章

  1. JavaScript 解析 Django Python 生成的 datetime 数据 时区问题解决

    JavaScript 解析 Django/Python 生成的 datetime 数据 当Web后台使用Django时,后台生成的时间数据类型就是Python类型的. 项目需要将几个时间存储到数据库中 ...

  2. Python下载Yahoo!Finance数据

    Python下载Yahoo!Finance数据的三种工具: (1)yahoo-finance package. (2)ystockquote. (3)pandas.

  3. Delphi中使用python脚本读取Excel数据

    Delphi中使用python脚本读取Excel数据2007-10-18 17:28:22标签:Delphi Excel python原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 . ...

  4. python打印表格式数据,留出正确的空格和段落星号或注释

    python打印表格式数据,留出正确的空格,格式化打出 代码如下: def printPicnic(itemsDict,leftWidth,rightWidth): print('PICNIC ITE ...

  5. python查询数据库返回数据

    python查询数据库返回数据主要运用到flask框架,pymysql 和 json‘插件’ #!/usr/bin/python # -*- coding: UTF-8 -*- import pymy ...

  6. 用python在后端将数据写入到数据库并读取

    用python在后端将数据写入到数据库: # coding:utf- import pandas as pd from sqlalchemy import create_engine # 初始化数据库 ...

  7. Python 进程之间共享数据

    最近遇到多进程共享数据的问题,到网上查了有几篇博客写的蛮好的,记录下来方便以后查看. 一.Python multiprocessing 跨进程对象共享  在mp库当中,跨进程对象共享有三种方式,第一种 ...

  8. Python使用plotly绘制数据图表的方法

    转载:http://www.jb51.net/article/118936.htm 本篇文章主要介绍了Python使用plotly绘制数据图表的方法,实例分析了plotly绘制的技巧. 导语:使用 p ...

  9. python中json格式数据输出实现方式

    python中json格式数据输出实现方式 主要使用json模块,直接导入import json即可. 小例子如下: #coding=UTF-8 import json info={} info[&q ...

随机推荐

  1. 《编写可维护的JavaScript》——JavaScript编码规范(二)

    昨天是我偶像生日,现在整个人都还好兴奋啊O(∩_∩)O~  闲话少说,让我先发篇随笔留念一下^_^ ////////////////////////////////正文分割线///////////// ...

  2. 在webapp上使用input:file, 指定capture属性调用默许相机,摄像,录音功能

    ## 在webapp上使用input:file, 指定capture属性调用默认相机,摄像,录音功能 在iOS6下开发webapp,使用inputz之file,很有用 <input type=& ...

  3. er

    电子锁管理 设备管理 设备管理 信息编辑;回收 电子锁发放 电子锁初始化,发放 记录车辆在发车时使用的电子锁 电子锁开锁联系人管理 电子锁开锁联系人管理 根据订单路线中的投点,设置每个投递点的开锁联系 ...

  4. 理解LSTM网络

    转载自:http://blog.csdn.net/ycheng_sjtu/article/details/48792467

  5. tinkphp验证码的使用

    页面显示验证码: <div class="input-group has-feedback "> <input id="yzm" type=& ...

  6. MySQL导入SQL文件及常用命令

      在MySQL Qurey   Brower中直接导入*.sql脚本,是不能一次执行多条sql命令的,在mysql中执行sql文件的命令: mysql> source   d:/myprogr ...

  7. Command: sl (Steam Locomotive)

    You might be aware of command 'ls' the list command and use it frequently to view the contents of a ...

  8. JAVA窗口程序实例一

    package 甲; import java.awt.Dimension; import java.text.SimpleDateFormat; import java.util.Calendar; ...

  9. 【分享】图解Windows Server 2012 R2 配置IIS 8全过程

    最近计划更换服务器,包括IIS服务器与数据库服务器,操作系统都是Windows Server 2012 R2,第一次接触Windows Server 2012,感觉比较新鲜,一路摸索完成了IIS 8 ...

  10. C# java MD5加密方不一致问题

    说来惭愧,做开发几年了,一直在吸取,今天也写写自已关于技术的一点点理解,不正之处,请大家多多指点. 由于之前开发的项目使用的是C#,用户信息使用的C#的MD5加密码方式,而现在需要切换到Java平台下 ...