载入数据和保存数据
In [34]: arr = np.loadtxt("/home/hadoop/wujiadong/np.txt")
In [35]: np.save("/home/hadoop/wujiadong/np_out.txt",arr)
数学运算
nump.dot:矩阵乘法运算
numpy.transponse:矩阵转置
求解方程组
In [39]: a = np.array([[3,6,-5],[1,-3,2],[5,-1,4]])
In [40]: b = np.array([12,-2,10])
In [41]: x = np.linalg.inv(a).dot(b)
In [42]: x
Out[42]: array([ 1.75, 1.75, 0.75])
In [43]: a
Out[43]:
array([[ 3, 6, -5],
[ 1, -3, 2],
[ 5, -1, 4]])
In [44]: b
Out[44]: array([12, -2, 10])
In [61]: d1 = np.array([[1,2,3,4],[5,6,7,8]])
In [62]: d1
Out[62]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [63]: d2 = np.random.random((4,2))
In [64]: d2
Out[64]:
array([[ 0.33936881, 0.02394269],
[ 0.55151109, 0.70094196],
[ 0.04284826, 0.80405587],
[ 0.59282838, 0.94140771]])
In [65]: d1.dot(d2)
Out[65]:
array([[ 3.94224929, 7.60362506],
[ 10.04847544, 17.48501797]])
In [66]: d2.T
Out[66]:
array([[ 0.33936881, 0.55151109, 0.04284826, 0.59282838],
[ 0.02394269, 0.70094196, 0.80405587, 0.94140771]])
In [68]: np.linalg.inv(d2.T.dot(d2))
Out[68]:
array([[ 3.43382891, -1.67438636],
[-1.67438636, 1.31036906]])
##对矩阵d2进行奇异值分解 :d2=u*s*h
In [70]: u,s,h = np.linalg.svd(d2)
In [71]: u
Out[71]:
array([[-0.11515737, 0.59645451, 0.74476033, -0.27624781],
[-0.54915702, 0.30352265, -0.53710331, -0.5637558 ],
[-0.45256189, -0.73026033, 0.3934938 , -0.32721586],
[-0.69307509, 0.13724381, 0.04488564, 0.70625514]])
In [72]: s
Out[72]: array([ 1.60238234, 0.47920264])
In [73]: h
Out[73]:
array([[-0.48191562, -0.87621763],
[ 0.87621763, -0.48191562]])