Python笔记(读取txt文件中的数据)
在机器学习中,常常需要读取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
详细可以查看下面文档
- pandas官方文档:https://pandas.pydata.org/pandas-docs/version/0.18.1/generated/pandas.read_table.html
- pandas.core.frame.DataFrame:https://blog.csdn.net/daydayup_668819/article/details/82315565
Python笔记(读取txt文件中的数据)的更多相关文章
- Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!
using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft ...
- java读取TXT文件中的数据
将文件放在一个指定的磁盘目录下: File file = new File("指定的文件路径"); try{ BufferedReader br = new BufferedRea ...
- Java中读取txt文件中中文字符时,出现乱码的解决办法
这是我写的一个Java课程作业时,遇到的问题. 问题描述: 我要实现的就是将txt文件中的内容按一定格式读取出来后,存放在相应的数组. 我刚开始运行时发现,英文可以实现,但是中文字符就是各种乱码. 最 ...
- Flex读取txt文件中的内容(三)
Flex读取txt文件中的内容 1.设计源码 LoadTxt.mxml: <?xml version="1.0" encoding="utf-8"?> ...
- SQL SERVER 使用BULK Insert将txt文件中的数据批量插入表中(1)
1/首先建立数据表 CREATE TABLE BasicMsg( RecvTime FLOAT NOT NULL , --接收时间,不存在时间相同的数据 AA INT NOT NULL, --24位地 ...
- Flex读取txt文件中的内容(二)
Flex读取txt文件中的内容 自动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8" ...
- Flex读取txt文件中的内容(一)
Flex读取txt文件中的内容 phone.txt: 13000003847 13000003848 13000003849 13000003850 13000003851 13000003852 1 ...
- Flex读取txt文件中的内容报错
Flex读取txt文件中的内容 1.具体错误如下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativePat ...
- python批量读取txt文件为DataFrame
我们有时候会批量处理同一个文件夹下的文件,并且希望读取到一个文件里面便于我们计算操作.比方我有下图一系列的txt文件,我该如何把它们写入一个txt文件中并且读取为DataFrame格式呢? 首先我们要 ...
随机推荐
- NOIP2016 D2T2 蚯蚓
洛谷P2827 其实是一道不是很难的模拟题,暴力好像可以拿80,AC的话要发现其中隐含的单调性 首先是一个小技巧,每次将所有蚯蚓的长度都+q肯定时间复杂度很大,那我们就想,其他所有的蚯蚓加,就相当于取 ...
- python补充之进制转换、exec、eval、compile
目录 eval.exec和compile 1.eval函数 2.exec函数 eval()函数和exec()函数的区别 python中的进制转换 eval.exec和compile 1.eval函数 ...
- sql2008 误操作还原至指定时间点
--drop database db --创建一个测试库 create database db go --备份一个完整备份文件 backup database db to disk = 'd:\db. ...
- 牛客网 珂学送分( 期望DP )
题意 : 题目链接 分析 : 听队友说一般概率从前往后推.期望从后往前推......... #include<bits/stdc++.h> using namespace std; ; d ...
- R list和data frame 排序
pathway_name = rownames(g1) tm <- list('P-value' = c(), 'Pathway_name' = c()) :dim(g1)[]){ result ...
- 阿里云CentOS 7.3安装Redis详细步骤
############ 准备 ############### 从Redis官网下载Linux redis3.2.6版本,我下载的redis-3.2.6.tar.gz(目前最新稳定版),下载到/u ...
- Java数据结构之单链表
这篇文章主要讲解了通过java实现单链表的操作,一般我们开始学习链表的时候,都是使用C语言,C语言中我们可以通过结构体来定义节点,但是在Java中,我们没有结构体,我们使用的是通过类来定义我们所需要的 ...
- CharacterEncodingFilter详解
字符编码过滤器 (Spring框架对字符编码的处理) 基于函数回调,对所有请求起作用,只在容器初始化时调用一次,依赖于servlet容器. web.xml配置文件 <filter> &l ...
- 1>/dev/null 2>&1的含义
shell中可能经常能看到:>/dev/null 2>&1 分解这个组合:“>/dev/null 2>&1” 为五部分. 1:> 代表重定向到哪 ...
- review star 评论-评分 文本分析
<!DOCTYPE html> Title 立项背景: 0-突然被限制,无法访问原amazon_asin_reviews_us数据库: 1-原数据库asin类别.厂家信息不明: 2-自然语 ...