>

LoeiJe

:D 获取中...

何以解忧?唯有暴富

numpy学习笔记

numpy学习笔记

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 2019-10-25 不更新时间了
# numpy 学习笔记
# icenaive
# 参考: https://github.com/lijin-THU/notes-python
# 仅供个人学习使用
#
# 14. 向量化函数

import numpy as np

def sinc(x):
if x == 0.0:
return 1.0
else :
w = np.pi * x
return np.sin(w) / w

print(sinc(0.0), sinc(3.0))


# 这个函数不能够作用与数组
x = np.array([1, 2, 3])
# print(sinc(x))

# 使用numpy的 vectorize 将函数sinc 向量化,产生一个新的函数
vsinc = np.vectorize(sinc)
print(vsinc(x))
# 作用相当于为x中的每个值调用sinc函数

import matplotlib.pyplot as plt
x = np.linspace(-5, 5, 101)
plt.plot(x, vsinc(x))
plt.show()

# 因为这样的用法涉及大量的函数调用,因此,向量化函数的效率并不高。