Python NumPy基础入门

Python NumPy基础入门,第1张

一、NumPy简介
  • NumPy是高性能科学计算和数据分析的基础包。它是pandas等其他各种工具的基础
  • NumPy的主要功能:
    • ndarray,一个多维数据结构,高效且节省空间
    • 无需循环对整组数据进行快速运算的数学函数
    • 线性代数、随机数生成和傅里叶变换功能
  • 安装方法:pip install numpy
二、初体验
1、案例一

已知若干家跨国公司的市值(美元),将其换算成人名币

# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np
import random

a = [random.uniform(100.0, 200.0) for i in range(3)]
print(a)
b = np.array(a)
c = b * 6.8
print(c, c[1])

结果:

[129.70799881689956, 115.85222551317838, 161.37833819727018]
[ 882.01439195  787.79513349 1097.37269974] 787.795133489613
2、案例二

已知购物车中每件商品的价格与商品件数,求总金额

# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np
import random

a = [random.uniform(10.0, 20.0) for i in range(3)]  # 金额
a = np.array(a)
print(a)
b = [random.randint(1, 10) for i in range(3)]  # 件数
b = np.array(b)
print(b)
print(a * b, (a*b).sum())

结果:

[18.21776421 13.63878433 13.37232689]
[9 3 7]
[163.95987791  40.916353    93.60628826] 298.48251916984424
三、ndarray(多维数组对象)
1、简介
  • 创建ndarray:np.array(array_like)
  • 数组与列表的区别:
    • 数组对象内的元素类型必须相同
    • 数组大小不可修改
2、常用属性

# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a)
b = np.array(range(10))
print(b)
print(b.size, b.ndim, b.shape, b.dtype)
print('------------------\n')
c = np.array([[1, 2, 3], [4, 5, 6]])
print(c)
print(c.T, c.size, c.ndim, c.shape, c.dtype)

结果:

[1 2 3 4 5]
[0 1 2 3 4 5 6 7 8 9]
10 1 (10,) int64
------------------

[[1 2 3]
 [4 5 6]]
[[1 4]
 [2 5]
 [3 6]] 6 2 (2, 3) int64
3、数据类型

4、numpy创建

# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np

print(np.array([0] * 10))
print(np.zeros(10), np.zeros(10, dtype='int'), np.ones(10))  # 默认float64
print("empty: ", np.empty(3))
print("arange: ", np.arange(1, 5), np.arange(1, 5, 0.3))
print("linspace: ", np.linspace(0, 3, 4))
print("eye:\n", np.eye(5))

结果:

[0 0 0 0 0 0 0 0 0 0]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0 0 0 0 0 0 0 0 0 0] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
empty:  [6.94190175e-310 6.94190175e-310 1.28822032e-057]
arange:  [1 2 3 4] [1.  1.3 1.6 1.9 2.2 2.5 2.8 3.1 3.4 3.7 4.  4.3 4.6 4.9]
linspace:  [0. 1. 2. 3.]
eye:
 [[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.]]
5、ndarray批量运算 (1)数组与标量
# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np

