numpy detailed tutorial notes

Table of Contents
1. Introduction to numpy 2. Summary
of functions, attributes and methods
3. Data types
4. Array indexing and slicing
5. Array combination and decomposition
6. Array access

1. Introduction to numpy
NumPy (Numerical Python) is an open source scientific computing library of Python,
implemented by C language, and provides the function of matrix operations. It is generally used with Scipy and matplotlib.

The core of the NumPy package is the ndarray object. It encapsulates python's native n-dimensional array of the same data type. In order to ensure its excellent performance,
many operations are executed after the code is compiled locally

Function:
1. ndarray, a multi-dimensional data structure, efficient and space saving

2. Mathematical functions for fast calculation of the entire set of data without looping

3. Tools for reading and writing disk data and tools for manipulating memory mapped files

4. Linear algebra, random number generation and Fourier transform functions

5. Tools for users to integrate C, C++ and other codes

Python code and NumPy operation efficiency comparison

import sys
import numpy as np
import time
def python_list(n):
	a = list(range(n))
	b = list(range(n))
	c = []
	for i in range(len(a)):
		a[i] = i**2
		b[i] = i**3
		c.append(a[i]+b[i])
	return c
py_st_time = time.time()
c = 10000
python_list(c)
waste_time1 = time.time()-py_st_time
print("python中容器向量元素为:%s   耗时:%.4f"%(c,waste_time1))

def numpy_list(n):
	a = np.arange(n)**2
	b = np.arange(n)**3
	c = a+b
	return c

numpy_st_time = time.time()
numpy_list(c)
waste_time2 =  time.time()-numpy_st_time
print("numpy向量元素为:%s     耗时:%.4f"%(c,waste_time2))
print("numpy比python容器效率高%d倍"%(waste_time1/waste_time2))

The main object of NumPy is a homogeneous multidimensional array, which is a table of elements (usually numbers), all types are the same (most of the same quality, special exceptions are heterogeneous),
indexed by non-negative integer tuples
NumPy's ndarray is Multidimensional array object.
It consists of two parts: the actual number; the metadata describing the data.
Most array operations only modify the metadata part, without changing the actual underlying data.
Please note that numpy.array is different from the standard Python library class array.array, which only deals with one-dimensional arrays and provides less functionality.

2. Summary of functions, attributes and methods

1, the operation of the array

np.abs(a) np.fabs(a) : 取各元素的绝对值 
np.sqrt(a) : 计算各元素的平方根 
np.square(a): 计算各元素的平方 
np.log(a) np.log10(a) np.log2(a) : 计算各元素的自然对数、10、2为底的对数 
np.ceil(a) np.floor(a) : 计算各元素的ceiling 值, floor值(ceiling向上取整,floor向下取整) 
np.rint(a) : 各元素 四舍五入 
np.modf(a) : 将数组各元素的小数和整数部分以两个独立数组形式返回 
np.exp(a) : 计算各元素的指数值 
np.sign(a) : 计算各元素的符号值 1(+),0,-1(-) 
. 
np.maximum(a, b) np.fmax() : 比较(或者计算)元素级的最大值 
np.minimum(a, b) np.fmin() : 取最小值 
np.mod(a, b) : 元素级的模运算 
np.copysign(a, b) : 将b中各元素的符号赋值给数组a的对应元素

np.ones(shape): 生成全1 
np.zeros((shape), ddtype = np.int32) : 生成int32型的全0 
np.full(shape, val): 生成全为val 
np.eye(n) : 生成单位矩阵

np.ones_like(a) : 按数组a的形状生成全1的数组 
np.zeros_like(a): 同理 
np.full_like (a, val) : 同理

2. ndarray object properties:

ndarray.ndim    #数组维度
ndarray.shape   #数组的各维度尺度
ndarray.size 	#数组元素的总数
ndarray.dtype  	#ndarray对象的元素类型
ndarray.itemsize #ndarray对象中每个元素的字节大小
ndarray.flags 	#ndarray对象的内存信息
ndarray.real 	#ndarray元素的实部
ndarray.imag 	#ndarray元素的虚部
ndarray.data 	#缓冲区包含数组的实际元素
ndarray.nbytes  #数组所占存储空间(itemsize和size的乘积)

3. Statistical functions of NumPy

