Python基础、函数及其相关函数库(Numpy、TensorFlow)常用函数集锦(更新中,更新日期:2017-08-03)
2017-06-09 14:23
746 查看
更新记录: 2017-07-09 2017-07-12 2017-07-13 2017-07-19 2017-08-03
Python基础
1.基本操作Python里的元素是从0开始编号
从某个元素开始取数字
>>>a=[1,2,3];b=a[1:];b [2, 3]
取某个元素之前的数字
>>>a=[1,2,3];b=a[:-1];b [1,2]
说明:B是从A的倒数第二个数字取前面的数字。
取倒数第一个元素
>>>a=[1,2,3];b=a[-1];b 3
取某一段元素
>>>a=[1,2,3,4] >>>b=a[0:2] >>>b [1,2]
说明:取值时左闭、右开
扩展列表
>>>w=[2]*5 >>>w [2,2,2,2,2]
2.移动数据
3.计算数据
4.控制语句
5.条件语句
if
def sign(self,x): if (x >= 0): logic=1 else: logic=0 return logic
while
count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!"
Python函数
参考资料:1.Python基础教程
2.Python学习与分享平台
3.Python 3.6.2rc2 documentation
len
>>>n=len([1,2,3,4,5,6]);n 6
>>>n=len([(1,2),(3,4),(5,6)]);n 3
>>>n=len(1,2,3,4,5,6) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: len() takes exactly one argument (6 given)
>>>n=len([1,2,3],[4,5,6]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: len() takes exactly one argument (2 given)
zip
>>>zip([1,2],[3,4]) [(1,3),(2,4)]
xrange
>>>t=[(1,2),(3,4),(5,6),(7,8)] >>>m=[t[k:k+2] for k in xrange(0,4,2)] >>>m [[(1, 2), (3, 4)], [(5, 6), (7, 8)]] >>>m[0] [(1, 2), (3, 4)]
.append
>>>a=[1,2] >>>a.append(3) >>>a a=[1,2,3] >>>a.append([3]) >>>a [1,2,3,[3]]
random.randint(a, b)
Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).Numpy函数
参考资料:NumPy Index
在Python中引入NumPy模块:
import numpy as np
np.arange
>>>a=np.arange(3) >>>a array([0, 1, 2]) >>>b=np.arange(3,7) >>>b array([3,4,5,6]) >>>c=np.arange(3.0) >>>c array([0., 1., 2.]) >>>d=np.arange(3,7,2) >>>d array([3,5]) >>>f=np.arange(0.0,5.0) >>>f array([ 0., 1., 2., 3., 4.])
np.random.randn
功能:生成随机数组代码:
>>>a=np.random.randn(9) >>>a array([-1.49333996, 0.52417595, 0.34511317, 0.72437468, -2.04038084, -1.0797781 , -0.69342441, -2.33804615, 1.66226234])
>>> a=np.random.randn(3,3) >>> a array([[ 1.45463641, 1.13425746, -0.45356713], [-0.14581477, -0.04591037, -1.699357 ], [ 0.31431015, -2.24838076, -0.69609836]])
>>>a=[np.random.randn(3,3)] >>>a [array([[-0.68384773, 1.16566546, 1.79952596], [-1.06512186, -0.4309544 , 0.14547754], [ 0.31870122, 0.05401874, 1.98810746]])]
>>> a=[np.random.randn(k,3) for k in [1,3]] >>> a [array([[-1.58348361, -1.36025393, -2.35910297]]), array([[-0.43975155, 0.40332687, 0.17635562], [-1.83589753, 0.09945764, -0.11786595], [ 1.17420373, 0.29638316, -0.33675276]])]
np.dot
功能:一维则进行点乘代码:
>>> np.dot([1,2,3],[4,5,6]) 32
功能:二维则矩阵相乘
代码:
>>> a=[[1,2],[3,4],[5,6]] >>> b=[[1,2,3],[4,5,6]] >>> np.dot(a,b) array([[ 9, 12, 15], [19, 26, 33], [29, 40, 51]])
说明:a为3*2矩阵,b为2*3矩阵,结果为3*3矩阵。
np.shape
功能:测量数组形状代码:
>>> a=np.array([(1,2),(3,4),(5,6)]) >>> a.shape (3,2)
np.reshape
>>>a = np.arange(6) >>>a array([0,1,2,3,4,5]) >>>b = np.reshape(a,(2,3)] array([[1,2,3], [4,5,6]])
>>>a=[1,2,3,4] >>>b =np.reshape(a,(2,2)) >>>b array([[1,2], [3,4]])
np.ravel()
功能:一维度化>>> x = np.array([[1, 2, 3], [4, 5, 6]]) >>> print(np.ravel(x)) [1 2 3 4 5 6]
np.zeros
功能:数组初始化为0代码:
>>> a=np.zeros((3,2)) >>> a array([[ 0., 0.], [ 0., 0.], [ 0., 0.]])
np.argmin
功能:找到最小值得位置代码:
>>> a=[5,1,2,3] >>> np.argmin(a) 1 >>> b=[[4,2,3],[1,2,3]] >>> np.argmin(b) 3
np.exp
>>>a=np.exp(2) >>>a 7.3890560989306504
TensorFlow
tf.Variable
W = tf.Variable([.3],dtype=tf.float32)
说明:用tf.zeros()、tf.ones()等函数也是创建变量并初始化。
tf.reshape
# coding: utf-8 # In[2]: import tensorflow as tf import numpy as np # In[3]: a = [1,2,3,4,5,6,7,8,9] # In[5]: b = tf.reshape(a, [3,3]) # In[6]: sess = tf.Session() # In[7]: print(sess.run(b)) # In[ ]:
相关文章推荐
- Python及其接口函数库(TensorFlow、NumPy、Matplotlib、sklearn、pandas)参考资料(更新中,更新日期:2017-07-10)
- TensorFlow常用函数集锦(更新中)
- python 中 常用到的 numpy 函数 整理
- Python学习入门基础教程(learning Python)--8.3 字典常用的方法函数介绍
- numpy库常用函数记录(不断更新)
- 2、Python基础--除法、常用数学函数
- 基础的prototype.js常用函数及其用法
- Python 相关常用重要链接收集(不断更新)
- 基础知识(八)python文件操作相关函数
- Python-老男孩-01_基础_文件IO_函数_yield_三元_常用内置函数_反射_random_md5_序列化_正则表达式_time
- python学习之1 numpy常用的函数
- 常用距离算法和相关系数及其Python实现
- 基础篇1:LoadRunner中常用函数参考手册(不断更新中)
- python 中 常用到的 numpy 函数 整理
- Oracle中的日期类型及其相关的函数
- Python修炼--常用字符串、数学函数 分类: python基础学习 2013-06-16 13:23 205人阅读 评论(0) 收藏
- C/C++常见无法解析外部函数及其对应Lib汇总 (最后更新日期 17 Nov 2010)
- python中常用检测字符串相关函数汇总
- python 中 常用到的 numpy 函数 整理
- Python常用小函数及特点(持续更新)