我使用sklearn工具已经有一段时间了,但是我现在有一个奇怪的问题。我只是想用sklearn构建一个KNN回归器,但是执行过程要花很长时间,在调试时,fit函数的执行似乎要花很长时间。 我应该提到数据集很大(大约25000条记录,有10个特性),但我仍然等了一个小时,没有结果。你知道吗
是什么原因造成的?你知道吗
相关代码:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 42)
regressor = neighbors.KNeighborsRegressor(n_neighbors = n, algorithm = algorithm, weights = weights)
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)
train_score = regressor.score(X_train, y_train)
test_score = regressor.score(X_test, y_test)
这就是KNN的本质。算法的时间复杂度是KNN is
O(nd)
,其中n是数据点的数量,d是维度(在您的例子中n = 25k, d = 10
)KNN将考虑所有的数据点,并选取最靠近的数据点。因此,如果您的数据很大,则需要更多的时间。
K
是超参数&您可以使用GridSearchCV或RandomizedSearchCV对其进行调优您可以选择:
您还可以尝试使用RBF内核SVM,因为它是KNN的一个很好的近似值&它将花费更少的时间
相关问题 更多 >
编程相关推荐