np.sum() np.min()
np.mean() np.max()
np.average() np.argmin()
np.std() np.argmax()
np.var() np.unravel_index()
np.median() np.ptp()

4. numpy random number function
numpy's random sub-library

np.random.rand(d0, d1, …,dn) : 各元素是(0, 1)的浮点数,服从均匀分布 
np.random.randn(d0, d1, …,dn):标准正态分布 
np.random.randint(low, high,( shape)): 依shape创建随机整数或整数数组,范围是[ low, high) 
np.random.seed(s) : 随机数种子

np.random.shuffle(a) : 根据数组a的第一轴进行随机排列,改变数组a 
np.random.permutation(a) : 根据数组a的第一轴进行随机排列, 但是不改变原数组,将生成新数组 
np.random.choice(a[, size, replace, p]) 
#从一维数组a中以概率p抽取元素, 形成size形状新数组,replace表示是否可以重用元素,默认为False。 


replace = False时,选取过的元素将不会再选取

uniform(low, high, size) : 产生均匀分布的数组,起始值为low,high为结束值,size为形状 
normal(loc, scale, size) : 产生正态分布的数组, loc为均值,scale为标准差,size为形状 
poisson(lam, size) : 产生泊松分布的数组, lam随机事件发生概率,size为形状 
eg: a = np.random.uniform(0, 10, (3, 4)) a = np.random.normal(10, 5, (3, 4))

5. numpy statistical functions

sum(a, axis = None) : 依给定轴axis计算数组a相关元素之和,axis为整数或者元组 
mean(a, axis = None) : 同理,计算平均值 
average(a, axis =None, weights=None) : 依给定轴axis计算数组a相关元素的加权平均值 
std(a, axis = None) :同理,计算标准差 
var(a, axis = None): 计算方差 
eg: np.mean(a, axis =1) : 对数组a的第二维度的数据进行求平均 
a = np.arange(15).reshape(3, 5) 
np.average(a, axis =0, weights =[10, 5, 1]) : 
对a第一各维度加权求平均,weights中为权重,注意要和a的第一维匹配

min(a) max(a) : 计算数组a的最小值和最大值 
argmin(a) argmax(a) : 计算数组a的最小、最大值的下标(注:是一维的下标) 
unravel_index(index, shape) : 根据shape将一维下标index转成多维下标 
ptp(a) : 计算数组a最大值和最小值的差 
median(a) : 计算数组a中元素的中位数(中值) 
eg:a = [[15, 14, 13], 
[12, 11, 10] ] 
np.argmax(a) –> 0 
np.unravel_index( np.argmax(a), a.shape) –> (0,0)

5. Numpy's gradient function

np.gradient(a) : 计算数组a中元素的梯度,f为多维时,返回每个维度的梯度 
离散梯度: xy坐标轴连续三个x轴坐标对应的y轴值:a, b, c 其中b的梯度是(c-a)/2 
而c的梯度是: (c-b)/1

当为二维数组时,np.gradient(a) 得出两个数组,第一个数组对应最外层维度的梯度,
第二个数组对应第二层维度的梯度。 

6, the creation and change of the array

np.arange(n) ; 元素从0到n-1的ndarray类型数组
np.linspace(start,stop,num=50 ,endpoint=True)	#创建从start到stop之间等分的num个元素的数组
num为数组中的元素个数,默认50;
endpoint为是否包括数组终止值,默认是包括(True).
eye  创建单位矩阵(主对角线元素为1,其他为0)
np.empty(shape, dtype = float, order = 'C')		#创建随机值数组,数组元素为随机值,因为它们未初始化
np.zeros(shape, dtype = float, order = 'C')		#返回特定大小,以 0 填充的新数组
np..ones(shape, dtype = None, order = 'C')	#返回特定大小,以 1 填充的新数组

Shape: 空数组的形状,整数或整数元组
Dtype: 所需的输出数组类型,可选
Order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组

>>> import numpy as np
>>> x = np.empty([3,3].dtype = int)
>>> x=np.empty([3,3],dtype = int)
>>> x
array([[1886272888, 1886217518, 1529379188],
   [1563634739, 2037670956, 1025533296],
   [1953392928,     658729,  170817889]])
 >>> y = np.zeros(2)
