|
1 | | -#!/usr/bin/env python2.7 |
2 | | -# -*- encoding:utf-8 -*- |
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | + |
3 | 4 | """ |
4 | 5 | Numpy中实现基本数学计算脚本 |
5 | 6 | Created on 2017-11-16 |
6 | | -@author: denglelai@baidu.com |
7 | | -@copyright: www.baidu.com |
| 7 | +author: denglelai |
8 | 8 | """ |
9 | 9 | import numpy as np |
10 | 10 |
|
11 | 11 |
|
12 | | -# 绝对值,1 |
13 | | -a = np.abs(-1) |
14 | | -print "-1的绝对值为:" + str(a) |
| 12 | +def main(): |
| 13 | + """ |
| 14 | + run basic operations of numpy |
| 15 | + """ |
| 16 | + |
| 17 | + # 绝对值,1 |
| 18 | + a_variable = np.abs(-1) |
| 19 | + print "-1的绝对值为:" + str(a_variable) |
| 20 | + |
| 21 | + # sin函数,1.0 |
| 22 | + a_variable = np.sin(np.pi / 2) |
| 23 | + print "pi/2的正弦值为:" + str(a_variable) |
| 24 | + |
| 25 | + # tanh逆函数,0.500001071578 |
| 26 | + a_variable = np.arctanh(0.462118) |
| 27 | + print "tanh(0.462118)值为:" + str(a_variable) |
15 | 28 |
|
16 | | -# sin函数,1.0 |
17 | | -b = np.sin(np.pi / 2) |
18 | | -print "pi/2的正弦值为:" + str(b) |
| 29 | + # e为底的指数函数,20.0855369232 |
| 30 | + a_variable = np.exp(3) |
| 31 | + print "e的3次方值为:" + str(a_variable) |
19 | 32 |
|
20 | | -# tanh逆函数,0.50000107157840523 |
21 | | -c = np.arctanh(0.462118) |
22 | | -print "tanh(0.462118)值为:" + str(c) |
| 33 | + # 2的3次方,8 |
| 34 | + a_variable = np.power(2, 3) |
| 35 | + print "2的3次方值为:" + str(a_variable) |
23 | 36 |
|
24 | | -# e为底的指数函数,20.085536923187668 |
25 | | -d = np.exp(3) |
26 | | -print "e的3次方值为:" + str(d) |
| 37 | + # 点积,1*3+2*4=11 |
| 38 | + a_variable = np.dot([1, 2], [3, 4]) |
| 39 | + print "向量[1. 2]与向量[3. 4]点乘值为:" + str(a_variable) |
27 | 40 |
|
28 | | -# 2的3次方,8 |
29 | | -f = np.power(2, 3) |
30 | | -print "2的3次方值为:" + str(f) |
| 41 | + # 开方,5 |
| 42 | + a_variable = np.sqrt(25) |
| 43 | + print "25的2次方根值为:" + str(a_variable) |
31 | 44 |
|
32 | | -# 点积,1*3+2*4=11 |
33 | | -g = np.dot([1, 2], [3, 4]) |
34 | | -print "向量[1. 2]与向量[3. 4]点乘值为:" + str(g) |
| 45 | + # 求和,10 |
| 46 | + a_variable = np.sum([1, 2, 3, 4]) |
| 47 | + print "对[1, 2, 3, 4]中元素求和结果为:" + str(a_variable) |
35 | 48 |
|
36 | | -# 开方,5 |
37 | | -h = np.sqrt(25) |
38 | | -print "25的2次方根值为:" + str(h) |
| 49 | + # 平均值,5.5 |
| 50 | + a_variable = np.mean([4, 5, 6, 7]) |
| 51 | + print "对[1, 2, 3, 4]中元素求平均结果为:" + str(a_variable) |
39 | 52 |
|
40 | | -# 求和,10 |
41 | | -l = np.sum([1, 2, 3, 4]) |
42 | | -print "对[1, 2, 3, 4]中元素求和结果为:" + str(l) |
| 53 | + # 标准差,0.968245836552 |
| 54 | + a_variable = np.std([1, 2, 3, 2, 1, 3, 2, 0]) |
| 55 | + print "对[1, 2, 3, 2, 1, 3, 2, 0]中元素求标准差结果为:" + str(a_variable) |
43 | 56 |
|
44 | | -# 平均值,5.5 |
45 | | -m = np.mean([4, 5, 6, 7]) |
46 | | -print "对[1, 2, 3, 4]中元素求平均结果为:" + str(m) |
47 | 57 |
|
48 | | -# 标准差,0.96824583655185426 |
49 | | -p = np.std([1, 2, 3, 2, 1, 3, 2, 0]) |
50 | | -print "对[1, 2, 3, 2, 1, 3, 2, 0]中元素求标准差结果为:" + str(p) |
| 58 | +if __name__ == '__main__': |
| 59 | + main() |
0 commit comments