源代码
"""
date:0328
K均值
KMeans模型
"""
import pandas as pd
df = pd.read_csv("D:/mechine learning/transfer-time-location.csv", index_col=0,encoding='GBK')
df[:3]
from sklearn.cluster import KMeans
model = KMeans(n_clusters=7)
X = df['transfer_time']
model.fit(X)
print('簇中心',model.cluster_centers_, '\n')
print(model.labels_, '\n')
print(model.inertia_, '\n') # 所有点到簇中心的距离平方和
print(iris.target)
# 划分数据集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(df['transfer_time'],
iris['location'],
test_size=0.2)
# 用fit()训练
from sklearn.cluster import KMeans
model = KMeans(n_clusters=7)
model.fit(X_train[:, 0:2])
# 预测
index_pred = model.predict(X_test[:, 0:2])
print(index_pred)
print(y_test)
报错:
ValueError: Expected 2D array, got 1D array instead:
array=[123. 968. 368. ... 546. 360. 308.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
解决思路:
参考代码