導航:首頁 > 編程語言 > 核聚類python

核聚類python

發布時間:2022-03-08 17:25:58

python代碼如何應用系統聚類和K-means聚類法進行聚類分析 然後選擇變數,建立適當的模型

-Means聚類演算法
k-means演算法以k為參數,把n個對象分成k個簇,使簇內具有較高的相似度,而簇間的相似度較低。

隨機選擇k個點作為初始的聚類中心。
對於剩下的點,根據其與聚類中心的距離,將其歸入最近的簇。
對每個簇,計算所有點的均值作為新的聚類中心。
重復2,3直到聚類中心不再發生改變

Figure 1

K-means的應用
數據介紹:
現有1999年全國31個省份城鎮居民家庭平均每人全年消費性支出的八大主要變數數據,這八大變數分別是:食品、衣著、家庭設備用品及服務、醫療保健、交通和通訊、娛樂教育文化服務、居住以及雜項商品和服務。利用已有數據,對31個省份進行聚類。

實驗目的:
通過聚類,了解1999年各個省份的消費水平在國內的情況。

技術路線:
sklearn.cluster.Kmeans

數據實例:

② python聚類如何解釋

代碼如下:# -*- coding: utf-8 -*-from sklearn.cluster import KMeansfrom sklearn.externals import joblibimport numpy final = open('c:/test/final.dat' , 'r') data = [line.strip().split('\t') for line in final]feature = [[float(x) for x in row[3:]] for row in data] #調用kmeans類clf = KMeans(n_clusters=9)s = clf.fit(feature)print s #9個中心print clf.cluster_centers_ #每個樣本所屬的簇print clf.labels_ #用來評估簇的個數是否合適,距離越小說明簇分的越好,選取臨界點的簇個數print clf.inertia_ #進行預測print clf.predict(feature) #保存模型joblib.mp(clf , 'c:/km.pkl') #載入保存的模型clf = joblib.load('c:/km.pkl') '''#用來評估簇的個數是否合適,距離越小說明簇分的越好,選取臨界點的簇個數for i in range(5,30,1): clf = KMeans(n_clusters=i) s = clf.fit(feature) print i , clf.inertia_

③ python對數據進行聚類怎麼顯示數據分類

將其整理成數據集為:
[ [1,0,"yes"],[1,1,"yes"],[0,1,"yes"],[0,0,"no"],[1,0,"no"] ]
演算法過程:

1、計算原始的信息熵。
2、依次計算數據集中每個樣本的每個特徵的信息熵。
3、比較不同特徵信息熵的大小,選出信息熵最大的特徵值並輸出。
運行結果:
col : 0 curInfoGain : 2.37744375108 baseInfoGain : 0.0
col : 1 curInfoGain : 1.37744375108 baseInfoGain : 2.37744375108
bestInfoGain : 2.37744375108 bestFeature: 0
結果分析:
說明按照第一列,即有無喉結這個特徵來進行分類的效果更好。
思考:
1、能否利用決策樹演算法,將樣本最終的分類結果進行輸出?如樣本1,2,3屬於男性,4屬於女性。

