numpy.loadtxt() 用法

文章目录

    • 1.numpy.loadtxt 用法 读取txt文件
    • 2.利用savetxt函数将数据存储到文件
    • 3.利用 loadtxt函数读取csv文件

1.numpy.loadtxt 用法 读取txt文件

numpy.loadtxt(fname, dtype=, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)

上面给出了loadtxt所有的关键字参数, 这里我们可以来一一解释并给出示例

import numpy as np
# 首先给出最简单的loadtxt的代码,实际上就是直接写文件名, 其他关键字参数都是默认的.
a = np.loadtxt('./data/test.txt')
print(a)  # a为浮点数的原因为Python默认的数字的数据类型为双精度浮点数
[[ 1.  2.  3.  4.]
 [ 2.  3.  4.  5.]
 [ 3.  4.  5.  6.]
 [ 4.  5.  6.  7.]]
# 这里的skiprows是指跳过前1行, 如果设置skiprows=2, 就会跳过前两行,数据类型设置为整形.
a = np.loadtxt('./data/test.txt', skiprows=1, dtype=int)
print(a)
[[2 3 4 5]
 [3 4 5 6]
 [4 5 6 7]]
# 这里的comment的是指, 如果行的开头为#就会跳过该行
a = np.loadtxt('./data/test1.txt', dtype=int, skiprows=1, comments='#')
print(a)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
# 这里的usecols是指只使用0,2两列, unpack是指会把每一列当成一个向量输出, 而不是合并在一起。
(a, b) = np.loadtxt('./data/test2.txt', dtype=int, skiprows=1, comments='#', delimiter=',', usecols=(0, 2), unpack=True)
print(a, b)
[1 4 7] [3 6 9]
# 最后介绍converters参数, 这个是对数据进行预处理的参数, 我们可以先定义一个函数, 这里的converters是一个字典, 表示第零列使用函数add_one来进行预处理
def add_one(x):
    return int(x)+1  # 注意到这里使用的字符的数据结构

(a, b) = np.loadtxt('./data/test2.txt', dtype=int, skiprows=1, converters={0:add_one}, comments='#', delimiter=',', usecols=(0, 2), unpack=True)
print(a, b)
[2 5 8] [3 6 9]

2.利用savetxt函数将数据存储到文件

savetxt(fileName,data)

参数:
fileName:保存文件路径和名称   
data:需要保存的数据  

3.利用 loadtxt函数读取csv文件

np.loadtxt(filepath,delimiter,usecols,unpack)

