Python讀取數(shù)據(jù)集并消除數(shù)據(jù)中的空行方法
更新時(shí)間:2018年07月12日 10:26:15 作者:AlistarHu
今天小編就為大家分享一篇Python讀取數(shù)據(jù)集并消除數(shù)據(jù)中的空行方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
# -*- coding: utf-8 -*-
# @ author hulei 2016-5-3
from numpy import *
import operator
from os import listdir
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# x,y=getDataSet_dz('iris.data.txt',4)
def getDataSet(filename,numberOfFeature): #將數(shù)據(jù)集讀入內(nèi)存
fr = open(filename)
numberOfLines = len(fr.readlines()) #get the number of lines in the file file.readlines()是把文件的全部內(nèi)容讀到內(nèi)存,并解析成一個(gè)list
returnMat = zeros((numberOfLines,numberOfFeature)) #prepare matrix to return 3代表數(shù)據(jù)集中特征數(shù)目###
classLabelVector = [] #prepare labels return
fr = open(filename)
index = 0
for line in fr.readlines():
line = line.strip() #strip() 參數(shù)為空時(shí),默認(rèn)刪除空白符(包括'\n', '\r', '\t', ' ')
listFromLine = line.split(',') #split 以什么為標(biāo)準(zhǔn)分割一次 分成數(shù)組中的每個(gè)元素
returnMat[index,:] = listFromLine[0:numberOfFeature]
#classLabelVector.append(int(listFromLine[-1])) #append() 方法向列表的尾部添加一個(gè)新的元素
if listFromLine[-1] == 'Iris-setosa' :
classLabelVector.append(1)
elif listFromLine[-1] == 'Iris-versicolor' :
classLabelVector.append(2)
else:
#elif listFromLine[-1] == 'Iris-virginica' :
classLabelVector.append(3)
index += 1
return returnMat,classLabelVector
def getDataSet_dz(filename,numberOfFeature): #改進(jìn)版,可以消除數(shù)據(jù)中的空白行
numberOfLines = 0
mx = [] #將數(shù)據(jù)集 去除空行后存入
fr = open(filename)
for line in fr.readlines():
line = line.strip()
if line != '' : #去除空白行
numberOfLines+=1
mx.append( line.split(',') )
returnMat = zeros((numberOfLines,numberOfFeature))
classLabelVector = []
for index in range(numberOfLines) :
returnMat[index,:] = mx[index][0:numberOfFeature]
if mx[index][-1] == 'Iris-setosa' :
classLabelVector.append(1)
elif mx[index][-1] == 'Iris-versicolor' :
classLabelVector.append(2)
else:
#elif listFromLine[-1] == 'Iris-virginica' :
classLabelVector.append(3)
return returnMat,classLabelVector
以上這篇Python讀取數(shù)據(jù)集并消除數(shù)據(jù)中的空行方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python merge、concat合并數(shù)據(jù)集的實(shí)例講解
- python 實(shí)現(xiàn)對數(shù)據(jù)集的歸一化的方法(0-1之間)
- python:pandas合并csv文件的方法(圖書數(shù)據(jù)集成)
- python 劃分?jǐn)?shù)據(jù)集為訓(xùn)練集和測試集的方法
- Python sklearn KFold 生成交叉驗(yàn)證數(shù)據(jù)集的方法
- 對python中數(shù)據(jù)集劃分函數(shù)StratifiedShuffleSplit的使用詳解
- 對python制作自己的數(shù)據(jù)集實(shí)例講解
- Python數(shù)據(jù)集切分實(shí)例
- python 篩選數(shù)據(jù)集中列中value長度大于20的數(shù)據(jù)集方法
- python調(diào)用攝像頭拍攝數(shù)據(jù)集
相關(guān)文章
解決pycharm remote deployment 配置的問題
今天小編就為大家分享一篇解決pycharm remote deployment 配置的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
10個(gè)Python辦公自動(dòng)化案例總結(jié)
Python作為一種簡單而強(qiáng)大的編程語言,不僅在數(shù)據(jù)科學(xué)和軟件開發(fā)領(lǐng)域廣受歡迎,還在辦公自動(dòng)化方面發(fā)揮了巨大作用,通過Python,我們可以編寫腳本來自動(dòng)執(zhí)行各種重復(fù)性任務(wù),從而提高工作效率并減少錯(cuò)誤,在本文中,我們總結(jié)了10個(gè)Python辦公自動(dòng)化案例2024-09-09
python時(shí)間日期操作方法實(shí)例小結(jié)
這篇文章主要介紹了python時(shí)間日期操作方法,結(jié)合實(shí)例形式總結(jié)分析了Python針對日期時(shí)間的轉(zhuǎn)換、計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2020-02-02
如何使用Python優(yōu)雅的合并兩個(gè)字典Dict
字典是Python語言中唯一的映射類型,在我們?nèi)粘9ぷ髦薪?jīng)常會(huì)遇到,下面這篇文章主要給大家介紹了關(guān)于如何使用Python優(yōu)雅的合并兩個(gè)字典Dict的相關(guān)資料,需要的朋友可以參考下2023-05-05
python pandas cumsum求累計(jì)次數(shù)的用法
這篇文章主要介紹了python pandas cumsum求累計(jì)次數(shù)的用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

