python使用pandas抽样训练数据中某个类别实例

所属分类: 脚本专栏 / python 阅读数: 1791
收藏 0 赞 0 分享

废话真的一句也不想多说,直接看代码吧!

# -*- coding: utf-8 -*- 
 
import numpy 
from sklearn import metrics 
from sklearn.svm import LinearSVC 
from sklearn.naive_bayes import MultinomialNB 
from sklearn import linear_model 
from sklearn.datasets import load_iris 
from sklearn.cross_validation import train_test_split 
from sklearn.preprocessing import OneHotEncoder, StandardScaler 
from sklearn import cross_validation 
from sklearn import preprocessing 
import scipy as sp
from sklearn.linear_model import LogisticRegression
from sklearn.feature_selection import SelectKBest ,chi2
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
#import iris_data 
 
'''
creativeID,userID,positionID,clickTime,conversionTime,connectionType,
telecomsOperator,appPlatform,sitesetID,positionType,age,gender,
education,marriageStatus,haveBaby,hometown,residence,appID,appCategory,label
'''
 
 
def test():
 df = pd.read_table("/var/lib/mysql-files/data1.csv", sep=",")
 df1 = df[["connectionType","telecomsOperator","appPlatform","sitesetID",
    "positionType","age","gender","education","marriageStatus",
    "haveBaby","hometown","residence","appCategory","label"]]
 print df1["label"].value_counts()
 N_data = df1[df1["label"]==0]
 P_data = df1[df1["label"]==1]
 N_data = N_data.sample(n=P_data.shape[0], frac=None, replace=False, weights=None, random_state=2, axis=0)
 #print df1.loc[:,"label"]==0
 print P_data.shape
 print N_data.shape
 
 data = pd.concat([N_data,P_data])
 print data.shape
 data = data.sample(frac=1).reset_index(drop=True) 
 print data[["label"]]
 return

补充拓展:pandas实现对dataframe抽样

随机抽样

import pandas as pd
#对dataframe随机抽取2000个样本
pd.sample(df, n=2000)

分层抽样

利用sklean中的函数灵活进行抽样

from sklearn.model_selection import train_test_split
#y是在X中的某一个属性列
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, stratify=y)

以上这篇python使用pandas抽样训练数据中某个类别实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Python实现图像几何变换

这篇文章主要介绍了Python实现图像几何变换的方法,实例分析了Python基于Image模块实现图像翻转、旋转、改变大小等操作的相关技巧,非常简单实用,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中的urllib模块使用详解

这篇文章主要介绍了Python中的urllib模块使用详解,是Python入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

Python的多态性实例分析

这篇文章主要介绍了Python的多态性,以实例形式深入浅出的分析了Python在面向对象编程中多态性的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

python生成IP段的方法

这篇文章主要介绍了python生成IP段的方法,涉及Python文件读写及随机数操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python操作redis的方法

这篇文章主要介绍了python操作redis的方法,包括Python针对redis的连接、设置、获取、删除等常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python妹子图简单爬虫实例

这篇文章主要介绍了python妹子图简单爬虫,实例分析了Python爬虫程序所涉及的页面源码获取、进度显示、正则匹配等技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

分析用Python脚本关闭文件操作的机制

这篇文章主要介绍了分析用Python脚本关闭文件操作的机制,作者分Python2.x版本和3.x版本两种情况进行了阐述,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现搜索指定目录下文件及文件内搜索指定关键词的方法

这篇文章主要介绍了python实现搜索指定目录下文件及文件内搜索指定关键词的方法,可实现针对文件夹及文件内关键词的搜索功能,需要的朋友可以参考下
收藏 0 赞 0 分享

python中getaddrinfo()基本用法实例分析

这篇文章主要介绍了python中getaddrinfo()基本用法,实例分析了Python中使用getaddrinfo方法进行IP地址解析的基本技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python查找指定具有相同内容文件的方法

这篇文章主要介绍了python查找指定具有相同内容文件的方法,涉及Python针对文件操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多