當前位置:首頁 » 編程語言 » 聚類python

聚類python

發布時間: 2022-01-12 10:12:13

⑴ 如何用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怎麼做聚類樹狀圖

#-*-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對人員軌跡聚類

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

⑷ 怎麼用python進行聚類分析

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

⑸ k-means聚類演算法python實現,導入的數據集有什麼要求

一,K-Means聚類演算法原理
k-means 演算法接受參數 k
;然後將事先輸入的n個數據對象劃分為
k個聚類以便使得所獲得的聚類滿足:同一聚類中的對象相似度較高;而不同聚類中的對象相似度較小。聚類相似度是利用各聚類中對象的均值所獲得一個「中心對
象」(引力中心)來進行計算的。
K-means演算法是最為經典的基於劃分的聚類方法,是十大經典數據挖掘演算法之一。K-means演算法的基本思想是:以空間中k個點為中心進行聚類,對最靠近他們的對象歸類。通過迭代的方法,逐次更新各聚類中心的值,直至得到最好的聚類結果。

⑹ 減法聚類如何用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()

⑺ 用python2.7做kmeans聚類演算法怎麼導入數據

指定文件名
問題描述:一堆二維數據,用kmeans演算法對其進行聚類,下面例子以分k=3為例。
原數據:
1.5,3.1
2.2,2.9
3,4
2,1
15,25
43,13
32,42
0,0
8,9
12,5
9,12
11,8
22,33
24,25

實現代碼:

[python] view plain
#coding:utf-8
from numpy import *
import string
import math

def loadDataSet(filename):
dataMat = []
fr = open(filename)
for line in fr.readlines():
element = line.strip('\n').split(',')
number = []
for i in range(len(element)):
number.append(string.atof(element[i]))
dataMat.append(number)
return dataMat

def distEclud(vecA, vecB):
count = len(vecA)
s = 0.0
for i in range(0, count):
s = s + power(vecA[i]-vecB[i], 2)
return sqrt(s)

def clusterOfElement(means, element):
min_dist = distEclud(means[0], element)
lable = 0
for index in range(1, len(means)):
dist = distEclud(means[index], element)
if(dist < min_dist):
min_dist = dist
lable = index
return lable

def getMean(cluster): #cluster=[[[1,2],[1,2],[1,2]....],[[2,1],[2,1],[2,1],[2,1]...]]
num = len(cluster) #1個簇的num,如上為3個
res = []
temp = 0
dim = len(cluster[0])
for i in range(0, dim):
for j in range(0, num):
temp = temp + cluster[j][i]
temp = temp / num
res.append(temp)
return res

def kMeans():
k = 3
data = loadDataSet('data.txt')
print "data is ", data
inite_mean = [[1.1, 1], [1, 1],[1,2]]

count = 0
while(count < 1000):
count = count + 1
clusters = []
means = []
for i in range(k):
clusters.append([])
means.append([])

for index in range(len(data)):
lable = clusterOfElement(inite_mean, data[index])
clusters[lable].append(data[index])

for cluster_index in range(k):
mea = getMean(clusters[cluster_index])
for mean_dim in range(len(mea)):
means[cluster_index].append(mea[mean_dim])

for mm in range(len(means)):
for mmm in range(len(means[mm])):
inite_mean[mm][mmm] = means[mm][mmm]

print "result cluster is ", clusters
print "result means is ", inite_mean

kMeans()

⑻ python聚類每次結果相同么

python中用pdf_multivariate求解多維密度分布,然後用plot_surface畫三維曲面圖; 另外用matlab也!

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

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

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

Figure 1

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

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

技術路線:
sklearn.cluster.Kmeans

數據實例:

⑽ 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

熱點內容
循跡小車演算法 發布:2024-12-22 22:28:41 瀏覽:82
scss一次編譯一直生成隨機數 發布:2024-12-22 22:04:24 瀏覽:956
嫁接睫毛加密 發布:2024-12-22 21:50:12 瀏覽:975
linuxbin文件的安裝 發布:2024-12-22 21:46:07 瀏覽:798
vlcforandroid下載 發布:2024-12-22 21:45:26 瀏覽:664
電腦做網關把數據發送至伺服器 發布:2024-12-22 21:44:50 瀏覽:431
新華三代理什麼牌子的伺服器 發布:2024-12-22 21:33:21 瀏覽:342
歡太會員密碼是什麼 發布:2024-12-22 20:57:28 瀏覽:74
sqllocaldb 發布:2024-12-22 20:07:08 瀏覽:126
如何找到我的伺服器 發布:2024-12-22 19:52:14 瀏覽:301