Ray 学习笔记,温习 python: matrix

143 阅读1分钟

​本文已参与「新人创作礼」活动,一起开启掘金创作之路。

本系列是作者用 python 学习 Ray 框架的笔记。 Ray 是 UC berkley 提出的分布式机器学习。sklearn 是运行在单机上的机器学习,虽然支持多线程,但分布式并不支持。在开始学习Ray之前,先温习一下Python的基本知识。本文写于2021年3月12日。

1. Matrix:

a. 从array 生成 matrix , 请注意这种方式是多维数组,留意print type的命令行。type 还是ndarray。

g = np.array([
             np.array([1,3,4]),
             np.array([3,4,5])
             ])
print("g is {0}".format(g))
>g is [[1 3 4]
       [3 4 5]]
print("type of g is {0}".format(type(g)))
> type of g is <class 'numpy.ndarray'>
print("the shape of the matrix g is: {0}".format(g.shape))
> the shape of the matrix g is: (2, 3)

b. 从list 生成 matrix, 请留意下面的命令行输出,变量h的类型是 matrix,而不是ndarray .

h = np.matrix([[3,5,6],
               [4,8,9]])

print("h is {0}".format(h))
> h is [[3 5 6]
        [4 8 9]]

print("type of h is {0}".format(type(h)))
> type of h is <class 'numpy.matrix'>

print("the shape of the matrix h is: {0}".format(h.shape))
> the shape of the matrix h is: (2, 3)

2. Matrix 的操作

numpy 的transpose 命令对多维ndarray和matrix都有效,也就是说 transpose对 上面 a,b matrix的两种生成方式都是有效的。transpose 后,type 是不变的,多维数组还是多维数组,matrix还是matrix。

print("transpose of matrix g is: {0}".format(g.T))
> transpose of matrix g is: [[1 3]
                             [3 4]
                             [4 5]]

print("transpose of matrix h is: {0}".format(h.T))
> transpose of matrix h is: [[3 4]
                             [5 8]
                             [6 9]]