在机器学习中,常常需要读取txt文本中的数据,这里主要整理了两种读取数据的方式

数据内容

  • 共有四列数据,前三列为特征值,最后一列为数据标签
40920	8.326976	0.953952	3
14488 7.153469 1.673904 2
26052 1.441871 0.805124 1
75136 13.147394 0.428964 1
38344 1.669788 0.134296 1
72993 10.141740 1.032955 1
35948 6.830792 1.213192 3
42666 13.276369 0.543880 3
67497 8.631577 0.749278 1
35483 12.273169 1.508053 3

方式一:手动读取

from numpy import *
import operator
from os import listdir def file2matrix(filename):
fr = open(filename)
numberOfLines = len(fr.readlines()) #get the number of lines in the file
returnMat = zeros((numberOfLines,3)) #prepare matrix to return
classLabelVector = [] #prepare labels return
fr = open(filename)
index = 0
for line in fr.readlines():
line = line.strip()
listFromLine = line.split('\t')
returnMat[index,:] = listFromLine[0:3]
classLabelVector.append(int(listFromLine[-1]))
index += 1
return returnMat,classLabelVector dataMat,dataLabel = file2matrix("datingTestSet2.txt") print(dataMat, dataLabel)

方式二:使用pandas

import numpy as np
import pandas as pd
df_news = pd.read_table('datingTestSet2.txt',header = None)
df_news

详细可以查看下面文档

Python笔记(读取txt文件中的数据)的更多相关文章

  1. Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!

    using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft ...

  2. java读取TXT文件中的数据

    将文件放在一个指定的磁盘目录下: File file = new File("指定的文件路径"); try{ BufferedReader br = new BufferedRea ...

  3. Java中读取txt文件中中文字符时,出现乱码的解决办法

    这是我写的一个Java课程作业时,遇到的问题. 问题描述: 我要实现的就是将txt文件中的内容按一定格式读取出来后,存放在相应的数组. 我刚开始运行时发现,英文可以实现,但是中文字符就是各种乱码. 最 ...

  4. Flex读取txt文件中的内容(三)

    Flex读取txt文件中的内容 1.设计源码 LoadTxt.mxml: <?xml version="1.0" encoding="utf-8"?> ...

  5. SQL SERVER 使用BULK Insert将txt文件中的数据批量插入表中(1)

    1/首先建立数据表 CREATE TABLE BasicMsg( RecvTime FLOAT NOT NULL , --接收时间,不存在时间相同的数据 AA INT NOT NULL, --24位地 ...

  6. Flex读取txt文件中的内容(二)

    Flex读取txt文件中的内容 自动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8" ...

  7. Flex读取txt文件中的内容(一)

    Flex读取txt文件中的内容 phone.txt: 13000003847 13000003848 13000003849 13000003850 13000003851 13000003852 1 ...

  8. Flex读取txt文件中的内容报错

    Flex读取txt文件中的内容 1.具体错误如下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativePat ...

  9. python批量读取txt文件为DataFrame

    我们有时候会批量处理同一个文件夹下的文件,并且希望读取到一个文件里面便于我们计算操作.比方我有下图一系列的txt文件,我该如何把它们写入一个txt文件中并且读取为DataFrame格式呢? 首先我们要 ...

随机推荐

  1. postman-参数化

    1.txt 1.如图第一行为变量名,下面行为对应的值 2.设置 Pre-request-Script 参数 data为文件名,username.password自定义参数名:在Tests最好加上断言 ...

  2. css盒子模型中的border属性

        认识border属性 我们可以通过boder属性来为元素设置边框:元素的边框 (border) 是围绕元素内容和内边距的一条或多条线.CSS border 属性允许你规定元素边框的样式.宽度和 ...

  3. B/S上传文件夹

    文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...

  4. spring util包 StringUtils工具类中的isEmpty() 方法解析

    今天在公司看到同事写的代码,无意发现在判断字符串类型时,使用的是StringUtils工具类中的isEmpty()去判断如下所示 @RequestMapping(value = "/pub/ ...

  5. CSS设置透明背景

    filter: alpha(opacity=40); background-color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0.4); 通过改 ...

  6. 高通平台msm8916修改开机logo【原创】

    经过两天的奋战终于把开机logo给搞定了啊. 首先修改开机logo要从哪里入手呢?先分析一下源码看看. ---> void display_image_on_screen() { struct ...

  7. PriorityBlockingQueue 源码分析

    PriorityBlockingQueue PriorityBlockingQueue 能解决什么问题?什么时候使用 PriorityBlockingQueue? 1)PriorityBlocking ...

  8. C# out 和 ref 区别

    C#里面的 out 和ref参数时常会用到 记录一下: public void Start() { //outSum没必要赋值,赋值了也完全没用. //如果AddByOut函数内部直接使用out对应的 ...

  9. 011-Spring Boot 运行流程分析SpringApplication.run

    一.程序入口 1.1.静态方法 //直接调用run方法 ConfigurableApplicationContext context = SpringApplication.run(App.class ...

  10. 删除C:\Program Files (x86)\Common Files\baidu 等误装软件且正常模式删不掉的文件夹

    ---------方法一-------- C:\Program Files\Common Files\Baidu\BaiduProtect\1.1.0.26打开以上路径找到反注册程序uninst.ex ...