随笔分类 - Python
用Python来实现机器学习算法
摘要:参考文档: http://www.cnblogs.com/fengmk2/archive/2008/04/21/1163766.html; 解释: *args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个dict。并且同时使用*args和**kwargs时,必须*
阅读全文
摘要:解决方法: 修改/usr/local/lib/python2.7/site-packages/IPython/utils/terminal.py中的 from backports.shutil_get_terminal_size import get_terminal_size as _get_te
阅读全文
摘要:1 >>> a=mat(zeros((3,2)));2 >>> uniform(size=a.shape)3 array([[ 0.08886636, 0.37942544],4 [ 0.37711361, 0.3751705 ],5 [ 0.11307029, 0...
阅读全文
摘要:使用数组的reshape方法,可以创建一个改变了尺寸的新数组,原数组的shape保持不变; 1 >>> a = np.array([1, 2, 3, 4]);b = np.array((5, 6, 7, 8));c = np.array([[1, 2, 3, 4],[4, 5, 6, 7], [7,...
阅读全文
摘要:Python中怎样生成一个随机序列?代码例子如下:生成一个0-9的随机序列1 >>> from numpy.random import normal,random,uniform;2 >>> import numpy as np;3 >>> random.permutation(range(10))...
阅读全文
摘要:shuffle()方法将序列的所有元素随机排序。下面是语法:1 import random2 3 random.shuffle (lst )lst可以是序列或者元组; 1 >>> import random; 2 >>> indexList=[1,2,4,5,8,6]; 3 >>> indexLis...
阅读全文
摘要:1 >>> from tkinter import *;2 >>> root=Tk()3 >>> myLabel=Label(root,text="Hello world!");4 >>> myLabel.grid()5 >>> root.mainloop()先使用from tkinter impo...
阅读全文
摘要:1 # 12-1 FP树的类定义 2 class treeNode: 3 def _init_(self,nameValue,numOccur,parentNode): 4 self.name=nameValue; # 节点的名字 5 self.count=...
阅读全文
摘要:1 >>> a={1:'a',2:'b'}2 >>> 1 in a3 True4 >>> 'a' in a5 False6 >>> 2 in a7 True8 >>> 即可以判断某个键是否存在于字典中;1 >>> a.has_key(1)2 Traceback (most recent call l...
阅读全文
摘要:1 1 >>> line2=['10.235186', '11.321997']2 2 >>> line3=[list(map(lambda x:float(x),line2))];3 3 >>> line34 4 [[10.235186, 11.321997]]注意:1 >>> map(lambd...
阅读全文
摘要:1 >>> a=mat([[1,2,3],[5,6,9]]);2 >>> a3 matrix([[1, 2, 3],4 [5, 6, 9]])5 >>> shape(a)[0]6 27 >>> shape(a)[1]8 3如上面的a矩阵,2行3列;计算行使用shape(a)[0];计...
阅读全文
摘要:截取部分内容如下:10.235186 11.32199710.122339 11.8109939.190236 8.9049439.306371 9.8473948.330131 8.340352怎样将数据转化为矩阵?第一步使用open()函数打开文件:1 >>>...
阅读全文
摘要:截取部分内容如下:10.235186 11.32199710.122339 11.8109939.190236 8.9049439.306371 9.8473948.330131 8.340352怎样将数据转化为矩阵?第一步使用open()函数打开文件:1 >>>...
阅读全文
摘要:包含yield语句的函数会被特地编译为生成器函数;当函数被调用时,他们返回一个生成器对象,这个对象支持迭代器接口。函数也许会有个return语句,但它的作用是用来yield产生值的。举例如下: 1 >>> def g(n): 2 for i in range(n): 3 ...
阅读全文
摘要:首先看看矩阵中.A操作的结果 1 >>> a=mat([[1,2,3],[2,3,0]]); 2 >>> a 3 matrix([[1, 2, 3], 4 [2, 3, 0]]) 5 >>> a.A 6 array([[1, 2, 3], 7 [2, 3, 0]]) 8...
阅读全文
摘要:1 >>> a=mat([[1],[2],[3]]); 2 >>> b=mat([[0],[2],[3]]); 3 >>> a 4 matrix([[1], 5 [2], 6 [3]]) 7 >>> b 8 matrix([[0], 9 [2],10...
阅读全文
摘要:1 # 创建数据集,5*7的矩阵 2 def loadExData(): 3 return [[1,1,1,0,0], 4 [2,2,2,0,0], 5 [1,1,1,0,0], 6 [5,5,5,0,0], 7 ...
阅读全文
摘要:1 >>> from numpy import *; 2 >>> U,Sigma,VT=linalg.svd([[1,1],[]7,7]) 3 SyntaxError: invalid syntax 4 >>> U,Sigma,VT=linalg.svd([[1,1],[7,7]]) 5 >>> ...
阅读全文
摘要:如dict={1:'a',2:'b'},要得到一个字典dict={1:'a',2:'b',3:{1:'a',2:'b'}} 1 >>> dict={1:'a',2:'b'} 2 >>> dict 3 {1: 'a', 2: 'b'} 4 >>> dict[3]={1: 'a'} 5 >>> dict...
阅读全文
摘要:如字典dic={'a':1,'f':2,'c':3,'h':0};要对其进行排序:函数原型:sorted(dic,value,reverse);dic为比较函数;value为比较对象(键或值);reverse:注明升序还是降序,True--降序,False--升序(默认); 1 import ope...
阅读全文

浙公网安备 33010602011771号