NumPy入门

作者: Leoshi | 来源:发表于2017-04-01 09:56 被阅读134次

Numpy是Python科学计算中最常用的工具,包括SciPy以及Pandas等工具也有对NumPy的依赖,因此Python科学计算应当从NumPy开始。

ndarray

ndarray是NumPy的核心数据类型,包括Pandas的DataFrame类型也与它有互转的方法。按其名称ndarray来看用的比较多的是矩阵运算。

创建

# coding:utf8
import numpy as np

a = np.array([1, 2, 3, 4])  # 使用list初始化
b = np.array((5, 6, 7, 8))  # 使用tuple初始化
c = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])  # 使用嵌套list初始化
print type(a)   # <type 'numpy.ndarray'>
print b.shape   # (4,)
print c.shape   # (2, 4)

print c
# [[1 2 3 4]
#  [5 6 7 8]]
c.shape = (4, 2)    # 更改shape属性 也可指定为(4, -1) or (-1, 2)
print c
print c.dtype   # int64
# ndarray在内存中连续存储,更改shape之后矩阵也随之更改
# [[1 2]
#  [3 4]
#  [5 6]
#  [7 8]]

d = a.reshape(2, 2)
# 此时a与d共享内存空间
print d

t = a.astype(np.float64)    # 转换类型
print t

# float64 的别名
print [key for key, value in np.typeDict.items() if value is np.float64]

自动生成数组

# coding:utf8
import numpy as np
# 数组的自动生成

# 0到1(不包括)以0.1为步长生成数组
a = np.arange(0, 1, 0.1)
print a
# [ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9]
print type(a)
# <type 'numpy.ndarray'>

# 0到1(使用endpoint确定是否包括,默认True)取10个数
b = np.linspace(0, 1, 10, endpoint=False)
print b
# [ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9]
c = np.linspace(0, 1, 10)
print c
# [ 0.          0.11111111  0.22222222  0.33333333  0.44444444  0.55555556
#  0.66666667  0.77777778  0.88888889  1.        ]

# 10**0 到 10**2 取等比3个
d = np.logspace(0, 2, 3, endpoint=True, base=10.0)
print d
# [   1.   10.  100.]
print d.dtype   # float64

e = np.zeros((2, 3), dtype='float')
print e
# [[ 0.  0.  0.]
#  [ 0.  0.  0.]]

f = np.ones((2, 3), dtype='int')
print f
# [[1 1 1]
#  [1 1 1]]

# 未初始化内存
g = np.empty((2, 4))
print g
# [[  6.90359047e-310   6.90359047e-310   1.58101007e-322   3.16202013e-322]
#  [  0.00000000e+000   1.17133162e-316   1.17133162e-316   1.17133162e-316]]

# 填充
h = np.full((2, 3), 6.0)
print h
# [[ 6.  6.  6.]
#  [ 6.  6.  6.]]

s = 'abcd'
i = np.fromstring(s, dtype=np.int8)
print i
# [ 97  98  99 100]

# 通过函数创建数组 这里输出乘法表
j = np.fromfunction(lambda x, y: (x + 1) * (y + 1), (9, 9), dtype=int)
print j

存取元素

# coding:utf8
import numpy as np

# 存取元素
a = np.arange(0, 10, 1)
print a
# [0 1 2 3 4 5 6 7 8 9]

# 切片
print a[3:5]
# [3, 4]    与list切片一致
print a[:5]
# [0 1 2 3 4] 省略0
print a[::-1]
# [9 8 7 6 5 4 3 2 1 0] 逆
print a[5:1:-2]
# [5 3]
a[2:4] = 100, 101
# 更改
print a
# [  0   1 100 101   4   5   6   7   8   9]

# 高级用法
b = a[[1, 3, 6, 8]]  # 取a的第1,2,6,8组成新的,不共享内存
c = a[np.array([[1, 3, 6, 8], [2, 5, 7, -3]])]
d = a[[1, 3, 6, 8, 2, 5, 7, -3]].reshape(2, 4)
print c == d    # 2*4 的 True
# 判断
print a[a < 7]  # [0 1 4 5 6]

e = np.arange(5, 0, -1)
# 长度一致,取True对应元素
print e[np.array([True, False, True, False, False])]
# [5, 3]

# randint(low, high, size, dtype)
f = np.random.randint(0, 10, 4)
print f

ufunc

# coding:utf8
import numpy as np

# ufunc
# 所谓的ufunc即对array每个数值进行操作的函数

a = np.linspace(0, 2*np.pi, 10)
print a
# 对a的每个值进行sin操作,输出也可指定为a
b = np.sin(a, out=a)
print a == b    # 1*10 True

c = np.arange(6.0).reshape((2, 3))
print c
print c.item(1, 2)  # 取c的1行2列(始于0行0列)

# 四则
d = np.arange(10)
e = np.arange(10, 0, -1)
f = np.arange(5)
print d + e
# print e + f     # err
print np.add(d, e)
# np.add(x1, x2, out=x1)
# np.subtract() np.multiply()
# np.divide() np.true_divide() np.floor_divide()
# np.negative() np.power() np.mod() np.remainder()
# np.equal() np.not_equal() np.less() np.greater()
# np.logical_and() or not xor

函数库(常用部分)

# coding: utf8
import numpy as np
# 函数库
# 随机数
print np.random.rand(3, 4)
print np.random.randn(3, 4)

# 统计
a = np.random.randint(0, 10, (3, 4))
print a
print a.sum()
print a.sum(keepdims=True)  # 保持维数
print a.var()
print a.mean()
print a.std()
print a.prod()
print np.average(a)
# 大小与排序
print a.max()
print a.min()
print a.argmin()    # 最小值下标
print a.sort()
print np.median(a)

相关文章

  • Numpy简易入门笔记

    来自 AI基础:Numpy简易入门手动运行了一遍实例代码,笔记待查于此处。 Numpy 简易入门 Numpy是 N...

  • numpy 学习(待更新)

    numpy 学习 标签(空格分隔): 机器学习 Numpy 入门 一、安装 pip install numpyor...

  • Numpy

    1.numpy.tile(A,B)函数,实例验证 快速入门 Numpy[https://mp.weixin.qq....

  • NumPy之 索引技巧

    系列文章 一次性搞定NumPy入门基础知识NumPy之操控ndarray的形状NumPy之浅拷贝和深拷贝NumPy...

  • Numpy语法入门(一)

    1 Numpy简易入门 1.1 认识Numpy数组对象 1.1.1 np.arange In [1]: Out[1...

  • numpy-n2

    [TOC]说明:本文是numpy入门的第二篇笔记。 numpy的智能切片 numpy提供了比原始python强大的...

  • Python编程&数据科学入门 Lesson4

    第四课 - NumPy 入门 本课内容: 0. 导入 NumPy 包 1. 创建 NumPy 数组 2. 索引和切...

  • Numpy 学习图谱

    在学习 Numpy 的时候,整理了一份 Numpy 学习图谱,希望同样可以帮助到想要入门 Numpy 的朋友。 N...

  • 【Chapter 4】 NumPy基础:数组和矢量计算

    【Chapter 4】 NumPy基础:数组和矢量计算 使用 Python 进行科学计算:NumPy入门 NumP...

  • 对 NumPy.dot() 的理解

    【对 Numpy.dot() 的理解】 我看了一下 「使用 Python 进行科学计算:NumPy入门 」 这个教...

网友评论

    本文标题:NumPy入门

    本文链接:https://www.haomeiwen.com/subject/qnqrnttx.html