>>> y
array([0., 0.])
>>> z = np.zeros([2,2])
>>> z
array([[0., 0.],
       [0., 0.]])


np.reshape(shape) : 不改变当前数组,改变维度
np.resize(shape) : 改变当前数组,改变维度
np.swapaxes(ax1, ax2) : 将两个维度调换 
np.flatten() : 多维变一维,可保存结果
np.ravel() : 多维变一维,仅返回一个视图
np.transpose() : 转置矩阵
np.T 			 : 转置矩阵(一维数组转置后是其本身)
np.vstack():垂直方向(纵向)的数组堆叠
np.hstack():水平方向(横向)的数组合并
np.real 		 : 给出复数数组的实部
np.imag 	 	 : 给出复数数组的虚部

>>> import numpy as np 
>>> a = np.arange(20).reshape(4,5)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
>>> a.shape
(4, 5)
>>> a.ndim
2
>>> a.dtype.name
'int32'
>>> a.itemsize
4
>>> a.size
20
>>> type(a)
<class 'numpy.ndarray'>
>>> b = np.array([4,5,6])
>>> b
array([4, 5, 6])
>>> type(b)
<class 'numpy.ndarray'>


>>> import numpy as np 		#导入库为np
>>> a = np.array([1,2,3])
>>> a
array([1, 2, 3])
>>> a.dtype
dtype('int32')
>>> a.shape
(3,)

>>> b = array([arange(2),arange(2)])	#建立二维数组	
>>> b
array([[0, 1],
       [0, 1]])
>>> b.shape
(2, 2)

>>> a = array([[1,2],[3,4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a[0,0]
1
>>> a[0,0],a[0,1],a[1,0],a[1,1]		#二维数组数据元素位置
(1, 2, 3, 4)

Three, numpy data type

bool_	布尔型数据类型(True 或者 False)
int_	默认的整数类型(类似于 C 语言中的 long,int32 或 int64)
intc	与 C 的 int 类型一样,一般是 int32 或 int 64
intp	用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64)
int8	整数(-128 to 127)
int16	整数(-32768 to 32767)
int32	整数(-2**31 to 2**31-1)
int64	整数(-2**63 to 2**63-1)
uint8	无符号整数(0 to 255)
uint16	无符号整数(0 to 65535)
uint32	无符号整数(0 to 2**32-1)
uint64	无符号整数(0 to 2**64-1)
float_	float64 类型的简写
float16	半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位
float32	单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位
float64	双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位
complex_	complex128 类型的简写,即 128 位复数

Complex numbers cannot be converted to integers or floating-point numbers, floating-point numbers can be converted to complex numbers

Numpy can use character encoding to represent data types

i 整数 
u 无符号整数 
f 单精度浮点数 
d 双精度浮点数 
b 布尔值 
D 复数 
S 字符串 
U unicode字符串 
V void (空) 


>>> arange(7,dtype='f')			#创建单精度浮点数数组
array([0., 1., 2., 3., 4., 5., 6.], dtype=float32)

Custom data type

Data type conversion: a.astype(new_type): eg, a.astype (np.float)
array to list conversion: a.tolist()

>>> dtype(float)
dtype('float64')
>>> dtype('f')
dtype('float32')
>>> dtype('d')
dtype('float64')
>>> dtype('f8')		#2、4、8分别代表精度为16、32、64位的浮点数
dtype('float64')
>>> sctypeDict.keys()	#查询完整的NumPy数据类型列表

dtype class attributes

>>>t = np.dtype('Float64')
>>> t.char 					
'd'
>>> t.type
<class 'numpy.float64'>
>>> t.str
'<f8'

Fourth, the index and slice of the array

Index of one-dimensional array

All index methods of lists can be used on one-dimensional arrays.
List index can refer to: Detailed List

Two-dimensional array index

The position index must be written in the form of [rows, cols] to index a single element by specifying the row and column.
Use np.ix_() for multiple element indexes. Use commas to separate multiple single-element indexes.

>>> a = np.arange(20).reshape(4,5)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
>>> a[3,4]
19
>>> a[np.ix_([0,-3],[2,2])]
array([[2, 2],
       [7, 7]])

Multidimensional array index
a = np.arange(24).reshape((2, 3, 4))
a[1, 2, 3] represents the number in 3 dimensions, and the number of each dimension is separated by
a comma a [:,: ,:: 2] By default, it means starting from the 0th element to the last element

>>> a = np.arange(24)
>>> a
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 = np.arange(24).reshape(2,3,4)
>>> 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]]])
>>> a.shape
(24,)
>>> b.shape
(2, 3, 4)
>>> b[0,2]
array([ 8,  9, 10, 11])
>>> b[0,1,::2]
array([4, 6])
>>> b[0,:,:]
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> b[0,...]				#多个冒号可以用一个省略号(...)来代替
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> b[...,1]				
array([[ 1,  5,  9],
       [13, 17, 21]])
