我有以下數據:Python:如何在k-means中將特定數據點的初始質心?
import pandas as pd
import random
import matplotlib.pyplot as plt
df = pd.DataFrame()
df['x'] = [3, 2, 4, 3, 4, 6, 8, 7, 8, 9]
df['y'] = [3, 2, 3, 4, 5, 6, 5, 4, 4, 3]
df['val'] = [1, 10, 1, 1, 1, 8, 1, 1, 1, 1]
k = 2
centroids = {i + 1: [np.random.randint(0, 10), np.random.randint(0, 10)] for i in range(k)}
plt.scatter(df['x'], df['y'], color='blue')
for i in centroids.keys():
plt.scatter(*centroids[i], color='red', marker='^')
plt.show()
我希望把數據點的初始質心與最高值。然後,在這種情況下,質心應位於座標爲(2,2)和(6,6)的數據點上。
x y val
0 3 3 1
1 2 2 10
2 4 3 1
3 3 4 1
4 4 5 1
5 6 6 8
6 8 5 1
7 7 4 1
8 8 4 1
9 9 3 1
您在使用scikit的KMeans估計器學習?如果是這樣,你可以通過一個數組給予初始中心。請參閱'init'參數[here](http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html)。或者你是問如何構建這個數組呢? –
@MarkDickinson是的,我問如何編寫Python代碼讓我把質心放在具有最高值的節點上,因爲我沒有在這裏使用scikit學習。我爲kmeans寫了自己的代碼。 – arizamoona