需要重采样的数据文件(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. java equals和==的区别

    大概说equals和==都比较的是什么: 1. boolean tem = a == b; 首先==比较的肯定是地址,从堆栈的角度说也就是说==比较的是栈上面的内容.因为栈是用来存放地址或是java中 ...

  2. LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16 Debug/firstapi.exe :

    #include <windows.h> #include <iostream.h> void main() { char SourceFileName[MAX_PATH]; ...

  3. cmd 下切换目录

    隔了段时间没用cmd 工作台,发现不会切换目录了,感觉特sb,为避免再次出现sb情况,记下来在说 1.切换磁盘(磁盘字母) d: 2.切换到指定目录 cd d:\www 在cmd下执行php文件 c: ...

  4. C# 调用Excel 出现服务器出现意外情况. (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT)

    C# 调用Excel 出现服务器出现意外情况. (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT) private Microsoft.Office.Intero ...

  5. Android AsyncTask分析

    ---恢复内容开始--- 因为android的UI线程是不安全的,如果你UI线程里执行一些耗时任务,很容易就导致程序崩溃.并且目前网络任务也不能够在UI线程里执行.处理这些问题除了直接写一个线程以外, ...

  6. JS跨域

    //2011-7-25 (function(){ //闭包 function load_script(xyUrl, callback){ var head = document.getElements ...

  7. wkhtmltopdf 安装使用笔记(CentOS6)

    1. 在官网下载安装文件. http://wkhtmltopdf.org/ 安装时如果提示某些库找不到的话,使用yum安装即可. 2. 命令行测试 $ wkhtmltopdf http://news. ...

  8. Sprite(精灵)&& 三个特殊的层Layer

    用来作为以后复习使用. 1 #include "ScenceScend.h" CCScene* ScenceScend::scene() { CCScene* s = CCScen ...

  9. mate 标签的 http-equiv

    http-equiv 1. 页面的描述 <meta http-equiv="description" content="This is my page"& ...

  10. office2003?2007共存?版本各自打开的解决方案

    在现在的办公软件中, Microsoft出品的 Office集成办公软件占据了绝大多数的市场份额,从最初的 Office 2000,到后面的 Office 2003以至近两年刚发行的 Office 2 ...