本文已参与「新人创作礼」活动,一起开启掘金创作之路。
本系列是作者用 python 学习 Ray 框架的笔记。 Ray 是 UC berkley 提出的分布式机器学习。sklearn 是运行在单机上的机器学习,虽然支持多线程,但分布式并不支持。在开始学习Ray之前,先温习一下Python的基本知识。本文写于2021年3月12日。
1. Array
a = np.ones(3) 可以生成array。
a = np.ones(3)
print("a is {0}".format(a))
> a is [1. 1. 1.]
print("type of a is {0}".format(type(a)))
> type of a is <class 'numpy.ndarray'>
或者:
b = np.array([2,3,4])
b = np.array([3,4,6])
print("b is {0}".format(b))
> b is [3 4 6]
print("type of b is {0}".format(type(b)))
> type of b is <class 'numpy.ndarray'>
变量b是array,是从list [ 2, 3, 4] 转换而来,array 不能直接通过以下方式生成。
a1 = [1.0 1.2 1.4]
2. list
array 可以转换成为 list,也可以直接生成list。请注意 array 和 list 的区别。在print命令中,array的元素分隔号是空格,list的元素分隔号是逗号。array 和 list 是两种不同的变量。有些python的框架的运算只支持一种变量,比如 list。所以区分 array 和 list 很重要。
c = np.ones(3).tolist()
c = np.ones(3).tolist()
print("c is {0}".format(c))
> c is [1.0, 1.0, 1.0]
print("type of c is {0}".format(type(c)))
>type of c is <class 'list'>
或者直接:
d = [6, 9, 10]
d = [6,8,9]
print("d is {0}".format(d))
> d is [6, 8, 9]
print("type of d is {0}".format(type(d)))
> type of d is <class 'list'>
3. array 和 list 的转换
# array 转换为 list
e = np.ones(3).tolist()
print("e is {0}".format(e))
> e is [1.0, 1.0, 1.0]
print("type of e is {0}".format(type(e)))
> type of e is <class 'list'>
# list 转换为 array
f = np.array([3, 9, 10])
print("f is {0}".format(f))
> f is [ 3 9 10]
print("type of f is {0}".format(type(f)))
> type of f is <class 'numpy.ndarray'>
3. array 和 list 的操作
对于 array和list,numpy 都可以对这两个变量进行操作运算。比如 np.mean 操作,我们来看一下区别:
先来看看ndarray 的np.mean,注意不管是 axis=0 (列操作) 和 axis=1 (行操作),结果都是1维数组。
na = np.ones((2,3))
print("na is: {0}".format(na))
>na is: [[1. 1. 1.]
[1. 1. 1.]]
naMeanPerColumn = np.mean(na,axis=0)
print("naMeanPerColumn is: {0}".format(naMeanPerColumn))
> naMeanPerColumn is: [1. 1. 1.]
print("type of naMeanPerColumn is: {0}".format(type(naMeanPerColumn)))
> type of naMeanPerColumn is: <class 'numpy.ndarray'>
print("naMeanPerColumn's shape is: {0}".format(naMeanPerColumn.shape))
> naMeanPerColumn's shape is: (3,)
naMeanPerRow = np.mean(na,axis=1)
print("naMeanPerRow is: {0}".format(naMeanPerRow))
> naMeanPerRow is: [1. 1.]
print("type of naMeanPerRow is {0}".format(type(naMeanPerRow)))
> type of naMeanPerRow is <class 'numpy.ndarray'>
print("naMeanPerRow's shape is: {0}".format(naMeanPerRow.shape))
> naMeanPerRow's shape is: (2,)
再来看看list,列操作 axis=0, np.mean后,matrix是1行3列的,行操作后,axis=1,np.mean后,matrix是2行1列的,类型没有变化,都是matrix。
oa = np.matrix([[1,1,1],
[1,1,1]])
oaMeanPerColumn = np.mean(oa,axis=0)
print("oaMeanPerColumn is: {0}".format(oaMeanPerColumn))
> oaMeanPerColumn is: [[1. 1. 1.]]
print("type of oaMeanPerColumn is: {0}".format(type(oaMeanPerColumn)))
> type of oaMeanPerColumn is: <class 'numpy.matrix'>
print("oaMeanPerColumn's shape is: {0}".format(oaMeanPerColumn.shape))
> oaMeanPerColumn's shape is: (1, 3)
oaMeanPerRow = np.mean(oa,axis=1)
print("oaMeanPerRow is: {0}".format(oaMeanPerRow))
> oaMeanPerRow is: [[1.]
[1.]]
print("type of oaMeanPerRow is {0}".format(type(oaMeanPerRow)))
> type of oaMeanPerRow is <class 'numpy.matrix'>
print("oaMeanPerRow's shape is: {0}".format(oaMeanPerRow.shape))
> oaMeanPerRow's shape is: (2, 1)
ndarray 和 list的这些细微差别,请别忽视。