python使用pandas抽樣訓(xùn)練數(shù)據(jù)中某個(gè)類別實(shí)例
廢話真的一句也不想多說,直接看代碼吧!
# -*- 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
補(bǔ)充拓展:pandas實(shí)現(xiàn)對(duì)dataframe抽樣
隨機(jī)抽樣
import pandas as pd #對(duì)dataframe隨機(jī)抽取2000個(gè)樣本 pd.sample(df, n=2000)
分層抽樣
利用sklean中的函數(shù)靈活進(jìn)行抽樣
from sklearn.model_selection import train_test_split #y是在X中的某一個(gè)屬性列 X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, stratify=y)
以上這篇python使用pandas抽樣訓(xùn)練數(shù)據(jù)中某個(gè)類別實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)合并兩個(gè)有序列表的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)合并兩個(gè)有序列表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
opencv python簡(jiǎn)易文檔之圖像處理算法
OpenCV是一個(gè)開源庫,包含了許多計(jì)算機(jī)視覺算法,它在計(jì)算機(jī)視覺和圖像處理中起著重要作用,用于實(shí)時(shí)操作,其效率足以滿足工業(yè)上的要求,這篇文章主要給大家介紹了關(guān)于opencv python簡(jiǎn)易文檔之圖像處理算法的相關(guān)資料,需要的朋友可以參考下2021-08-08
Django-Xadmin后臺(tái)首頁添加小組件報(bào)錯(cuò)的解決方案
這篇文章主要介紹了Django-Xadmin后臺(tái)首頁添加小組件報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
python實(shí)現(xiàn)將html表格轉(zhuǎn)換成CSV文件的方法
這篇文章主要介紹了python實(shí)現(xiàn)將html表格轉(zhuǎn)換成CSV文件的方法,涉及Python操作csv文件的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Pandas自定義選項(xiàng)option設(shè)置
pandas有一個(gè)option系統(tǒng)可以控制pandas的展示情況,一般來說我們不需要進(jìn)行修改,但是不排除特殊情況下的修改需求。本文將會(huì)詳細(xì)講解pandas中的option設(shè)置,感興趣的可以了解下2021-07-07
Python實(shí)現(xiàn)接受任意個(gè)數(shù)參數(shù)的函數(shù)方法
下面小編就為大家分享一篇Python實(shí)現(xiàn)接受任意個(gè)數(shù)參數(shù)的函數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
使用python數(shù)據(jù)清洗代碼實(shí)例
這篇文章主要介紹了使用python數(shù)據(jù)清洗代碼實(shí)例,分享一下近期用python做數(shù)據(jù)清洗匯總的相關(guān)代碼,這里我們用到的python包有pandas、numpy、os等,需要的朋友可以參考下2023-07-07

