Python 永远运行的函数
我有一个家庭作业问题,我必须组合几个函数来返回涉及kmeans集群的特定输出 我们不需要使用sklearn的kmeans模块,而是需要将一些预先编写的函数与我们自己的余弦相似距离计算器结合起来,这些函数复制kmeans模块的“幕后”功能。我能够找到代码来帮助组装余弦相似性计算器(“余弦sim”),然后讲师给了我们执行kmeans聚类的代码(“randCent”和“kmeans”)。这三个功能如下所示 为了编写使用这些输出的函数,我尝试运行这个kmeans函数来查看它创建的输出的形状/类型。然而,它似乎永远在运行。考虑到我的训练数据有2000行,共有9328列(词频),这并不奇怪。我让它一夜之间运行了9-10个小时,今天早上它仍然在运行。我试着制作一个更小的训练集——即使是只有5排的训练集,它仍然可以永远运行。我也在另一台电脑上试过,但效果相同 代码中是否存在问题(例如被卡在循环中),或者需要几天才能完全运行 谢谢你的帮助Python 永远运行的函数,python,performance,time,jupyter-notebook,Python,Performance,Time,Jupyter Notebook,我有一个家庭作业问题,我必须组合几个函数来返回涉及kmeans集群的特定输出 我们不需要使用sklearn的kmeans模块,而是需要将一些预先编写的函数与我们自己的余弦相似距离计算器结合起来,这些函数复制kmeans模块的“幕后”功能。我能够找到代码来帮助组装余弦相似性计算器(“余弦sim”),然后讲师给了我们执行kmeans聚类的代码(“randCent”和“kmeans”)。这三个功能如下所示 为了编写使用这些输出的函数,我尝试运行这个kmeans函数来查看它创建的输出的形状/类型。然而,
## Import Modules
from numpy import *
import pandas as pd
from os import chdir
import matplotlib.pyplot as plt
%matplotlib inline
## Cosine distance calculation function (that I found elsewhere)
def cosine_sim (vec1, vec2):
x_sqr_sum = 0
y_sqr_sum = 0
xy_sum = 0
for i in range(len(vec1)):
x = vec1[i]
y = vec2[i]
x_sqr_sum += x*x
y_sqr_sum += y*y
xy_sum += x*y
return xy_sum/sqrt(x_sqr_sum*y_sqr_sum)
## Function that creates centroids (provided by instructor)
def randCent(dataSet, k):
n = shape(dataSet)[1]
centroids = zeros((k,n), dtype=float)
for j in range(n): #create random cluster centers
minJ = min(dataSet[:,j])
rangeJ = float(max(dataSet[:,j]) - minJ)
centroids[:,j] = minJ + rangeJ * random.rand(k)
return centroids
## Function that performs kMeans clustering (provided by instructor and calls the previous two functions
def kMeans(dataSet, k, distMeas=cosine_sim, createCent=randCent):
m = shape(dataSet)[0]
clusterAssment = zeros((m,2))#create mat to assign data points
#to a centroid, also holds SE of each point
centroids = createCent(dataSet, k)
clusterChanged = True
while clusterChanged:
clusterChanged = False
for i in range(m):#for each data point assign it to the closest centroid
minDist = inf; minIndex = -1
for j in range(k):
distJI = distMeas(centroids[j,:],dataSet[i,:])
if distJI < minDist:
minDist = distJI; minIndex = j
if clusterAssment[i,0] != minIndex: clusterChanged = True
clusterAssment[i,:] = minIndex,minDist**2
#print (centroids)
for cent in range(k):#recalculate centroids
ptsInClust = dataSet[nonzero(clusterAssment[:,0]==cent)[0]] #get all the point in this cluster - Note: this was incorrect in the original distribution.
if(len(ptsInClust)!=0):
centroids[cent,:] = mean(ptsInClust, axis=0)
return centroids, clusterAssment
导入模块
从numpy进口*
作为pd进口熊猫
从操作系统导入chdir
将matplotlib.pyplot作为plt导入
%matplotlib内联
##余弦距离计算函数(我在别处找到)
def余弦sim(vec1、vec2):
x_sqr_和=0
y_sqr_和=0
xy_和=0
对于范围内的i(len(vec1)):
x=vec1[i]
y=vec2[i]
x_sqr_sum+=x*x
y_sqr_sum+=y*y
xy_和+=x*y
返回xy_和/sqrt(x_和*y和)
##创建质心的功能(由讲师提供)
def randCent(数据集,k):
n=形状(数据集)[1]
质心=零((k,n),数据类型=浮点)
对于范围(n)内的j:#创建随机聚类中心
minJ=min(数据集[:,j])
rangeJ=浮点(最大值(数据集[:,j])-minJ)
质心[:,j]=minJ+rangeJ*random.rand(k)
返回质心
##执行kMeans群集的函数(由讲师提供并调用前两个函数
def kMeans(数据集,k,distMeas=cosine_sim,createCent=randCent):
m=形状(数据集)[0]
clusterAssment=零((m,2))#创建mat以分配数据点
#对于质心,还保留每个点的SE
质心=createCent(数据集,k)
clusterChanged=True
虽然集群发生了变化:
clusterChanged=False
对于范围(m)内的i:#对于每个数据点,将其指定给最近的质心
minDist=inf;minIndex=-1
对于范围(k)内的j:
distJI=distMeas(质心[j,:],数据集[i,:])
如果是明德主义者:
minDist=distJI;minIndex=j
如果clusterAssment[i,0]!=minIndex:clusterChanged=True
集群协会[i,:]=minIndex,明德主义者**2
#打印(质心)
对于范围内的百分比(k):#重新计算质心
ptsInClust=dataSet[nonzero(clusterAssment[:,0]==cent)[0]]#获取此集群中的所有点-注意:这在原始分发中是不正确的。
如果(len(ptsInClust)!=0):
质心[cent,:]=平均值(ptsInClust,轴=0)
返回质心,群集关联
唯一明显的地方是
如果clusterAssment[i,0]!=minIndex:clusterChanged=True
-你确定clusterAssment
最终/曾经等于minIndex
?另外,当你运行clusterAssment[i,:]=minIndex,mindsist**2
,这会改变clusterAssment[i,0]
?如果没有,它将永远运行,因为它从未更改过。谢谢。我想这就是问题所在。我更改了while循环,使其只运行100次迭代。如果这解决了问题,请随时与我们联系!对不起,我不仅是一个编程高手,而且在这个网站上我仍然是一个有点像傻瓜的人,我想确保我遵守规则/保持精简gs尽可能的帮助。如果修改后的while循环有效,我需要做什么?我是否对我的原始帖子进行了更改?如果有效,请告诉我,我会将我的评论作为答案发布。然后可以接受该答案,未来的用户将知道什么是有效的。