参数:
filepath:加载文件路径  
delimiter:加载文件分隔符  
usecols:加载数据文件中列索引    
unpack:当加载多列数据时是否需要将数据列进行解耦赋值给不同的变量  
data = np.loadtxt('./data/data.csv',delimiter=',',skiprows=1, usecols=(2,3))
print(data)
print(data.shape)
[[  37.8   69.2]
 [  39.3   45.1]
 [  45.9   69.3]
 [  41.3   58.5]
 [  10.8   58.4]
 [  48.9   75. ]
 [  32.8   23.5]
 [  19.6   11.6]
 [   2.1    1. ]
 [   2.6   21.2]
 [   5.8   24.2]
 [  24.     4. ]
 [  35.1   65.9]
 [   7.6    7.2]
 [  32.9   46. ]
 [  47.7   52.9]
 [  36.6  114. ]
 [  39.6   55.8]
 [  20.5   18.3]
 [  23.9   19.1]
 [  27.7   53.4]
 [   5.1   23.5]
 [  15.9   49.6]
 [  16.9   26.2]
 [  12.6   18.3]
 [   3.5   19.5]
 [  29.3   12.6]
 [  16.7   22.9]
 [  27.1   22.9]
 [  16.    40.8]
 [  28.3   43.2]
 [  17.4   38.6]
 [   1.5   30. ]
 [  20.     0.3]
 [   1.4    7.4]
 [   4.1    8.5]
 [  43.8    5. ]
 [  49.4   45.7]
 [  26.7   35.1]
 [  37.7   32. ]
 [  22.3   31.6]
 [  33.4   38.7]
 [  27.7    1.8]
 [   8.4   26.4]
 [  25.7   43.3]
 [  22.5   31.5]
 [   9.9   35.7]
 [  41.5   18.5]
 [  15.8   49.9]
 [  11.7   36.8]
 [   3.1   34.6]
 [   9.6    3.6]
 [  41.7   39.6]
 [  46.2   58.7]
 [  28.8   15.9]
 [  49.4   60. ]
 [  28.1   41.4]
 [  19.2   16.6]
 [  49.6   37.7]
 [  29.5    9.3]
 [   2.    21.4]
 [  42.7   54.7]
 [  15.5   27.3]
 [  29.6    8.4]
 [  42.8   28.9]
 [   9.3    0.9]
 [  24.6    2.2]
 [  14.5   10.2]
 [  27.5   11. ]
 [  43.9   27.2]
 [  30.6   38.7]
 [  14.3   31.7]
 [  33.    19.3]
 [   5.7   31.3]
 [  24.6   13.1]
 [  43.7   89.4]
 [   1.6   20.7]
 [  28.5   14.2]
 [  29.9    9.4]
 [   7.7   23.1]
 [  26.7   22.3]
 [   4.1   36.9]
 [  20.3   32.5]
 [  44.5   35.6]
 [  43.    33.8]
 [  18.4   65.7]
 [  27.5   16. ]
 [  40.6   63.2]
 [  25.5   73.4]
 [  47.8   51.4]
 [   4.9    9.3]
 [   1.5   33. ]
 [  33.5   59. ]
 [  36.5   72.3]
 [  14.    10.9]
 [  31.6   52.9]
 [   3.5    5.9]
 [  21.    22. ]
 [  42.3   51.2]
 [  41.7   45.9]
 [   4.3   49.8]
 [  36.3  100.9]
 [  10.1   21.4]
 [  17.2   17.9]
 [  34.3    5.3]
 [  46.4   59. ]
 [  11.    29.7]
 [   0.3   23.2]
 [   0.4   25.6]
 [  26.9    5.5]
 [   8.2   56.5]
 [  38.    23.2]
 [  15.4    2.4]
 [  20.6   10.7]
 [  46.8   34.5]
 [  35.    52.7]
 [  14.3   25.6]
 [   0.8   14.8]
 [  36.9   79.2]
 [  16.    22.3]
 [  26.8   46.2]
 [  21.7   50.4]
 [   2.4   15.6]
 [  34.6   12.4]
 [  32.3   74.2]
 [  11.8   25.9]
 [  38.9   50.6]
 [   0.     9.2]
 [  49.     3.2]
 [  12.    43.1]
 [  39.6    8.7]
 [   2.9   43. ]
 [  27.2    2.1]
 [  33.5   45.1]
 [  38.6   65.6]
 [  47.     8.5]
 [  39.     9.3]
 [  28.9   59.7]
 [  25.9   20.5]
 [  43.9    1.7]
 [  17.    12.9]
 [  35.4   75.6]
 [  33.2   37.9]
 [   5.7   34.4]
 [  14.8   38.9]
 [   1.9    9. ]
 [   7.3    8.7]
 [  49.    44.3]
 [  40.3   11.9]
 [  25.8   20.6]
 [  13.9   37. ]
 [   8.4   48.7]
 [  23.3   14.2]
 [  39.7   37.7]
 [  21.1    9.5]
 [  11.6    5.7]
 [  43.5   50.5]
 [   1.3   24.3]
 [  36.9   45.2]
 [  18.4   34.6]
 [  18.1   30.7]
 [  35.8   49.3]
 [  18.1   25.6]
 [  36.8    7.4]
 [  14.7    5.4]
 [   3.4   84.8]
 [  37.6   21.6]
 [   5.2   19.4]
 [  23.6   57.6]
 [  10.6    6.4]
 [  11.6   18.4]
 [  20.9   47.4]
 [  20.1   17. ]
 [   7.1   12.8]
 [   3.4   13.1]
 [  48.9   41.8]
 [  30.2   20.3]
 [   7.8   35.2]
 [   2.3   23.7]
 [  10.    17.6]
 [   2.6    8.3]
 [   5.4   27.4]
 [   5.7   29.7]
 [  43.    71.8]
 [  21.3   30. ]
 [  45.1   19.6]
 [   2.1   26.6]
 [  28.7   18.2]
 [  13.9    3.7]
 [  12.1   23.4]
 [  41.1    5.8]
 [  10.8    6. ]
 [   4.1   31.6]
 [  42.     3.6]
 [  35.6    6. ]
 [   3.7   13.8]
 [   4.9    8.1]
 [   9.3    6.4]
 [  42.    66.2]
 [   8.6    8.7]]
