1.arange range 区别
>>> type(range(10))
<type 'list'>
>>> type(xrange(10))
<type 'xrange'>
可以看到,一个返回的list类型,一个返回的xrange类型(但我还没有明白xrange类型是什么,请大侠指教)在网上的查的一些资料:xrange性能好一些,对数据量大,表现明显,尽量用xrange
2.shape
>>> a*d
matrix([[ 3, 4, 5, 6, 7, 8],
[ 6, 8, 10, 12, 14, 16],
[ 9, 12, 15, 18, 21, 24],
[12, 16, 20, 24, 28, 32]])
>>> shape(a*d)
(4, 6)
3.数组操作[:,2]、[2,:]
取数组的第二列(首列为0列),返回一个数组
>>> (a*d)[:,2]
matrix([[ 5],
[10],
[15],
[20]])
>>> type(a*d)
<class 'numpy.matrixlib.defmatrix.matrix'>
同理:[2,:]则会返回第二行(首行为0行)4.python 拷贝,深拷贝、浅拷贝
import copy
a = [1, 2, 3, 4, ['a', 'b']] #原始对象
b = a #赋值,传对象的引用
c = copy.copy(a) #对象拷贝,浅拷贝
d = copy.deepcopy(a) #对象拷贝,深拷贝
a.append(5) #修改对象a
a[4].append('c') #修改对象a中的['a', 'b']数组对象
print 'a = ', a
print 'b = ', b
print 'c = ', c
print 'd = ', d
D:\coding\python>python 7_t.py
a = [1, 2, 3, 4, ['a', 'b', 'c'], 5]
b = [1, 2, 3, 4, ['a', 'b', 'c'], 5]
c = [1, 2, 3, 4, ['a', 'b', 'c']]
d = [1, 2, 3, 4, ['a', 'b']]
5.python科学技术-矩阵的运算
multiply(a,b) = [a1*b1,a2*b2,a3*b3.....]
>>> a.T
matrix([[1, 2, 3, 4]])
>>> b.T
matrix([[2, 3, 4, 5]])
>>> multiply(a.T,b.T)
matrix([[ 2, 6, 12, 20]])
import numpy as np
arr = np.arange(10)#创建拥有10个元素的数组
larr = arr.tolist()#转换list
arr = np.zeros((1,3,3))#创建n维数组
mat = np.matrix(arr)#将数组转换为矩阵,矩阵为3*3
alist = [1, 2, 3]
np.array(alist)#使用List创建数组
#创建
arr = np.arange(100)
arr = np.arange(10,100)#创建包含10~99数组
arr = np.linspace(1, 2, 100)#创建包含100个取值范围在1~2之间的数组
arr = np.logspace(0, 1, 100, base=10)#返回包含100个取值范围在10+[0~1]之间的数组
cube = np.zeros((5,5,5)).astype(int) + 1 #使用astype设置数据类型
cube = np.ones((5, 5, 5)).astype(np.float32)#创建3维数组,元素为1
#通过指定数据类型创建n维数组数组
arr = np.zeros(2, dtype=int)
arr = np.zeros(2, dtype=np.float32)
arr1d = np.arange(1000)#一维数组
arr3d = arr1d.reshape((10,10,10))#转换为3维数组
arr3d = np.reshape(arr1d, (10, 10, 10))
arr4d = np.zeros((10, 10, 10, 10))
arr1d = arr4d.ravel()#将4维数组转换为1维数组
recarr = np.zeros((2,), dtype=('i4,f4,a10'))#指定n维数组中每列的数据类型,2*3
col1 = np.arange(2) + 1
col2 = np.arange(2, dtype=np.float32)
col3 = ['Hello', 'World']
recarr[:]=zip(col1,col2,col3)#按列方式组装
#为每列命名
recarr.dtype.names = ('Integers' , 'Floats', 'Strings')
arr[0,1]#访问单个元?
arr[:,1]#访问第2列
arr[1,:]#访问第2行
arr = np.arange(7)
index = np.where(arr > 2)#查找>2的索引
new_arr = np.delete(arr, index)#删除index对应的元素
更多请参考:http://phddreamer.blog.163.com/blog/static/18993409620135271852137/
6.range
>>> range(1,5) #代表从1到5(不包含5)
[1, 2, 3, 4]
>>> range(1,5,2) #代表从1到5,间隔2(不包含5)
[1, 3]
>>> range(5) #代表从0到5(不包含5)
[0, 1, 2, 3, 4]
7.random
Python中的random模块用于生成随机数。下面介绍一下random模块中最常用的几个函数。
random.random
random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0
random.uniform
random.uniform的函数原型为:random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成的随机数n: a <= n <= b。如果 a <b, 则 b <= n <= a。
>>> random.uniform(10,20)
12.74479703401203
>>> random.uniform(30,20)
23.25193439153898
random.randint
random.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
>>> random.randint(20,10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mtrand.pyx", line 885, in mtrand.RandomState.randint (numpy\random\mtran
d\mtrand.c:6981)
ValueError: low >= high
>>> random.randint(5,10)
8
random.randrange
random.randrange的函数原型为:random.randrange([start], stop[, step]),从指定范围内,按指定基数递增的集合中 获取一个随机数。如:random.randrange(10, 100, 2),结果相当于从[10, 12, 14, 16, ... 96, 98]序列中获取一个随机数。random.randrange(10, 100, 2)在结果上与 random.choice(range(10, 100, 2) 等效。
random.choice
random.choice从序列中获取一个随机元素。其函数原型为:random.choice(sequence)。参数sequence表示一个有序类型。这里要说明 一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。list, tuple, 字符串都属于sequence。有关sequence可以查看python手册数据模型这一章。下面是使用choice的一些例子:
>>> print random.choice(("Tuple", "List", "Dict"))
List
>>> print random.choice(["JGood", "is", "a", "handsome", "boy"])
is
random.shuffle
random.shuffle的函数原型为:random.shuffle(x[, random]),用于将一个列表中的元素打乱。如:
>>> p
['powerful', 'Python', 'and so on...', 'is', 'simple']
>>> random.shuffle(p)
>>> p
['Python', 'and so on...', 'is', 'simple', 'powerful']
8.tile、zeros函数
>>> a
matrix([[1],
[2],
[3],
[4]])
>>> tile(a,2) #在列方向上重复[0,0]1次,默认行1次
matrix([[1, 1],
[2, 2],
[3, 3],
[4, 4]])
>>> tile(a,(2,3)) #在行方向上重复a 2次,列3次
matrix([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4]])
>>> zeros((2,2,3))
array([[[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.]]])
9.python 调试方法
D:\coding\python>python -m pdb 8_t.py
> d:\coding\python\8_t.py(13)<module>()
-> '''
(Pdb)