>>> b[1,...]
array([[12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])
>>> b[:,1]
array([[ 4,  5,  6,  7],
       [16, 17, 18, 19]])
>>> b[0,:,1]
array([1, 5, 9])
>>> b[0,:,-1]
array([ 3,  7, 11])
>>> b[0,::-1,-1]
array([11,  7,  3])
>>> b[0,::2,-1]
array([ 3, 11])
>>> b[::-1]
array([[[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]],

       [[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]]])


>>> b.ravel()		#展平
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])
>>> c=b.flatten()	#展平并保存结果
>>> c
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],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

>>> b.shape = (6,4)			#维度转换
>>> 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.transpose()				#转置矩阵
array([[ 0,  4,  8, 12, 16, 20],
       [ 1,  5,  9, 13, 17, 21],
       [ 2,  6, 10, 14, 18, 22],
       [ 3,  7, 11, 15, 19, 23]])

>>> b.reshape((2,12))
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],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])
>>> b.resize((2,12))
>>> 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]])

Five, the combination and decomposition of arrays

>>> a = np.arange(9).reshape(3,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> b = 2 * a
>>> b
array([[ 0,  2,  4],
       [ 6,  8, 10],
       [12, 14, 16]])

1. Horizontal combination

np.hstack(())
np.concatenate((),axis = 1) #axis defaults to 0

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

2.Vertical combination

np.vstack(())
np.concatenate((),axis = 0)

>>> 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. Deep combination

Stack and combine along the vertical axis (depth)

e.g. dstack (())

>>> 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 combination

For one-dimensional arrays, it will be combined in the column direction.
For two-dimensional arrays, the same effect as hstack

np.column_stack(())

>>> set1 = np.arange(2)
>>> set1
array([0, 1])
>>> np.array([0,1])
array([0, 1])
>>> set2 = 2 * set1
>>> set2
array([0, 2])
>>> np.column_stack((set1,set2))
array([[0, 0],
       [1, 2]])


>>> np.column_stack((a,b))
array([[ 0,  1,  2,  0,  2,  4],
       [ 3,  4,  5,  6,  8, 10],
       [ 6,  7,  8, 12, 14, 16]])

>>> np.column_stack((a,b)) == np.hstack((a,b))
array([[ True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True]])

5. Row combination

np.row_stack(()) One-dimensional arrays are directly stacked

>>> np.row_stack((set1,set2))
array([[0, 1],
       [0, 2]])

6, the separation of the array

hsplit horizontal split
split horizontal split (specify axis=1)
vsplit vertical split
split vertical split (specify axis=0)
dsplit deep split

>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> np.hsplit(a,3)			水平分割
[array([[0],
       [3],
       [6]]), array([[1],
       [4],
       [7]]), array([[2],
       [5],
       [8]])]
np.split(a, 3, axis=1)		水平分割(上同)
[array([[0],
       [3],
       [6]]), array([[1],
       [4],
       [7]]), array([[2],
       [5],
       [8]])]
>>> np.vsplit(a, 3)			垂直分割
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
>>> np.split(a, 3, axis=0)	垂直分割(上同)
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]

>>> c = np.arange(27).reshape(3,3,3)
>>> c
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]]])
>>> np.dsplit(c,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]]])]

Six, data access

Access to one-dimensional and two-dimensional data

Save one-dimensional or two-dimensional data
np.savetxt(frame, array, fmt='% .18e', delimiter = None)

The frame is the name of the accessed file or string, etc., which can be a compressed file of .gz .bz2;
array represents the name of the array of data to be stored in the file;
fmt represents the format of the written file eg: %d% .2f% .18e ;
delimiter: split string, default is space
eg: np.savetxt('a.csv', a, fmt=%d, delimiter =',')

