99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀Python KMeans聚類問(wèn)題分析
Python KMeans聚類問(wèn)題分析
2018-05-23
收藏

Python KMeans聚類問(wèn)題分析

今天用python實(shí)現(xiàn)了一下簡(jiǎn)單的聚類分析,順便熟悉了numpy數(shù)組操作和繪圖的一些技巧,在這里做個(gè)記錄。


frompylabimport*
fromsklearn.clusterimportKMeans
 
## 利用numpy.append()函數(shù)實(shí)現(xiàn)matlab多維數(shù)組合并的效果,axis 參數(shù)值為 0 時(shí)是 y 軸方向合并,參數(shù)值為 1 時(shí)是 x 軸方向合并,分別對(duì)應(yīng)matlab [A ; B] 和 [A , B]的效果
 
#創(chuàng)建5個(gè)隨機(jī)的數(shù)據(jù)集
x1=append(randn(500,1)+5,randn(500,1)+5,axis=1)
x2=append(randn(500,1)+5,randn(500,1)-5,axis=1)
x3=append(randn(500,1)-5,randn(500,1)+5,axis=1)
x4=append(randn(500,1)-5,randn(500,1)-5,axis=1)
x5=append(randn(500,1),randn(500,1),axis=1)
 
# 下面用較笨的方法把5個(gè)數(shù)據(jù)集合并成 (2500,2)大小的數(shù)組data
data=append(x1,x2,axis=0)
data=append(data,x3,axis=0)
data=append(data,x4,axis=0)
data=append(data,x5,axis=0)
 
plot(x1[:,0],x1[:,1],'oc',markersize=0.8)
plot(x2[:,0],x2[:,1],'og',markersize=0.8)
plot(x3[:,0],x3[:,1],'ob',markersize=0.8)
plot(x4[:,0],x4[:,1],'om',markersize=0.8)
plot(x5[:,0],x5[:,1],'oy',markersize=0.8)
 
 
k=KMeans(n_clusters=5,random_state=0).fit(data)
t=k.cluster_centers_# 獲取數(shù)據(jù)中心點(diǎn)
 
plot(t[:,0],t[:,1],'r*',markersize=16)# 顯示這5個(gè)中心點(diǎn),五角星標(biāo)記~
 
title('KMeans Clustering')
box(False)
 
xticks([]) # 去掉坐標(biāo)軸的標(biāo)記
yticks([])
 
show()

結(jié)果如下:

2017/01/11更新

今天重新試運(yùn)行程序的出現(xiàn)報(bào)錯(cuò)了,提示導(dǎo)入NUMPY_MKL失敗,因?yàn)橹坝妹頿ip install -U numpy手動(dòng)更新了numpy,最初的是在http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy 里下載的numpy-1.11.2+mkl-cp27-cp27m-win_amd64.whl 文件安裝的,只要重新安裝回去就可以了

2017/1/18更新

python中還有一個(gè)叫plotly 的package,可以通過(guò)pip install plotly 或 pip3 install plotly(Python3.X) ,使用這個(gè)package可以繪制精美的圖像,官網(wǎng)中有很多例子介紹,同時(shí)plotly 還支持matlab,R等,但是個(gè)人覺(jué)得plotly 的繪圖語(yǔ)法相比matplotlib 的繁瑣,需要照著例程來(lái)修改才比較方便,不過(guò)如果只是要想數(shù)據(jù)可視化更好看的話參考官網(wǎng)例程并做修改也無(wú)妨,下面是來(lái)自官網(wǎng)的一段示例代碼:

importplotly.plotly as py
importplotly.graph_objs as go
importplotly
importnumpy as np
 
#生成三組高斯分布(Gaussian Distribution)點(diǎn)集
 
x0=np.random.normal(2,0.45,300)
y0=np.random.normal(2,0.45,300)
 
x1=np.random.normal(6,0.8,200)
y1=np.random.normal(6,0.8,200)
 
x2=np.random.normal(4,0.3,200)
y2=np.random.normal(4,0.3,200)
 
#創(chuàng)建圖形對(duì)象 graph object
trace0=go.Scatter(
 x=x0,
 y=y0,
 mode='markers',
)
trace1=go.Scatter(
 x=x1,
 y=y1,
 mode='markers'
)
trace2=go.Scatter(
 x=x2,
 y=y2,
 mode='markers'
)
trace3=go.Scatter(
 x=x1,
 y=y0,
 mode='markers'
)
#布局是一個(gè)字典,字典關(guān)鍵字keys包括:'shapes', 'showlegend'
layout={
 'shapes': [
  {
   'type':'circle',
   'xref':'x',
   'yref':'y',
   'x0':min(x0),
   'y0':min(y0),
   'x1':max(x0),
   'y1':max(y0),
   'opacity':0.2,
   'fillcolor':'blue',
   'line': {
    'color':'blue',
   },
  },
  {
   'type':'circle',
   'xref':'x',
   'yref':'y',
   'x0':min(x1),
   'y0':min(y1),
   'x1':max(x1),
   'y1':max(y1),
   'opacity':0.2,
   'fillcolor':'orange',
   'line': {
    'color':'orange',
   },
  },
  {
   'type':'circle',
   'xref':'x',
   'yref':'y',
   'x0':min(x2),
   'y0':min(y2),
   'x1':max(x2),
   'y1':max(y2),
   'opacity':0.2,
   'fillcolor':'green',
   'line': {
    'color':'green',
   },
  },
  {
   'type':'circle',
   'xref':'x',
   'yref':'y',
   'x0':min(x1),
   'y0':min(y0),
   'x1':max(x1),
   'y1':max(y0),
   'opacity':0.2,
   'fillcolor':'red',
   'line': {
    'color':'red',
   },
  },
 ],
 'showlegend':False,
}
data=[trace0, trace1, trace2, trace3]
#圖像包括數(shù)據(jù)部分和布局部分
fig={
 'data': data,
 'layout': layout,
}
#使用離線的方式繪制圖像,因?yàn)闆](méi)有注冊(cè)官方的網(wǎng)站,而且那個(gè)網(wǎng)站不容易進(jìn)去,所以用離線繪制
plotly.offline.plot(fig, filename='clusters')

結(jié)果是通過(guò)瀏覽器打開(kāi)圖片的,可以保存到本地,如下圖:

總結(jié):plotly 這個(gè)庫(kù)雖然語(yǔ)法比較繁瑣,但是對(duì)數(shù)據(jù)顯示要求較高的情況下可以充分利用,一般繪圖的話使用matplotlib比較方便,特別是ipython模式下先執(zhí)行from pylab import * 可以獲得和MATLAB 類似的工作環(huán)境。



數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }