机器学习\数据挖掘学习日记 20160430

Machine Learning in Action

p52 Listing 2.2 :Text record to Numpy parsing code


使用python自带的stream和str类实现

def file2matrix(filename):
    fr = open(filename)
    numberOfLines = len(fr.readlines())
    returnMat = np.zeros((numberOfLines,3))
    classLabelVector = []
    fr = open(filename)
    index = 0
    for line in fr.readlines():
        line = line.strip()
        listFromLine = line.split('\t')
        returnMat[index,:] = listFromLine[0:3]
        #把表示数值的字符串赋值给储存数值的np.array时会自动转换
        classLabelVector.append(int(listFromLine[-1]))
        index += 1
    return returnMat,classLabelVector

(fun) file = open(filename)
Open file and return a corresponding stream.
https://docs.python.org/3/library/functions.html#open

(om) file.readlines()
Read and return a list of lines from the stream
返回的是一个1D list, 文件中的每行内容为list中的一个str
for line in file.readlines() 在py3中可以简写为 for line in file
https://docs.python.org/3/library/io.html?highlight=readlines#io.IOBase.readlines

相似的method有
(om) file.readline(size=-1)
Read and return one line from the stream. If size is specified, at most size bytes will be read.
https://docs.python.org/3/library/io.html?highlight=readlines#io.IOBase.readline

(om) str.strip([chars])
Return a copy of the string with the leading and trailing characters removed

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值