定义 k-1 簇质心 -- SKlearn KMeans
Define k-1 cluster centroids -- SKlearn KMeans
我正在对部分标记的数据集执行二元分类。我对它的 1 有一个可靠的估计,但对它的 0 没有。
来自 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.
我想传递一个 ndarray,但我只有 1 个可靠的质心,而不是 2 个。
有没有办法最大化第K-1个质心和第K个质心之间的熵?或者,有没有办法手动初始化 K-1 个质心并使用 K++ 处理剩余的质心?
=========================================== ==========
相关问题:
试图定义具有 n-1 个特征的 K 个质心。 (我想用 n 个特征定义 k-1 个质心)。
,但其中一位开发人员将其解释为错误,并且“很容易实施[能够]”
我有理由相信这会按预期工作,但如果您发现错误,请纠正我。 (从 geeks for geeks 拼凑而成):
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:]
它的用法:
# 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)
其中 presence_seed 和 absence_seed 是已知的质心位置。
我正在对部分标记的数据集执行二元分类。我对它的 1 有一个可靠的估计,但对它的 0 没有。
来自 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.
我想传递一个 ndarray,但我只有 1 个可靠的质心,而不是 2 个。
有没有办法最大化第K-1个质心和第K个质心之间的熵?或者,有没有办法手动初始化 K-1 个质心并使用 K++ 处理剩余的质心?
=========================================== ==========
相关问题:
我有理由相信这会按预期工作,但如果您发现错误,请纠正我。 (从 geeks for geeks 拼凑而成):
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:]
它的用法:
# 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)
其中 presence_seed 和 absence_seed 是已知的质心位置。