Python讀取數(shù)據(jù)集并消除數(shù)據(jù)中的空行方法
如下所示:
# -*- 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()是把文件的全部?jī)?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ù)中的空行方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python merge、concat合并數(shù)據(jù)集的實(shí)例講解
- python 實(shí)現(xiàn)對(duì)數(shù)據(jù)集的歸一化的方法(0-1之間)
- python:pandas合并csv文件的方法(圖書(shū)數(shù)據(jù)集成)
- python 劃分?jǐn)?shù)據(jù)集為訓(xùn)練集和測(cè)試集的方法
- Python sklearn KFold 生成交叉驗(yàn)證數(shù)據(jù)集的方法
- 對(duì)python中數(shù)據(jù)集劃分函數(shù)StratifiedShuffleSplit的使用詳解
- 對(duì)python制作自己的數(shù)據(jù)集實(shí)例講解
- Python數(shù)據(jù)集切分實(shí)例
- python 篩選數(shù)據(jù)集中列中value長(zhǎng)度大于20的數(shù)據(jù)集方法
- python調(diào)用攝像頭拍攝數(shù)據(jù)集
相關(guān)文章
簡(jiǎn)述:我為什么選擇Python而不是Matlab和R語(yǔ)言
這篇文章主要介紹了簡(jiǎn)述:我為什么選擇Python而不是Matlab和R語(yǔ)言,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11一文教會(huì)你用nginx+uwsgi部署自己的django項(xiàng)目
uWSGI是一個(gè)Web服務(wù)器,它實(shí)現(xiàn)了WSGI協(xié)議、uwsgi、http等協(xié)議,下面這篇文章主要給大家介紹了關(guān)于用nginx+uwsgi部署自己的django項(xiàng)目的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Python利用AutoGrad實(shí)現(xiàn)自動(dòng)計(jì)算函數(shù)斜率和梯度
AutoGrad 是一個(gè)老少皆宜的 Python 梯度計(jì)算模塊。對(duì)于大學(xué)生、機(jī)器學(xué)習(xí)愛(ài)好者而言,你只需要傳遞給它Numpy這樣的標(biāo)準(zhǔn)數(shù)據(jù)庫(kù)下編寫(xiě)的損失函數(shù),它就可以自動(dòng)計(jì)算損失函數(shù)的導(dǎo)數(shù)(梯度)。本文將從普通斜率計(jì)算開(kāi)始,介紹到如何只使用它來(lái)實(shí)現(xiàn)一個(gè)邏輯回歸模型2022-07-07python3.7環(huán)境下安裝Anaconda的教程圖解
這篇文章主要介紹了python3.7環(huán)境下安裝Anaconda的教程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09python命令行執(zhí)行腳本找不到模塊ModuleNotFoundError問(wèn)題
這篇文章主要介紹了python命令行執(zhí)行腳本找不到模塊ModuleNotFoundError問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06openCV實(shí)踐項(xiàng)目之銀行卡卡號(hào)識(shí)別功能
最近在惡補(bǔ)opencv,在前期不太那么認(rèn)真的學(xué)習(xí)狀態(tài)下,著手搞了一下這個(gè)小項(xiàng)目實(shí)戰(zhàn),基于模板匹配下的銀行卡卡號(hào)識(shí)別,下面這篇文章主要給大家介紹了關(guān)于openCV實(shí)踐項(xiàng)目之銀行卡卡號(hào)識(shí)別功能的相關(guān)資料,需要的朋友可以參考下2022-11-11Python搭建自己IP代理池的方法實(shí)現(xiàn)
本文主要介紹了Python搭建自己IP代理池的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python django框架 web端視頻加密的實(shí)例詳解
這篇文章主要介紹了Python django框架 web端視頻加密,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11