a = np.arange(1, 5)
print(a)
print(a + 1, a * 3, 1 // a, a ** 2, a > 2)

结果:

[1 2 3 4]
[2 3 4 5] [ 3  6  9 12] [1 0 0 0] [ 1  4  9 16] [False False  True  True]
(2)同样大小数组与数组
# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np

a = np.arange(1, 5)
b = np.arange(4, 8)
print(a, b)
print(a + b, b / a, b ** a, b % a, a == b)

结果:

[1 2 3 4] [4 5 6 7]
[ 5  7  9 11] [4.   2.5  2.   1.75] [   4   25  216 2401] [0 1 0 3] [False False False False]
四、索引与切片
1、ndarray索引
# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np

a = np.arange(1, 5)
print(a, a[0])
b = np.arange(1, 7).reshape((2, 3))
print(b, b[0][0], b[0, 0])

结果:

[1 2 3 4] 1
[[1 2 3]
 [4 5 6]] 1 1
2、ndarray切片

注意:array切片没有复制对象

# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np

a = np.arange(1, 5)
print(a, a[0:2], a[:2], a[2:])
print('--------------------')
b = [1, 2, 3, 4]
c = a[:2]  # 如果不想修改a[:2].copy()
d = b[:2]
c[0] = 10
d[0] = 10
print(a, b)
print('--------------------')
e = np.arange(1, 7).reshape((2, 3))
print(e)
print(e[0:2, 0:2])

结果:

[1 2 3 4] [1 2] [1 2] [3 4]
--------------------
[10  2  3  4] [1, 2, 3, 4]
--------------------
[[1 2 3]
 [4 5 6]]
[[1 2]
 [4 5]]
五、布尔型索引
# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np

a = np.arange(1, 5)
print(a, a[a > 2], a > 2)
# 取出偶数
print(a[a % 2 == 0], a[(a > 1) & (a % 2 == 0)])
print(a[(a > 3) | (a % 2 == 0)])   # 或

结果:

[1 2 3 4] [3 4] [False False  True  True]
[2 4] [2 4]
[2 4]
六、花式索引
# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np

a = np.arange(20, 40)
print(a)
print(a[[1, 4, 5, 6]])
print("----------------")
b = np.arange(20, 40).reshape(4, 5)
print(b)
print(b[0, 2:4], b[0, b[0] > 22])

# 特殊取法
print("----------------")
print(b[[1, 3], :][:, [1, 3]])

结果:

[20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]
[21 24 25 26]
----------------
[[20 21 22 23 24]
 [25 26 27 28 29]
 [30 31 32 33 34]
 [35 36 37 38 39]]
[22 23] [23 24]
----------------
[[26 28]
 [36 38]] [[26 28]
 [36 38]]
七、通用函数
1、简介
  • 通用函数:能同时对数组中所有元素进行运算的函数

  • 常见通用函数:

    • 一元函数:abs,sqrt,exp,log,ceil,floor,rint,trunc,modf,isnan,isinf,cos,sin,tan
    • 二元函数:add,sbustract,multiply,divide,power,mod,maximum,mininum
  • 补充:

    • nan(Not a Number):不等于任何浮点数(nan != nan)
    • inf(infinity):比任何浮点数都大
    • NumPy中创建特殊值:np.nan np.inf
    • 在数据分析中,nan常被用作表示数据缺失值
2、案例
# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np

a = np.arange(-2.5, 2.5)
b = np.array([4, 16])
print(a, abs(a), np.abs(a), np.sqrt(b))

# floor: 往小取整 ceil: 往大取整 round,rint: 四舍五入 trunc: 去掉小数位
print(np.floor(a), np.ceil(a), np.round(a), np.trunc(a))

# modf: 整数和小数分开
print(np.modf(a))

结果:

[-2.5 -1.5 -0.5  0.5  1.5] [2.5 1.5 0.5 0.5 1.5] [2.5 1.5 0.5 0.5 1.5] [2. 4.]
[-3. -2. -1.  0.  1.] [-2. -1. -0.  1.  2.] [-2. -2. -0.  0.  2.] [-2. -1. -0.  0.  1.]
(array([-0.5, -0.5, -0.5,  0.5,  0.5]), array([-2., -1., -0.,  0.,  1.]))
八、统计方法
1、简介
sum  求和            min 求最小值
mean 求平均数         max 求最大值
std  求标准差         argmin 求最小值索引
var  求平方差         argmax 求最大值索引
2、案例
# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np

a = np.array([4, 3, 5, 6])
print(a.sum(), a.mean(), a.std(), a.var())
print(a.min(), a.max(), a.argmin(), a.argmax())

结果:

18 4.5 1.118033988749895 1.25
3 6 1 3
九、随机数生成
1、简介
rand        给定形状产生随机数组(0到1之间的数)
randint     给定形状产生随机整数
choice      给定形状产生随机选择
shuffle     与random.shutffle相同,打乱
uniform     给定形状产生随机数组
2、案例
# !/usr/bin/env python
# coding:utf-8
"""
 Name: main.py
 Author: WieLun
 Time: 2022/3/28
 Descript:

 """
import numpy as np

a = np.array([4, 3, 5, 6])
print(np.random.rand(2))
print(np.random.randint(2, 5))
print(np.random.choice([1, 2, 3], 3))
li = [4, 2, 5, 1]
np.random.shuffle(li)
print(li)
print(np.random.uniform(2.0, 4.0, 3))

结果:

[0.72368355 0.55070405]
2
[2 3 1]
[2, 4, 1, 5]
[3.08668595 2.13194105 2.28684747]

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/713982.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-24
下一篇 2022-04-24

发表评论

登录后才能评论

评论列表(0条)

保存