一个间接读取txt文件数据,并保存为矩阵的小程序(小白一枚)

小白一枚,记录一下使用python中的失误
一个小循环,里面主要是array的应用,之间的合并

用元祖设置维度
>>> b.shape=(4,2,3)
>>> b
array([ 0, 1, 2],
        [ 3, 4, 5,6, 7, 8],
        [ 9, 10, 11,12, 13, 14],
        [15, 16, 17,18, 19, 20],
        [21, 22, 23])
转置:
>>> b
array(0, 1],
       [2, 3)
>>> b.transpose()
array(0, 2],
       [1, 3)

=============数组的组合==============
>>> a
array(0, 1, 2],
       [3, 4, 5],
       [6, 7, 8)
>>> b = a*2
>>> b
array(0, 2, 4],
       [ 6, 8, 10],
       [12, 14, 16)

1.水平组合
>>> np.hstack((a,b))
array(0, 1, 2, 0, 2, 4],
       [ 3, 4, 5, 6, 8, 10],
       [ 6, 7, 8, 12, 14, 16)
>>> np.concatenate((a,b),axis=1)
array(0, 1, 2, 0, 2, 4],
       [ 3, 4, 5, 6, 8, 10],
       [ 6, 7, 8, 12, 14, 16)

2.垂直组合
>>> np.vstack((a,b))
array(0, 1, 2],
       [ 3, 4, 5],
       [ 6, 7, 8],
       [ 0, 2, 4],
       [ 6, 8, 10],
       [12, 14, 16)
>>> np.concatenate((a,b),axis=0)
array(0, 1, 2],
       [ 3, 4, 5],
       [ 6, 7, 8],
       [ 0, 2, 4],
       [ 6, 8, 10],
       [12, 14, 16)

3.深度组合:沿着纵轴方向组合
>>> np.dstack((a,b))
array([ 0, 0],
        [ 1, 2],
        [ 2, 4,3, 6],
        [ 4, 8],
        [ 5, 10,6, 12],
        [ 7, 14],
        [ 8, 16])

4.列组合column_stack()
一维数组:按列方向组合
二维数组:同hstack一样

5.行组合row_stack()
以为数组:按行方向组合
二维数组:和vstack一样

6.==用来比较两个数组
>>> a==b
array(True, False, False],
       [False, False, False],
       [False, False, False, dtype=bool)
#True那个因为都是0啊

==================数组的分割===============
>>> a
array(0, 1, 2],
       [3, 4, 5],
       [6, 7, 8)
>>> b = a*2
>>> b
array(0, 2, 4],
       [ 6, 8, 10],
       [12, 14, 16)

1.水平分割(难道不是垂直分割???)
>>> np.hsplit(a,3)
[array(0],
       [3],
       [6),
 array(1],
       [4],
       [7),
array(2],
       [5],
       [8)]
split(a,3,axis=1)同理达到目的

2.垂直分割
>>> np.vsplit(a,3)
[array(0, 1, 2), array(3, 4, 5), array(6, 7, 8)]

split(a,3,axis=0)同理达到目的

3.深度分割
某三维数组:::
>>> d = np.arange(27).reshape(3,3,3)
>>> d
array([ 0, 1, 2],
        [ 3, 4, 5],
        [ 6, 7, 8,9, 10, 11],
        [12, 13, 14],
        [15, 16, 17,18, 19, 20],
        [21, 22, 23],
        [24, 25, 26])

深度分割后(即按照深度的方向分割)
注意:dsplite只对3维以上数组起作用
raise ValueError('dsplit only works on arrays of 3 or more dimensions')
ValueError: dsplit only works on arrays of 3 or more dimensions

>>> np.dsplit(d,3)
[array([ 0],
        [ 3],
        [ 6,9],
        [12],
        [15,18],
        [21],
        [24]), array([ 1],
        [ 4],
        [ 7,10],
        [13],
        [16,19],
        [22],
        [25]), array([ 2],
        [ 5],
        [ 8,11],
        [14],
        [17,20],
        [23],
        [26])]

===================数组的属性=================
>>> a.shape #数组维度
(3, 3)
>>> a.dtype #元素类型
dtype('int32')
>>> a.size #数组元素个数
9
>>> a.itemsize #元素占用字节数
4
>>> a.nbytes #整个数组占用存储空间=itemsize*size
36
>>> a.T #转置=transpose
array(0, 3, 6],
       [1, 4, 7],
       [2, 5, 8)

下面这个主要是在间隔选取txt文件的一个小循环

import numpy as np
from numpy import linalg as la
from numpy import *
import itertools


def open_file(path,row1,row2):
    """path:位置,row1:起始行数,row2:终止行数"""
    with open(path) as f_in:
        x = np.genfromtxt(itertools.islice(f_in,row1,row2), dtype=float)  #genformtxt及itertools的用法,
    return x
a = int(input('请输入截取行'))
b = int(input('请输入间隔'))
t = int (input('请输入有多少个时间段'))
c = 0
d = int(input('请输入headlines行数'))
snapshots =np.zeros(shape=(b,2))
for i in range(1,t+1):
    c = a + d
    d = b + c
    i += 1
    snapshots = np.vstack((snapshots,open_file(path1, c, d)))
print(snapshots)


版权声明:本文为qq_40362523原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。