2、示常式序生成的決策樹只有一層,當特徵量增多的時候,如何生成具有多層結構的決策樹?
3、如何評判分類結果的好壞?
在下一篇文章中,我將主要對以上三個問題進行分析和解答。如果您也感興趣,歡迎您訂閱我的文章,也可以在下方進行評論,如果有疑問或認為不對的地方,您也可以留言,我將積極與您進行解答。
完整代碼如下:
from math import log
"""
計算信息熵
"""
def calcEntropy(dataset):
diclabel = {} ## 標簽字典,用於記錄每個分類標簽出現的次數
for record in dataset:
label = record[-1]
if label not in diclabel.keys():
diclabel[label] = 0
diclabel[label] += 1
### 計算熵
entropy = 0.0
cnt = len(dataset)
for label in diclabel.keys():
prob = float(1.0 * diclabel[label]/cnt)
entropy -= prob * log(prob,2)
return entropy
def initDataSet():
dataset = [[1,0,"yes"],[1,1,"yes"],[0,1,"yes"],[0,0,"no"],[1,0,"no"]]
label = ["male","female"]
return dataset,label
#### 拆分dataset ,根據指定的過濾選項值,去掉指定的列形成一個新的數據集
def splitDataset(dataset , col, value):
retset = [] ## 拆分後的數據集
for record in dataset:
if record[col] == value :
recedFeatVec = record[:col]
recedFeatVec.extend(record[col+1:]) ### 將指定的列剔除
retset.append(recedFeatVec) ### 將新形成的特徵值列表追加到返回的列表中
return retset
### 找出信息熵增益最大的特徵值
### 參數:
### dataset : 原始的數據集
def findBestFeature(dataset):
numFeatures = len(dataset[0]) - 1 ### 特徵值的個數
baseEntropy = calcEntropy(dataset) ### 計算原始數據集的熵
baseInfoGain = 0.0 ### 初始信息增益
bestFeature = -1 ### 初始的最優分類特徵值索引
### 計算每個特徵值的熵
for col in range(numFeatures):
features = [record[col] for record in dataset] ### 提取每一列的特徵向量 如此處col= 0 ,則features = [1,1,0,0]
uniqueFeat = set(features)
curInfoGain = 0 ### 根據每一列進行拆分,所獲得的信息增益
for featVal in uniqueFeat:
subDataset = splitDataset(dataset,col,featVal) ### 根據col列的featVal特徵值來對數據集進行劃分
prob = 1.0 * len(subDataset)/numFeatures ### 計運算元特徵數據集所佔比例
curInfoGain += prob * calcEntropy(subDataset) ### 計算col列的特徵值featVal所產生的信息增益
# print "col : " ,col , " featVal : " , featVal , " curInfoGain :" ,curInfoGain ," baseInfoGain : " ,baseInfoGain
print "col : " ,col , " curInfoGain :" ,curInfoGain ," baseInfoGain : " ,baseInfoGain
if curInfoGain > baseInfoGain:
baseInfoGain = curInfoGain
bestFeature = col
return baseInfoGain,bestFeature ### 輸出最大的信息增益,以獲得該增益的列
dataset,label = initDataSet()
infogain , bestFeature = findBestFeature(dataset)
print "bestInfoGain :" , infogain, " bestFeature:",bestFeature

④ python怎麼做聚類樹狀圖

