【机器学习实战】——常见函数积累

目录

第二章 k近邻算法

1、array.sum(axies = 1) :

2、array.argsort(axies=0/1)

3、array.tile(mat,(m,n))

4、dict.get(key,value)

5、sorted函数

6、string.strip()函数

7、string.split()

8、scatter()函数

9、min()&max()

10、enumerate(s,0/1)

11、np.where()

12、zip()函数,压缩数据

13、*list——解压

14、round(float类型的数,保留小数的位数)——保留指定位数的小数

第十章 聚类算法

1、nonzero()函数

解释

2、matrix.A

3.add_axes()新增子区域


第二章 k近邻算法

1、array.sum(axies = 1) :

返回数组行向量和组成的数组

2、array.argsort(axies=0/1)

https://blog.csdn.net/Python798/article/details/81138040

返回返回的是数组值从小到大的索引值

axies = 0:按列排序

axies = 1:按行排列

3、array.tile(mat,(m,n))

https://www.jianshu.com/p/9519f1984c70

复制功能,将数组mat横向复制m个,纵向复制n个

4、dict.get(key,value)

根据键名称来获得在字典dictionary中对应的值,若在dict中不存在该键key,则返回指定的value值,注意这不会改变原始字典,即不会将不存在的key添加到字典中去

a = {1:2,3:1}
print(a.get(1,999))
print(a.get(4,12))
print(a)

2#key存在返回值
12#key不存在返回指定的value
{1: 2, 3: 1}#不改变原始字典的键值对

以下方式会改变原始字典的键值对:

a = {1:2,3:1}
key = "jjj"
print(a)
a[key] = a.get(key,0)
print(a)

{1: 2, 3: 1}
{1: 2, 3: 1, 'jjj': 0}

5、sorted函数

根据一定的规则(升序or降序)对指定维度(这里通过operator来指定)进行排序

sorted(classCount.items(),#获得所有的键值对,以元组的形式存在,元组存放在一个列表中
       key = operator.itemgetter(1),#对第二个元素进行排序
       reverse = True)#True表示从大到小进行排序

6、string.strip()函数

去掉字符串首尾空格

7、string.split()

按照指定的划分规则将字符串划分成一个个元素组成一个列表

8、scatter()函数

https://blog.csdn.net/TeFuirnever/article/details/88944438?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

    plt.scatter(returnmat[:,0],returnmat[:,1],
                15*np.array(classLabelVector),#指的是标记的大小
                15*np.array(classLabelVector))#表示的是颜色的值

9、min()&max()

返回最值

array.min(n)

当n=0时,返回的是每一列的最小值组成的列表,当n=1,返回的是每一行的最小值组成的列表

对于max来说同理

10、enumerate(s,0/1)

https://www.cnblogs.com/tylf-lk/p/10133489.html

返回的是一个元组,(index,value),索引号和值组成的元组

for  i in enumerate(s,1):

    print(i)

11、np.where()

https://www.cnblogs.com/massquantity/p/8908859.html

根据一定的筛选条件返回数值

12、zip()函数,压缩数据

https://blog.csdn.net/Trent1985/article/details/77096683

将多个序列的相同位置的元素组合成一个元组,最后变成一个列表返回

注意:这里zip所接受的多个序列的维度必须是一致的,各维度的·长度也必须是一样的,否则无法进行配对

from numpy import array
from numpy import int64
loc = (array([180, 180, 181, 181, 236, 236, 236, 236, 237, 237, 237, 237, 238,
       238, 238, 238], dtype=int64), array([580, 581, 580, 581, 566, 567, 568, 569, 566, 567, 568, 569, 566,
       567, 568, 569], dtype=int64))

loc_ = loc[::-1]#反向进行取值
print(*loc_)
for i in (zip(*loc_)):
    print(i)

'''
[580 581 580 581 566 567 568 569 566 567 568 569 566 567 568 569] [180 180 181 181 236 236 236 236 237 237 237 237 238 238 238 238]
(580, 180)
(581, 180)
(580, 181)
(581, 181)
(566, 236)
(567, 236)
(568, 236)
(569, 236)
(566, 237)
(567, 237)
(568, 237)
(569, 237)
(566, 238)
(567, 238)
(568, 238)
(569, 238)

'''

13、*list——解压

将list中的元素独立开来,作为独立的参数

https://blog.csdn.net/hellenlee22/article/details/89740923

14、round(float类型的数,保留小数的位数)——保留指定位数的小数

第十章 聚类算法

1、nonzero()函数

解释

nonzero(a)

返回数组a中非零元素的索引值数组。

(1)只有a中非零元素才会有索引值,那些零值元素没有索引值;
(2)返回的索引值数组是一个2维tuple数组,该tuple数组中包含一维的array数组。其中,一维array向量的个数与a的维数是一致的。
(3)索引值数组的每一个array均是从一个维度上来描述其索引值。比如,如果a是一个二维数组,则索引值数组有两个array,第一个array从行维度来描述索引值;第二个array从列维度来描述索引值。
(4) 该np.transpose(np.nonzero(x))
函数能够描述出每一个非零元素在不同维度的索引值。
(5)通过a[nonzero(a)]得到所有a中的非零值

链接:https://blog.csdn.net/u013698770/article/details/54632047

2、matrix.A

将矩阵mat转换成数组adrray

https://blog.csdn.net/qq_41800366/article/details/87866932

3.add_axes()新增子区域

个人理解就是类似于photoshop中的图层一样,为图中图做准备

add_axes新增子区域

add_axes为新增子区域,该区域可以座落在figure内任意位置,且该区域可任意设置大小
可以用来做一些子图,图中图
考虑如下代码:
import numpy as np
import matplotlib.pyplot as plt

#新建figure
fig = plt.figure()
#定义数据
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 7, 15, 24, 30, 50, 55]

#新建区域ax1
#figure的百分比,从figure 10%的位置开始绘制, 宽高是figure的80%
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
#获得绘制的句柄
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x, y, ‘r’)
ax1.set_title(‘area1’)

#新增区域ax2,嵌套在ax1内,看一看图中图是什么样,这就是与subplot的区别
left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
#获得绘制的句柄
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(x,y, ‘b’)
ax2.set_title(‘area2’)
plt.show()

参考链接:https://blog.csdn.net/qq_41011336/article/details/83017101

import matplotlib.pyplot as plt
import numpy as np
import cv2

fig = plt.figure()# 获得图像窗口句柄
rect0 = [0.1,0.1,0.8,0.8] # left, bottom, width, height
rect1 = [0.3,0.3,0.5,0.5] # left, bottom, width, height

# 读取图片
img = plt.imread(r"E:\Portland.png")
img1 = plt.imread(r"E:\二分聚类结果.png")

# 新增图层,在同一个图像窗口进行绘制,这是每一个图层的句柄
ax0 = fig.add_axes(rect0,label='ax0')
ax1 = fig.add_axes(rect1,label='ax1')

# 显示图像
ax0.imshow(img)
ax1.imshow(img1)

plt.show()

 

 

Supongo que te gusta

Origin blog.csdn.net/qq_45769063/article/details/108569722
Recomendado
Clasificación