Numpy入门之常用函数介绍(上篇)

一、Numpy简介

  • 一个用python实现的科学计算,包括:1、一个强大的N维数组对象Array;2、比较成熟的(广播)函数库;3、用于整合C/C++和Fortran代码的工具包;4、实用的线性代数、傅里叶变换和随机数生成函数。numpy和稀疏矩阵运算包scipy配合使用更加方便。
  • NumPy(Numeric Python)提供了许多高级的数值编程工具,如:矩阵数据类型、矢量处理,以及精密的运算库。专为进行严格的数字处理而产生。多为很多大型金融公司使用,以及核心的科学计算组织如:Lawrence Livermore,NASA用其处理一些本来使用C++,Fortran或Matlab等所做的任务。
  • 数据类型是ndarray。

二、Numpy中几个常用函数介绍

【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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值