2020-04-11 机器学习相关函数

2020-04-11  本文已影响0人  陆寒晨

np.array_split() & np.split()

np.array_split() -> 不均等划分;
np.split() -> 均等划分
参数均为int的划分结果数量

np.arange()

  • 1个参数:0到这个参数,步长为1
  • 2个参数:开始到终点,步长为1
  • 3个参数:开始到终点,步长指定

plt.gca()

获取当前的Figure或Axes对象,图表或子图

np.linspace()

返回等间距的样本均匀分布的样本

np.meshgrid(x,y)

生成网格点,x,y的每行相同,批量生成

np.c_()

按行连接两个矩阵,即矩阵左右相加,要求行数相等,相当于pandas中的merge()

np.r_()

按列连接两个矩阵,即矩阵上下相加,要求列数相等,相当于pandas中的contact()

ax.imshow

python画热图,ax = fig.add_subplot(221) -> ax.imshow(z) 或 ax = plt.gca() -> ax.imshow(z)

numpy.ravel() && numpy.flatten()函数

两者的功能都是将高维数组大散,变成一维,不同的是flatten返回拷贝,ravel返回视图,修改ravel返回值时可修改原数组,而拷贝则不行。

getattr()函数

获取对象的属性【类对象的属性值,类的方法调用】

# 举例
class A():
    x = 11
    def set(self):
        a, b = 1, 2
        return a+b
cc = A()
print("getattr(cc,'x') = {}".format(getattr(cc,'x')))
fun_set = getattr(cc,'set')
print("set function = {}".format(fun_set()))
# output:
getattr(cc,'x') = 11
set function = 3

hasattr()函数

用于判断对象是否包含对应的属性。

class A():
    x = 11
    def set(self):
        a, b = 1, 2
        return a+b
cc = A()
print("Class A has attribute x ? {}".format(hasattr(cc, 'x')))
print("Class A has attribute y ? {}".format(hasattr(cc, 'y')))
print("Class A has attribute set ? {}".format(hasattr(cc, 'set')))
# output
Class A has attribute x ? True
Class A has attribute y ? False # 因没有y属性
Class A has attribute set ? True
上一篇下一篇

猜你喜欢

热点阅读