栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

numpy文件提取数据【完整例子+数据包】

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

numpy文件提取数据【完整例子+数据包】

'''
Description: numpy数据的保存和提取
Autor: 365JHWZGo
Date: 2021-11-12 18:32:54
LastEditors: 365JHWZGo
LastEditTime: 2021-11-12 20:25:09
'''
import os
import numpy as np

# 超参数
path = 'pytorch/datasets/day_wise_datashrink.csv'
#文末提取

# 打印当前目录的文件
# print('pytorch里的文件:n',os.listdir("."))

# 原始数据

# 打开并以只读的方式查看文件
with open(path, 'r') as f:
    print('原始数据n', f.read())

# 从中抽离出需要处理的数据

# 抽取出标题
title = np.loadtxt(path, delimiter=",", dtype=str, max_rows=1)[np.newaxis, :]
# print(title.shape)

# YMD年月日【yyyy-mm-dd => yyyymmdd】
YMD = np.loadtxt(path, delimiter=",", skiprows=1,
                 max_rows=10, dtype=object, usecols=0)
YMD = [int(''.join(i.split('/'))) for i in YMD]
# print('年月日:n',YMD)

# 其他数据

# 获得列的长度
length = len(np.loadtxt(path, delimiter=",",
             skiprows=1, max_rows=1, dtype=object))

# 获得col下标
col = [i for i in range(length)]

# 对数据进行处理
otherInt1 = np.loadtxt(path, delimiter=",", skiprows=1,
                       max_rows=10, dtype=int, usecols=col[1:-4])
otherInt2 = np.loadtxt(path, delimiter=",", skiprows=1,
                       max_rows=10, dtype=int, usecols=col[-1])
otherFloat1 = np.loadtxt(path, delimiter=",", skiprows=1,
                         max_rows=10, dtype=float, usecols=col[-4:-1])
# print(otherFloat1)
# 整合数据
# 方法1:比较繁琐,先创建一个shape的数组,之后对其进行替换
# after = np.linspace(1,length*10,length*10).reshape(10,length)

# 方法2:使用concatenate对其进行合并
YMD = np.array(YMD)[:, np.newaxis]
# print(YMD)
after = np.concatenate(
    [YMD, otherInt1, otherFloat1, otherInt2[:, np.newaxis]], axis=1, dtype=object)
after = np.concatenate([title, after], axis=0, dtype=object)
print('处理之后的数据:n', after)

# 变成更好看的格式
print('展示数据'.center(40, '-'))
for i in range(len(after[0])):
    print(after[0][i], end='|')
print()
for i in range(1, len(after)):
    for j in range(len(after[i])):
        print(str(after[i][j]), end='t')
    print()

在本文中使用到的数据day_wise_datashrink.csv
欢迎下载使用!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/487771.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号