#-*-coding:utf-8-*-importmathimportpylabaspl#數據集:每三個是一組分別是西瓜的編號,密度,含糖量data="""
1,0.697,0.46,2,0.774,0.376,3,0.634,0.264,4,0.608,0.318,5,0.556,0.215,
6,0.403,0.237,7,0.481,0.149,8,0.437,0.211,9,0.666,0.091,10,0.243,0.267,
11,0.245,0.057,12,0.343,0.099,13,0.639,0.161,14,0.657,0.198,15,0.36,0.37,
16,0.593,0.042,17,0.719,0.103,18,0.359,0.188,19,0.339,0.241,20,0.282,0.257,
21,0.748,0.232,22,0.714,0.346,23,0.483,0.312,24,0.478,0.437,25,0.525,0.369,
26,0.751,0.489,27,0.532,0.472,28,0.473,0.376,29,0.725,0.445,30,0.446,0.459"""#數據處理dataset是30個樣本(密度,含糖量)的列表a=data.split(',')
dataset=[(float(a[i]),float(a[i+1]))foriinrange(1,len(a)-1,3)]#計算歐幾里得距離,a,b分別為兩個元組defdist(a,b):
returnmath.sqrt(math.pow(a[0]-b[0],2)+math.pow(a[1]-b[1],2))#dist_mindefdist_min(Ci,Cj):
returnmin(dist(i,j)foriinCiforjinCj)#dist_maxdefdist_max(Ci,Cj):
returnmax(dist(i,j)foriinCiforjinCj)#dist_avgdefdist_avg(Ci,Cj):
returnsum(dist(i,j)foriinCiforjinCj)/(len(Ci)*len(Cj))#找到距離最小的下標deffind_Min(M):
min=1000
x=0;y=0
foriinrange(len(M)):forjinrange(len(M[i])):ifi!=jandM[i][j]<min:
min=M[i][j];x=i;y=jreturn(x,y,min)#演算法模型:defAGNES(dataset,dist,k):
#初始化C和M
C=[];M=[]foriindataset:
Ci=[]
Ci.append(i)
C.append(Ci)foriinC:
Mi=[]forjinC:
Mi.append(dist(i,j))
M.append(Mi)
q=len(dataset)#合並更新
whileq>k:
x,y,min=find_Min(M)
C[x].extend(C[y])
C.remove(C[y])
M=[]foriinC:
Mi=[]forjinC:
Mi.append(dist(i,j))
M.append(Mi)
q-=1
returnC#畫圖defdraw(C):
colValue=['r','y','g','b','c','k','m']foriinrange(len(C)):
coo_X=[]#x坐標列表
coo_Y=[]#y坐標列表
forjinrange(len(C[i])):
coo_X.append(C[i][j][0])
coo_Y.append(C[i][j][1])
pl.scatter(coo_X,coo_Y,marker='x',color=colValue[i%len(colValue)],label=i)

pl.legend(loc='upperright')
pl.show()

C=AGNES(dataset,dist_avg,3)
draw(C)

⑤ 減法聚類如何用Python實現

下面是一個k-means聚類演算法在python2.7.5上面的具體實現,你需要先安裝Numpy和Matplotlib:
from numpy import *
import time
import matplotlib.pyplot as plt

# calculate Euclidean distance
def euclDistance(vector1, vector2):
return sqrt(sum(power(vector2 - vector1, 2)))
# init centroids with random samples
def initCentroids(dataSet, k):
numSamples, dim = dataSet.shape
centroids = zeros((k, dim))
for i in range(k):
index = int(random.uniform(0, numSamples))
centroids[i, :] = dataSet[index, :]
return centroids
# k-means cluster
def kmeans(dataSet, k):
numSamples = dataSet.shape[0]
# first column stores which cluster this sample belongs to,
# second column stores the error between this sample and its centroid
clusterAssment = mat(zeros((numSamples, 2)))
clusterChanged = True
## step 1: init centroids
centroids = initCentroids(dataSet, k)
while clusterChanged:
clusterChanged = False
## for each sample
for i in xrange(numSamples):
minDist = 100000.0
minIndex = 0
## for each centroid
## step 2: find the centroid who is closest
for j in range(k):
distance = euclDistance(centroids[j, :], dataSet[i, :])
if distance < minDist:
minDist = distance
minIndex = j

## step 3: update its cluster
if clusterAssment[i, 0] != minIndex:
clusterChanged = True
clusterAssment[i, :] = minIndex, minDist**2
## step 4: update centroids
for j in range(k):
pointsInCluster = dataSet[nonzero(clusterAssment[:, 0].A == j)[0]]
centroids[j, :] = mean(pointsInCluster, axis = 0)
print 'Congratulations, cluster complete!'
return centroids, clusterAssment
# show your cluster only available with 2-D data
def showCluster(dataSet, k, centroids, clusterAssment):
numSamples, dim = dataSet.shape
if dim != 2:
print "Sorry! I can not draw because the dimension of your data is not 2!"
return 1
mark = ['or', 'ob', 'og', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']
if k > len(mark):
print "Sorry! Your k is too large! please contact Zouxy"
return 1
# draw all samples
for i in xrange(numSamples):
markIndex = int(clusterAssment[i, 0])
plt.plot(dataSet[i, 0], dataSet[i, 1], mark[markIndex])
mark = ['Dr', 'Db', 'Dg', 'Dk', '^b', '+b', 'sb', 'db', '<b', 'pb']
# draw the centroids
for i in range(k):
plt.plot(centroids[i, 0], centroids[i, 1], mark[i], markersize = 12)
plt.show()

⑥ 如何用python對文本進行聚類

實現原理:
首先從Tourist_spots_5A_BD.txt中讀取景點信息,然後通過調用無界面瀏覽器PhantomJS(Firefox可替代)訪問網路鏈接"http://ke..com/",通過Selenium獲取輸入對話框ID,輸入關鍵詞如"故宮",再訪問該網路頁面。最後通過分析DOM樹結構獲取摘要的ID並獲取其值。核心代碼如下:
driver.find_elements_by_xpath("//div[@class='lemma-summary']/div")

PS:Selenium更多應用於自動化測試,推薦Python爬蟲使用scrapy等開源工具。
# coding=utf-8
"""
Created on 2015-09-04 @author: Eastmount
"""

import time
import re
import os
import sys
import codecs
import shutil
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.action_chains import ActionChains

#Open PhantomJS
driver = webdriver.PhantomJS(executable_path="G:\phantomjs-1.9.1-windows\phantomjs.exe")
#driver = webdriver.Firefox()
wait = ui.WebDriverWait(driver,10)

#Get the Content of 5A tourist spots
def getInfobox(entityName, fileName):
try:
#create paths and txt files
print u'文件名稱: ', fileName
info = codecs.open(fileName, 'w', 'utf-8')

#locate input notice: 1.visit url by unicode 2.write files
#Error: Message: Element not found in the cache -
# Perhaps the page has changed since it was looked up
#解決方法: 使用Selenium和Phantomjs
print u'實體名稱: ', entityName.rstrip('\n')
driver.get("http://ke..com/")
elem_inp = driver.find_element_by_xpath("//form[@id='searchForm']/input")
elem_inp.send_keys(entityName)
elem_inp.send_keys(Keys.RETURN)
info.write(entityName.rstrip('\n')+'\r\n') #codecs不支持'\n'換行
time.sleep(2)

#load content 摘要
elem_value = driver.find_elements_by_xpath("//div[@class='lemma-summary']/div")
for value in elem_value:
print value.text
info.writelines(value.text + '\r\n')
time.sleep(2)

except Exception,e: #'utf8' codec can't decode byte
print "Error: ",e
finally:
print '\n'
info.close()

#Main function
def main():
#By function get information
path = "BaiSpider\\"
if os.path.isdir(path):
shutil.rmtree(path, True)
os.makedirs(path)
source = open("Tourist_spots_5A_BD.txt", 'r')
num = 1
for entityName in source:
entityName = unicode(entityName, "utf-8")
if u'故宮' in entityName: #else add a '?'
entityName = u'北京故宮'
name = "%04d" % num
fileName = path + str(name) + ".txt"
getInfobox(entityName, fileName)
num = num + 1
print 'End Read Files!'
source.close()
driver.close()

if __name__ == '__main__':
main()

⑦ 如何用Python對人員軌跡聚類

把你的 xy 變換成 onehot編碼 ,這樣的話 聚類演算法就都可以兼容了,
KMeans, DBScan, 層次聚類,等等都是可以的

⑧ 怎麼用python進行聚類分析

、K均值聚類K-Means演算法思想簡單,效果卻很好,是最有名的聚類演算法。聚類演算法的步驟如下:1:初始化K個樣本作為初始聚類中心;2:計算每個樣本點到K個中心的距離,選擇最近的中心作為其分類,直到所有樣本點分類完畢;3:分別計算K個類中所有樣本的質心,作為新的中心點,完成一輪迭代。通常的迭代結束條件為新的質心與之前的質心偏移值小於一

⑨ python聚類分析需要多長時間

有沒有編程基礎?如果以前學過其他語言,底子比較好,那麼從開始學Python到寫出一個最簡單的爬蟲幾天就可以搞定。如果沒有編程基礎,對普通人來說需要的時間就長了,光是學Python就很費時間,因為要打基矗

⑩ python核心演算法有哪些

python雖然具備很多高級模塊,也是自帶電池的編程語言,但是要想做一個合格的程序員,基本的演算法還是需要掌握,本文主要介紹列表的一些排序演算法~

閱讀全文

與核聚類python相關的資料

熱點內容
優信二手車解壓後過戶 瀏覽:62
Windows常用c編譯器 瀏覽:778
關於改善國家網路安全的行政命令 瀏覽:833
安卓如何下載網易荒野pc服 瀏覽:654
javainetaddress 瀏覽:104
蘋果4s固件下載完了怎麼解壓 瀏覽:1003
命令zpa 瀏覽:286
python編譯器小程序 瀏覽:945
在app上看視頻怎麼光線調暗 瀏覽:540
可以中文解壓的解壓軟體 瀏覽:593
安卓卸載組件應用怎麼安裝 瀏覽:913
使用面向對象編程的方式 瀏覽:339
程序員項目經理的年終總結範文 瀏覽:929
內衣的加密設計用來幹嘛的 瀏覽:433
淮安數據加密 瀏覽:292
魔高一丈指標源碼 瀏覽:982
松下php研究所 瀏覽:168
c回調java 瀏覽:401
夢幻端游長安地圖互通源碼 瀏覽:746
電腦本地文件如何上傳伺服器 瀏覽:313