- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 Python 中从头开始进行 K-Means 聚类。这是我的代码,我重新定义质心的方式有问题
这是我得到的输出:
Iteration 1:
[1.5, 8.1] [8.04, 1.525]
Iteration 2:
[4.98, 4.05] [2.87, 4.09]
Iteration 3:
[9.29, 8.28] [8.57, 7.87]
Iteration 4:
[9.97, 8.94] [inf, inf]
提前致谢!
# Example dataset
data = pd.DataFrame({'x' : [6.480, 7.320, 4.380, 8.040, 7.680, 6.600, 6.420, 5.940, 4.140, 5.700,
7.500, 7.620, 6.840, 7.500, 4.920, 3.780, 7.860, 4.260, 7.980, 6.840,
3.025, 2.300, 3.250, 2.975, 3.325, 1.500, 1.875, 2.850, 1.600, 2.525,
2.900, 2.175, 2.050, 1.650, 2.250, 3.475, 1.800, 2.975, 3.025, 2.175 ],
'y' : [6.300, 5.220, 6.060, 4.560, 7.080, 4.740, 3.660, 4.680, 4.800, 5.880,
8.100, 7.800, 3.900, 6.780, 4.860, 5.100, 4.380, 5.160, 5.520, 5.700,
2.125, 3.475, 2.500, 2.875, 2.075, 3.350, 1.525, 3.050, 2.950, 2.150,
2.125, 2.550, 3.375, 1.950, 1.700, 2.400, 2.525, 2.525, 2.675, 3.325]})
data['Cluster'] = 0
data['EuclideanDist1'] = 0
data['EuclideanDist2'] = 0
data['EuclideanDistD'] = 0
iterations = 0
C1nx = C1ny = 0
C2nx = C2ny = 0
C1c = 0
C2c = 0
C1 = [min(data['x']), max(data['y'])]
C2 = [max(data['x']), min(data['y'])]
count = 0
while(iterations < 40):
print(C1, C2)
for count in range(0, len(data)-1):
data['EuclideanDist1'][count] = ((data['x'][count] - C1[0])**2 + (data['y'][count] - C1[1])**2)**(0.5)
data['EuclideanDist2'][count] = ((data['x'][count] - C2[0])**2 + (data['y'][count] - C2[1])**2)**(0.5)
data['EuclideanDistD'][count] = data['EuclideanDist1'] [count]- data['EuclideanDist2'][count]
if data['EuclideanDistD'][count] >= 0:
data['Cluster'][count] = 1
C1nx = C1nx + data['x'][count]
C1ny = C1ny + data['y'][count]
C1c = C1c + 1
elif data['EuclideanDistD'][count] < 0:
data['Cluster'][count] = 2
C2nx = C2nx + data['x'][count]
C2ny = C2ny + data['y'][count]
C2c = C2c + 1
C1[0] = (C1nx / C1c)
C1[1] = (C1ny / C1c)
C2[0] = (C2nx / C2c)
C2[1] = (C2ny / C2c)
C1n = [0,0]
C2n = [0,0]
C1c = 0
C2c = 0
iterations = iterations + 1
最佳答案
避免在循环内堆叠查找[a][b]
。它的可读性不太好,而且速度很慢。适当使用局部变量。由于 Python 在解释器模式下相当慢,因此请尽可能使用矢量化 numpy 运算,以从更快的 C/Fortran 代码中受益。
关于python - 在 Python 中从头开始 K 均值聚类 - 重新定义质心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52066593/
我想获取每一行某些列的平均值。 我有此数据: w=c(5,6,7,8) x=c(1,2,3,4) y=c(1,2,3) length(y)=4 z=data.frame(w,x,y) 哪个返回:
类似于Numpy mean with condition我的问题将其扩展到对矩阵进行操作:计算矩阵 rdat 的行均值,跳过某些单元格 - 在本例中我使用 0 作为要跳过的单元格 - 就好像这些值从一
我有一个数据集,其中的列标题为产品名称、品牌、评级(1:5)、评论文本、评论有用性。我需要的是提出一个使用评论的推荐算法。我这里必须使用 python 进行编码。数据集采用.csv 格式。 为了识别数
我在 R^3 中有 n 个点,我想用 k 个椭球体或圆柱体覆盖它们(我不在乎;以更容易的为准)。我想大约最小化卷的并集。假设 n 是数万,k 是少数。开发时间(即简单性)比运行时更重要。 显然我可以运
我创建了一个计算均值、中位数和方差的程序。该程序最多接受 500 个输入。当有 500 个输入(我的数组的最大大小)时,我的所有方法都能完美运行。当输入较少时,只有“平均值”计算器起作用。这是整个程序
我已经完成了距离的计算并存储在推力 vector 中,例如,我有 2 个质心和 5 个数据点,我计算距离的方法是,对于每个质心,我首先计算 5 个数据点的距离并存储在阵列,然后与距离一维阵列中的另一个
下面的代码适用于每一列的总数,但我想计算出每个物种的平均值。 # Read data file into array data = numpy.genfromtxt('data/iris.csv',
我有一个独特的要求,我需要两个数据帧的公共(public)列(每行)的平均值。 我想不出这样做的 pythonic 方式。我知道我可以遍历两个数据框并找到公共(public)列,然后获取键匹配的行的平
我把它扔在那里,希望有人会尝试过这种荒谬的事情。我的目标是获取输入图像,并根据每个像素周围小窗口的标准差对其进行分割。基本上,这在数学上应该类似于高斯或盒式过滤器,因为它将应用于编译时(甚至运行时)用
有没有一种方法可以对函数进行向量化处理,使输出成为均值数组,其中每个均值代表输入数组的 0 索引值的均值?循环这个非常简单,但我正在努力尽可能高效。例如0 = 均值(0),1 = 均值(0-1),N
我正在尝试生成均值为 1 的指数分布随机数。我知道如何获取具有均值和标准差的正态分布随机数。我们可以通过normal(mean, standard_deviation)得到它,但是我不知道如何得到指数
我遇到了一段 Python 代码,它的内容类似于以下内容: a = np.array([1,2,3,4,5,6,7]) a array([1, 2, 3, 4, 5, 6, 7]) np.mean(a
我有两个数组。 x 是独立变量,counts 是 x 出现的次数,就像直方图一样。我知道我可以通过定义一个函数来计算平均值: def mean(x,counts): return np.sum
我有在纯 python 中计算平均速度的算法: speed = [...] avg_speed = 0.0 speed_count = 0 for i in speed: if i > 0:
我正在尝试计算扩展窗口的平均值,但是数据结构使得之前的答案至少缺少一点所需的内容(最接近的是:link)。 我的数据看起来像这样: Company TimePeriod IndividualID
我正在尝试实现 Kmeans python中的算法将使用cosine distance而不是欧几里得距离作为距离度量。 我知道使用不同的距离函数可能是致命的,应该小心使用。使用余弦距离作为度量迫使我改
有谁知道自组织映射 (SOM) 与 k 均值相比效果如何?我相信通常在颜色空间(例如 RGB)中,SOM 是将颜色聚类在一起的更好方法,因为视觉上不同的颜色之间的颜色空间存在重叠( http://ww
注意:我希望能得到更多有关如何处理和提出此类解决方案的指南,而不是解决方案本身。 我的系统中有一个非常关键的功能,它在特定上下文中显示为排名第一的分析热点。它处于 k-means 迭代的中间(已经是多
我有一个 pandas 数据框,看起来像这样: 给定行中的每个值要么是相同的数字,要么是 NaN。我想计算数据框中所有两列组合的平均值、中位数和获取计数,其中两列都不是 NaN。 例如,上述数据帧的结
任何人都知道如何调整简单的 K 均值算法来处理 this form 的数据集. 最佳答案 在仍然使用 k-means 的同时处理该形式的数据的最直接方法是使用 k-means 的内核化版本。 JSAT
我是一名优秀的程序员,十分优秀!