(200, 2)
#当加载csv文件的多列数据时可以使用unpack将加载的数据列进场解耦到不同数组中
data_seven,data_eight = np.loadtxt("./data/data.csv",delimiter=",",skiprows=1, usecols=(2,3),unpack=True)
print("第7列的数据:\n",data_seven.tolist())
print("第8列的数据:\n",data_eight)
第7列的数据:
 [37.8, 39.3, 45.9, 41.3, 10.8, 48.9, 32.8, 19.6, 2.1, 2.6, 5.8, 24.0, 35.1, 7.6, 32.9, 47.7, 36.6, 39.6, 20.5, 23.9, 27.7, 5.1, 15.9, 16.9, 12.6, 3.5, 29.3, 16.7, 27.1, 16.0, 28.3, 17.4, 1.5, 20.0, 1.4, 4.1, 43.8, 49.4, 26.7, 37.7, 22.3, 33.4, 27.7, 8.4, 25.7, 22.5, 9.9, 41.5, 15.8, 11.7, 3.1, 9.6, 41.7, 46.2, 28.8, 49.4, 28.1, 19.2, 49.6, 29.5, 2.0, 42.7, 15.5, 29.6, 42.8, 9.3, 24.6, 14.5, 27.5, 43.9, 30.6, 14.3, 33.0, 5.7, 24.6, 43.7, 1.6, 28.5, 29.9, 7.7, 26.7, 4.1, 20.3, 44.5, 43.0, 18.4, 27.5, 40.6, 25.5, 47.8, 4.9, 1.5, 33.5, 36.5, 14.0, 31.6, 3.5, 21.0, 42.3, 41.7, 4.3, 36.3, 10.1, 17.2, 34.3, 46.4, 11.0, 0.3, 0.4, 26.9, 8.2, 38.0, 15.4, 20.6, 46.8, 35.0, 14.3, 0.8, 36.9, 16.0, 26.8, 21.7, 2.4, 34.6, 32.3, 11.8, 38.9, 0.0, 49.0, 12.0, 39.6, 2.9, 27.2, 33.5, 38.6, 47.0, 39.0, 28.9, 25.9, 43.9, 17.0, 35.4, 33.2, 5.7, 14.8, 1.9, 7.3, 49.0, 40.3, 25.8, 13.9, 8.4, 23.3, 39.7, 21.1, 11.6, 43.5, 1.3, 36.9, 18.4, 18.1, 35.8, 18.1, 36.8, 14.7, 3.4, 37.6, 5.2, 23.6, 10.6, 11.6, 20.9, 20.1, 7.1, 3.4, 48.9, 30.2, 7.8, 2.3, 10.0, 2.6, 5.4, 5.7, 43.0, 21.3, 45.1, 2.1, 28.7, 13.9, 12.1, 41.1, 10.8, 4.1, 42.0, 35.6, 3.7, 4.9, 9.3, 42.0, 8.6]
第8列的数据:
 [  69.2   45.1   69.3   58.5   58.4   75.    23.5   11.6    1.    21.2
   24.2    4.    65.9    7.2   46.    52.9  114.    55.8   18.3   19.1
   53.4   23.5   49.6   26.2   18.3   19.5   12.6   22.9   22.9   40.8
   43.2   38.6   30.     0.3    7.4    8.5    5.    45.7   35.1   32.
   31.6   38.7    1.8   26.4   43.3   31.5   35.7   18.5   49.9   36.8
   34.6    3.6   39.6   58.7   15.9   60.    41.4   16.6   37.7    9.3
   21.4   54.7   27.3    8.4   28.9    0.9    2.2   10.2   11.    27.2
   38.7   31.7   19.3   31.3   13.1   89.4   20.7   14.2    9.4   23.1
   22.3   36.9   32.5   35.6   33.8   65.7   16.    63.2   73.4   51.4
    9.3   33.    59.    72.3   10.9   52.9    5.9   22.    51.2   45.9
   49.8  100.9   21.4   17.9    5.3   59.    29.7   23.2   25.6    5.5
   56.5   23.2    2.4   10.7   34.5   52.7   25.6   14.8   79.2   22.3
   46.2   50.4   15.6   12.4   74.2   25.9   50.6    9.2    3.2   43.1
    8.7   43.     2.1   45.1   65.6    8.5    9.3   59.7   20.5    1.7
   12.9   75.6   37.9   34.4   38.9    9.     8.7   44.3   11.9   20.6
   37.    48.7   14.2   37.7    9.5    5.7   50.5   24.3   45.2   34.6
   30.7   49.3   25.6    7.4    5.4   84.8   21.6   19.4   57.6    6.4
   18.4   47.4   17.    12.8   13.1   41.8   20.3   35.2   23.7   17.6
    8.3   27.4   29.7   71.8   30.    19.6   26.6   18.2    3.7   23.4
    5.8    6.    31.6    3.6    6.    13.8    8.1    6.4   66.2    8.7]

你可能感兴趣的:(python数据分析)