>>> import numpy as np
>>> i1 =np.eye(5)
>>> print(i1)
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
>>> np.savetxt("f.txt",i1)


>>>c,v=np.loadtxt('data.csv',delimiter=',',usecols=(6,7),unpack=True)
#数据存储在data.csv中,设置分隔符为',',usecols设置为元组,获取第7字段至第8字段的数据
#unpack参数设置为True(分拆存储不同列的数据)

Take one-dimensional or two-dimensional data

np.loadtxt(frame, dtype=np.float, delimiter = None, unpack = False) :

frame is the name of the file to be read, which can be a compressed file of
.gz.bz2 ; dtype: data type, the read data is stored in this type;
delimiter: split string, default is a space;
unpack: if it is True, read in The attributes will be written to different variables respectively.
eg: np.loadtxt('a.csv',dtype=np.int,delimiter=',')

NumPy's convenient file access
np.save(frame, array) or np.savez(frame, array): frame: file name, with .npy as the extension, compressed extension is .npz; array is the array variable
np.load (fname): frame: file name, with .npy extension, compressed extension is .npz

When using np.save() and np.load(), you don't need to consider the data type and dimension yourself.

>>>b = np.arange(20)
>>>np.save('b',b) 		#保存文件
>>>np.load('b.npy') 	#读取文件

CSV (Comma-Separated Value, comma separated value) format is a common file format.
Usually, the dump file of the database is the CSV format. CSV can only store one-dimensional and two-dimensional arrays, which
can be understood as an excel file, which can be used as excel. The software opens.

Multi-dimensional data access

Store multi-dimensional data
a.tofile(frame, sep='', format='%s' ):
frame: file name;
sep: data segmentation string, if it is an empty string, write the file as binary;
format:: write The format of the data
eg: a = np.arange(100).reshape(5, 10, 2)
a.tofile(“b.dat”, sep=”,”, format='%d')

Take multi-dimensional data
np.fromfile(frame, dtype = float, count=-1, sep=''):
frame: file name;
dtype: read data stored in this type;
count: number of read elements, -1 Means to read the entire file;
sep: data segmentation string, if it is an empty string, write the file as binary

tofile虽然可以存入多维数组,但是写入的文件其实是转换成了一维数组
如果用fromfile读取多维数组,fromfile只能读取相当于一个[]中的内容
所以该方法需要读取时知道存入文件时数组的维度和元素类型
a.tofile 和 np.fromfile 需要配合使用
可以通过元数据文件来存取额外信息(即再新建一个文件来存储这方面的信息)

Data or dimensions are not written manually, and the data type is unknown. Use the genformtxt() function

genfromtxt() reads data from external text files (CSV and txt)

np.genfromtxt(fname,dtype=<class 'float'>,comments='#',dskip_footer=0,converters=None,
missing_values=None,filling_valnames=None,)

fname: Specify the file path where the data needs to be read
dtype: Specify the data type of the data to be read, the default is floating point, if the data contains character data, the data type must be specified as str.
comments: specify the comment symbol, the default is' #', r If there is a'#' at the beginning of the original data line, the reading of these lines will be ignored.
delimiter: Specify the column delimiter of the data set.
skip_header: Whether to skip the first row of the data set, it is not skipped by default.
skip_footer: Whether to skip the footnotes of the dataset, it is not skipped by default.
converters: Convert the data in the specified column to other values.
missing_values: Specify the missing value tags. If the original data set contains the specified tags, such data will be missing values ​​after reading in.
filling_valnames: Specify the filling value of missing values.
usecols: Specify which columns need to be read.
names: set the column name for the column of the read number

>>> f = np.genfromtxt(fname = r'C:\Users\zhangliw\software\python\f.txt',delimiter = '\t',skip_header = 1)
>>> type(f)					#查看数据结构
<class 'numpy.ndarray'>
>>> f.ndim					#查看数据维数
1
>>> f.shape 				#查看数据行列数
(4,)
>>> f.dtype 				#查看数据元素的数据类型
dtype('float64')
>>> f.size 					#查看数组元素个数
4

Guess you like

Origin blog.csdn.net/qq_41952762/article/details/108538763