Zdefiniuj centroidy klastra k-1 - SKlearn KMeans
Przeprowadzam binarną klasyfikację częściowo oznaczonego zbioru danych. Mam wiarygodne oszacowanie jego 1, ale nie 0.
Z dokumentacji sklearn KMeans:
init : {‘k-means++’, ‘random’ or an ndarray}
Method for initialization, defaults to ‘k-means++’:
If an ndarray is passed, it should be of shape (n_clusters, n_features) and gives the initial centers.
Chciałbym przekazać ndarray, ale mam tylko 1 niezawodny centroid, a nie 2.
Czy istnieje sposób na maksymalizację entropii między centroidami K-1 a Kth? Alternatywnie, czy istnieje sposób, aby ręcznie zainicjować centroidy K-1 i użyć K ++ dla pozostałych?
==================================================== =====
Powiązane pytania:
Ma to na celu zdefiniowanie centroid K z cechami n-1. (Chcę zdefiniować centroidy k-1 z n cechami).
Oto opis tego, czego chcę , ale został zinterpretowany przez jednego z programistów jako błąd i jest „łatwy do wdrożenia [w stanie]”
Odpowiedzi
Jestem pewien, że to działa zgodnie z przeznaczeniem, ale proszę poprawić mnie, jeśli zauważysz błąd. (zebrane razem od geeków dla geeków ):
import sys
def distance(p1, p2):
return np.sum((p1 - p2)**2)
def find_remaining_centroid(data, known_centroids, k = 1):
'''
initialized the centroids for K-means++
inputs:
data - Numpy array containing the feature space
known_centroid - Numpy array containing the location of one or multiple known centroids
k - remaining centroids to be found
'''
n_points = data.shape[0]
# Initialize centroids list
if known_centroids.ndim > 1:
centroids = [cent for cent in known_centroids]
else:
centroids = [np.array(known_centroids)]
# Perform casting if necessary
if isinstance(data, pd.DataFrame):
data = np.array(data)
# Add a randomly selected data point to the list
centroids.append(data[np.random.randint(
n_points), :])
# Compute remaining k-1 centroids
for c_id in range(k - 1):
## initialize a list to store distances of data
## points from nearest centroid
dist = np.empty(n_points)
for i in range(n_points):
point = data[i, :]
d = sys.maxsize
## compute distance of 'point' from each of the previously
## selected centroid and store the minimum distance
for j in range(len(centroids)):
temp_dist = distance(point, centroids[j])
d = min(d, temp_dist)
dist[i] = d
## select data point with maximum distance as our next centroid
next_centroid = data[np.argmax(dist), :]
centroids.append(next_centroid)
# Reinitialize distance array for next centroid
dist = np.empty(n_points)
return centroids[-k:]
Jego użycie:
# For finding a third centroid:
third_centroid = find_remaining_centroid(X_train, np.array([presence_seed, absence_seed]), k = 1)
# For finding the second centroid:
second_centroid = find_remaining_centroid(X_train, presence_seed, k = 1)
Gdzie present_seed i missing_seed to znane lokalizacje centroidów.