没有合适的资源?快使用搜索试试~ 我知道了~
首页Python读取数据集并消除数据中的空行方法
如下所示: # -*- 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): #将数据集读入内存 fr = open(filename) numberOfLines =
资源详情
资源评论
资源推荐
Python读取数据集并消除数据中的空行方法读取数据集并消除数据中的空行方法
如下所示:如下所示:
# -*- 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): #将数据集读入内存
fr = open(filename)
numberOfLines = len(fr.readlines()) #get the number of lines in the file file.readlines()是把文件的全部内容读到内存,并解析
成一个list
returnMat = zeros((numberOfLines,numberOfFeature)) #prepare matrix to return 3代表数据集中特征数目###
classLabelVector = [] #prepare labels return
fr = open(filename)
index = 0
for line in fr.readlines():
line = line.strip() #strip() 参数为空时,默认删除空白符(包括'', '', ' ', ' ')
listFromLine = line.split(',') #split 以什么为标准分割一次 分成数组中的每个元素
returnMat[index,:] = listFromLine[0:numberOfFeature] #classLabelVector.append(int(listFromLine[-1])) #append() 方法向列表
的尾部添加一个新的元素
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): #改进版,可以消除数据中的空白行
numberOfLines = 0
mx = [] #将数据集 去除空行后存入
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读取数据集并消除数据中的空行方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大
家多多支持软件开发网。
您可能感兴趣的文章您可能感兴趣的文章:python merge、concat合并数据集的实例讲解python 实现对数据集的归一化的方法(0-1之
间)python:pandas合并csv文件的方法(图书数据集成)python 划分数据集为训练集和测试集的方法Python sklearn KFold 生成交
叉验证数据集的方法对python中数据集划分函数StratifiedShuffleSplit的使用详解对python制作自己的数据集实例讲解Python
数据集切分实例python 筛选数据集中列中value长度大于20的数据集方法python调用摄像头拍摄数据集
weixin_38595473
- 粉丝: 3
- 资源: 876
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0