iT邦幫忙

0

sklearn dataset make_moons() make_circles()

  • 分享至 

  • xImage
  •  

有趣的toy datasets make_moons() make_circles()
取自sklearn範例,machine learning toy datasets 可上官網連結
toy datasets 玩具數據集 make_moons()
Make two interleaving half circles 2個交錯的半圓型 (半月狀)

import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import make_moons, make_circles

#--- Make two interleaving half circles 2個交錯的半圓型 (半月狀)
def mkMoons(nnoise=0.1, nCount=1000, nfig=0):
    X, y = make_moons(noise=nnoise, n_samples=nCount)
    sns.scatterplot(
        x=X[:, 0], y=X[:, 1], hue=y,
        marker='o', s=25, edgecolor='k', legend=False
        ).set_title('make_moons noise '+str(nnoise))
    plt.savefig('moon'+str(nfig)+'.jpg')
    plt.show()

make_circles()
Make a large circle containing a smaller circle 大圓圈內有小圓圈
factor參數:兩個圈的距離 Scale factor between inner and outer circle in the range (0, 1)

#--- Make a large circle containing a smaller circle 大圓圈內有小圓圈
# factor : Scale factor between inner and outer circle in the range (0, 1)
def mkCircles(nnoise=0.1,nCount=1000, nfactor=0.5, nfig=0):
    X, y = make_circles(noise=nnoise, factor=nfactor, n_samples=nCount)
    sns.scatterplot(
        x=X[:, 0], y=X[:, 1], hue=y,
        marker='o', s=25, edgecolor='k', legend=False
        ).set_title('make_circles noise '+str(nnoise))
    plt.savefig('circle'+str(nfig)+'.jpg')
    plt.show()

寫個迴圈,試試看不同設定,畫出來的兩群,長像如何?

#--- call function
distance = [0.1, 0.2, 0.5, 0.7, 0.9]
k = 0
for d in distance:
    mkMoons(d,1000,k)
    k += 1
    
k = 0
for d in distance:
    mkCircles(d,1000,0.5,k)
    k += 1

兩個半月 noise 0.1 很明顯的兩群
https://ithelp.ithome.com.tw/upload/images/20210912/20111373rmoUi5samy.jpg
noise 0.9 兩群混在一起了
https://ithelp.ithome.com.tw/upload/images/20210912/20111373lZF8JdcZvG.jpg

大圈包小圈
https://ithelp.ithome.com.tw/upload/images/20210912/20111373hISppbglYF.jpg
https://ithelp.ithome.com.tw/upload/images/20210912/20111373b6xUz6azZN.jpg

Source Code GitHub download

也可以自行調整 factor 0~1 看看變化如何


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言