【机器学习实战】——常见函数积累
目錄
第二章 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 cv2fig = 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()?
?
總結
以上是生活随笔為你收集整理的【机器学习实战】——常见函数积累的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于单片机倾角检测仪设计分享
- 下一篇: iOS录音后播放声音变小的解决方法