Python中Numpy库使用总结
来源:互联网 发布:文档识别软件 编辑:程序博客网 时间:2024/06/04 19:42
Python中Numpy库使用总结
- Numpy库的使用
Numpy库和Pandas库均是python机器学习中不可缺少的一个包,其重要性我就不详细说明,直接上干活
Numpy库的使用
Numpy库的安装
- windown中提前安装好python,并且配置本地环境,具体方法参照百度
- 检查是否安装好pip(pip是python下一个好基友)
- window下命令行直接 pip install numpy 就okay
矩阵的基本属性
在本文中,直接套用线性代数的中矩阵代替原文中Numpy array
python下使用Numpy,首先需要引入numpy库
import numpy as np# 以下效果相同import numpy
python下Numpy元素的类型
import numpy as np# arange函数是生成0-14的排列 test_numpy = np.arange(15).reshape(3, 5)print(type(test_numpy))# 输出的结果为 <type 'numpy.ndarray'>
ndarray.ndim 函数,求解矩阵的维数
import numpy as nptest_numpy = np.arange(15).reshape(3, 5)test_ndim = test_numpy.ndimprint(test_ndim)# 输出的结果为 2
ndarray.shape 函数,求解矩阵的规模,通常情况用元组表示每一维的大小,比如一个3*2的矩阵求解结果为(3,2)
import numpy as nptest_numpy = np.arange(15).reshapes(3, 5)print(type(test_numpy.shape))# 输出结果为 <type 'tuple'>print(test_numpy.shape)# 输出结果为 (3, 5) Tip:有些版本会输出(3L, 5L)
ndarray.size 函数,返回矩阵元素的个数,等于ndarray.shape元素之积
import numpy as nptest_numpy = np.arange(15).reshapes(3, 5)print(test_numpy.size)# 输出结果为 15
ndarray.dtype 函数,返回矩阵中元素的类型,类型可以是自己创建的类型或者是标准的python类型,在Numpy中也有自己的类型,有numpy.int32, numpy.int16, numpy.float64等等类型
import numpy as nptest_numpy = np.arange(15).reshapes(3, 5)print(test_numpy.dtype)# 输出结果为 dtype('int32')print(test_numpy.dtype.name)# 输出结果为 int32
ndarray.itemsize函数,返回矩阵中每个元素所占的字节数,如同C语言中的sizeof(int)所占4个字节,ndarray.dtype表示当前元素的类型为int32,因此占32/8=4个字节
import numpy as nptest_numpy = np.arange(15).reshapes(3, 5)print(test_numpy.itemsize)# 输出结果为 4
- ndarray.data 返回矩阵在内存中的具体信息
import numpy as nptest_numpy = np.arange(15).reshapes(3, 5)print(test_numpy)# 输出结果 <read-write buffer for 0x0000000006778F30, size 60, offset 0 at 0x00000000061ADC38> 表明在内存中的具体情况
矩阵的创建
- 利用np.array()创建一个矩阵,注意array()里面是一个python列表或者元组
import numpy as nptest_numpy = np.array([1, 2, 3, 4])# 此种方法是错误的 np.array(1, 2, 3, 4)test_numpy_two = np.array([(1, 2, 3), (4, 5, 6)])
- 利用np.array()创建指定复杂联系元素
import numpy as nptest_numpy = np.array([1, 2], [3, 4], dtype=complex)# test_numpy为 array([[1.+0.j, 2.+0.j], [3.+0.j, 4.+0.j]])
- 利用np.zeros()创建矩阵,利用np.zeros时一般矩阵的大小已知,但是内容未知,我们将其所有元素初始化为0
import numpy as nptest_numpy_1 = np.zeros((3, 4))# 注意此处传入的参数是(3, 4),而非3,4test_numpy_2 = np.zeros((3, 4), dtype=np.int32)
- 利用np.ones()创建矩阵,使用np.ones条件和np.zeros一样,不过将其所有元素初始化为1
import numpy as np test_numpy_1 = np.zeros((3, 4)) test_numpy_2 = np.zeros((3, 4), dtype=np.int32)
- 利用np.arange()创建矩阵,np.arange()功能和python中的range()函数一样,生成一个序列。
import numpy as nptest_numpy = np.arange(0, 15, 1)
- 利用np.linspace()创建矩阵,np.linspace()的作用和np.arange()类似,但是略有不同,np.arange(begin, end, step)中指定起始区间,和步长,np.linspace(begin, end, num)中指定起始起始区间和需要取的数字。具体细节可以在编程中体会
import numpy as nptest_numpy = np.linspace(0, 15, 16)
- 其他函数同样可以创建矩阵,这里不一一介绍,如zeros_like, ones_like, empty, empty_like, numpy.random.rand, numpy.random.randn, fromfunction, fromfile
矩阵的打印
- 矩阵的打印,直接使用python中print()函数即可
import numpy as npnumpy_test = np.arange(15)print(numpy_test)
矩阵的基本运算
矩阵的基本运算和线性代数里面矩阵的运算一致,不明白的建议翻阅《线性代数》一书
- 矩阵的加法、减法和数乘
import numpy as nptest_numpy_a = np.array([5, 4, 3, 2])test_numpy_b = np.array([4, 3, 2, 1])# 矩阵的加法test_numpy_c = test_numpy_a + test_numpy_b# 此时test_numpy_c为 array([9, 7, 5, 3])# 矩阵的减法test_numpy_d = test_numpy_a - test_numpy_b# 此时test_numpy_d为 array([1, 1, 1, 1])#矩阵的数乘test_numpy_e = test_numpy_a * 2# 此时的test_numpy_c为 array([10, 8, 6, 4])
阅读全文
0 0
- Python中Numpy库使用总结
- python中numpy使用
- Python中NumPy的使用
- Python:numpy中random使用
- python的numpy包中矩阵总结
- python中numpy的用法总结
- Python中numpy相关函数总结
- python中numpy库matrix和array的融合使用
- python库numpy的使用
- 【Python】NumPy库基本使用
- Python中Numpy矩阵的使用
- 在 Python 2.7 中使用 NumPy : Array
- Python中NumPy简介及使用举例
- 【Python】numpy中tile函数的使用
- 【Python】numpy中argsort函数的使用
- Python库--numpy学习笔记总结
- python numpy 总结
- eclipse中配置python以及numpy库
- Linux命令概述
- python爬虫selenium爬取开开贷黑名单
- 限流算法
- 【jQuery】jQuery 教程
- String Task_Codeforces_118A
- Python中Numpy库使用总结
- 【jQuery】jQuery 简介
- linux中安装软件,查看、卸载已安装软件方法
- 13
- 单模模块和多模模块的区别
- Hbase通过命令将数据批量导入的方法
- android 连接tftp 服务器
- bzoj 4455: [Zjoi2016]小星星
- 【jQuery】jQuery 安装