一,关于矩阵的一些基本命令
1,生成一个3*4的元素全为0的Python数组:
>>> a = zeros((3,4))
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
2,把上面的数组变成矩阵:
>>> mat(a)
matrix([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
matrix([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
3,生成一个3*4的元素全为0的Python数组:
>>> b=ones((3,4))
>>> b
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]])
>>> b
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]])
4, >>>c= 2*b
array([[ 2., 2., 2., 2.],
[ 2., 2., 2., 2.],
[ 2., 2., 2., 2.]])
array([[ 2., 2., 2., 2.],
[ 2., 2., 2., 2.],
[ 2., 2., 2., 2.]])
5, >>> d=c**3
>>> d
array([[ 8., 8., 8., 8.],
[ 8., 8., 8., 8.],
[ 8., 8., 8., 8.]])
>>> d
array([[ 8., 8., 8., 8.],
[ 8., 8., 8., 8.],
[ 8., 8., 8., 8.]])
6,tile(A, (3,4)) 把 A作为元素生成一个3*4的python数组
>>> tile(2, (3,4))
array([[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]])
array([[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]])
>>> tile( d, (3,4))
array([[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.]])
array([[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.,
8., 8., 8.]])
6,读取文件并写入矩阵。参考教材。P21
7,用matplotlib绘制散点图。参考教材。P23
8,看如下语法:
>>> myDat
[[1, 1, 'yes'], [1, 1, 'yes'], [1, 0, 'no'], [0, 1, 'no'], [0, 1, 'no']]
[[1, 1, 'yes'], [1, 1, 'yes'], [1, 0, 'no'], [0, 1, 'no'], [0, 1, 'no']]
>>> featList = [example[1] for example in myDat]
>>> featList
[1, 1, 0, 1, 1]
>>> featList
[1, 1, 0, 1, 1]
>>> d = set(featList)
>>> d
set([0, 1])
>>> d
set([0, 1])
>>> featList = [example[2] for example in myDat]
>>> featList
['yes', 'yes', 'no', 'no', 'no']
>>> featList
['yes', 'yes', 'no', 'no', 'no']
>>> d = set(featList)
>>> d
set(['yes', 'no'])
>>> d
set(['yes', 'no'])
可以看出,[example[2] for example in myDat] 表示由 myDat中每一条数据的第2+1个元素组成的小数组.
set(featList) 表示由featList中不同的元素组成的集合。
9,用matplotlib绘制树形图。参考教材。P43
10,用matplotlib绘制一条直线。参考教材,P79
11,呵呵