【1】genfromtxt()函数:从指定文件夹中获取数据。
delimiter:以何种分隔符进行数据分割。dtype:默认读取的数据的用何种方式去读。其他元素可去官网查看具体用法。示例:
import numpy world_test = numpy.genfromtxt("C:\\Users\\Lenovo\\Desktop\\numpyTest.txt",delimiter=",",dtype=str) print(world_test)结果:
[['1998' 'test' '广东广州,男'] ['1998' 'test' '广东广州,男'] ['1998' 'test' '广东广州,男'] ['1998' 'test' '广东广州,男'] ['1998' 'test' '广东广州,男'] ['1998' 'test' '广东广州,男'] ['1998' 'test' '广东广州,男'] ['1998' 'test' '广东广州,男']]【2】array()函数:生成一个矩阵。
shape:查看又array函数创建出来的ndarray的结构。dtype:查看ndarray的数据类型。示例:
import numpy testArray = numpy.array([1,2,3,4,5]) print(testArray ) print(testArray .shape) matrix = numpy.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) print(matrix) print(matrix.shape) print(matrix.dtype)结果:
[1 2 3 4 5] (5,) [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]] (4, 3) int32【3】astype()函数:讲ndarray结果的数组的类型进行改变。
示例:
import numpy vector = numpy.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) print(vector.dtype) vectorSecond = vector.astype(float) print(vectorSecond.dtype) print(vector.dtype)结果:
int32 float64 int32【4】min()函数:获取矩阵中的最小值,并且返回。
示例:
import numpy minArray= numpy.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) minArray.min() #print(minArray.min())结果:
1【5】max()函数:获取矩阵中的最大值,并且返回。
示例:
import numpy maxArray= numpy.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) maxArray.max() #print(maxArray.max())结果:
12【6】sum()函数:获取某一维度的矩阵的和。
参数axis等于1时,表示获取每一行的和,当axis等于0时,表示获取每一列的和。示例:
import numpy matrix = numpy.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) print(matrix) print(matrix.sum(axis=1)) print(matrix.sum(axis=0))结果:
[[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]] [ 6 15 24 33] [22 26 30]【7】arange()函数:用于生成一维数组 。
默认一维为数组:numpy.arange(length)。自定义起点一维数组:numpy.arange(start, stop)。自定义起点步长一维数组:numpy.arange(start, stop, step)。示例:
npFirst = numpy.arange(5) npSecond = numpy.arange(2,5) npThree = numpy.arange(2,15,3) print(npFirst ) print(npSecond) print(npThree)结果:
[0 1 2 3 4] [2 3 4] [ 2 5 8 11 14]【8】reshape()函数:将一维数组转换为多维数组。
示例:
import numpy npFirst = numpy.arange(15) npSecond = npFirst.reshape(3,5) print(npFirst) print(npSecond)结果:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]]【9】ndim()函数:判断矩阵是属于哪个维度。
示例:
import numpy array = numpy.arange(15).reshape(3,5) print(array) print("维度:",array.ndim)结果:
[[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] 维度:2【10】zeros()函数:初始化数组的值为零。
numpy.zeros(5,dtype=int):初始化长度为5的一维数组的值为0,并且数值类型为int。numpy.zeros((2,3)):初始化一个二维数组的默认值为0,并且数值类型默认为float。示例:
import numpy arrayFirst = numpy.zeros(5,dtype=int) arraySecond = numpy.zeros((2,3)) print(arrayFirst) print(arraySecond)结果:
[0 0 0 0 0] [[0. 0. 0.] [0. 0. 0.]]下篇传送门:https://blog.csdn.net/weixin_42146366/article/details/90579581
