gpt4 book ai didi

python - 如何为 Python 实现此导入语句?

转载 作者:行者123 更新时间:2023-12-01 01:41:26 25 4
gpt4 key购买 nike

我在网上找到了一个用于亲和性传播的 python 模块。该代码可在此链接中找到。 https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/cluster/affinity_propagation_.py#L256

我已获取代码并将其放入名为affinitypropagationlib.py 的文件中。

我正在尝试创建一个“主”python 模块,该模块导入上面的 python 文件,但收到以下错误。

Warning (from warnings module):
File "C:\Users\Br. David Klecker\Downloads\WPy-3701\python-3.7.0.amd64\lib\site-packages\sklearn\utils\__init__.py", line 4
from collections import Sequence
DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
Traceback (most recent call last):
File "C:\Users\Br. David Klecker\Downloads\WPy-3701\notebooks\ap.py", line 4, in <module>
import affinitypropagationlib
File "C:\Users\Br. David Klecker\Downloads\WPy-3701\notebooks\affinitypropagationlib.py", line 12, in <module>
from ..base import BaseEstimator, ClusterMixin
ImportError: attempted relative import with no known parent package

我的 ap.py(我的主要 python 模块)的代码如下。

import matplotlib.pyplot as plt
import numpy as np
#from sklearn.cluster import AffinityPropagation
import affinitypropagationlib
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs

# generating sampl data
centers = [[5, 5], [0, 0], [1, 5],[5, -1]]
X, labels_true =make_blobs(n_samples=500, n_features=5, centers=centers, cluster_std=0.9, center_box=(1, 10.0), shuffle=True, random_state=0)

# Compute Affinity Propagation
af = AffinityPropagation(max_iter=150, preference =-120).fit(X)
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_

n_clusters_ = len(cluster_centers_indices)




#print results
print('Estimated number of clusters: %d' % n_clusters_)
print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels))
print("Completeness: %0.3f" % metrics.completeness_score(labels_true, labels))
print("V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels))
print("Adjusted Rand Index: %0.3f"% metrics.adjusted_rand_score(labels_true, labels))
print("Adjusted Mutual Information: %0.3f"% metrics.adjusted_mutual_info_score(labels_true, labels))
print("Silhouette Coefficient: %0.3f"% metrics.silhouette_score(X, labels))


# Drawing chart
# Plot result
import matplotlib.pyplot as plt
from itertools import cycle

plt.close('all')
plt.figure(1)
plt.clf()

colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
for k, col in zip(range(n_clusters_), colors):
class_members = labels == k
cluster_center = X[cluster_centers_indices[k]]
plt.plot(X[class_members, 0], X[class_members, 1], col + '.')
plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
for x in X[class_members]:
plt.plot([cluster_center[0], x[0]], [cluster_center[1], x[1]], col)

plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()

以下是affinitypropagationlib.py 文件中发生错误的开头几行。

import numpy as np
import warnings

from sklearn.exceptions import ConvergenceWarning
from ..base import BaseEstimator, ClusterMixin
from ..utils import as_float_array, check_array
from ..utils.validation import check_is_fitted
from ..metrics import euclidean_distances
from ..metrics import pairwise_distances_argmin

我对正在发生的事情感到茫然。我对 python 非常陌生,所以如果错误是基本的,我深表歉意。我的猜测是我仍然缺少affinitypropagation.lib 所调用的库,也许许多库名称之前的两个点可能是线索。

最佳答案

感谢评论者的帮助,我成功了!解决方案是简单地使用绝对导入而不是相对导入来包含库 sklearn

所以而不是

from ..base import BaseEstimator, ClusterMixin

只需使用

from sklearn.base import BaseEstimator, ClusterMixin. 

关于python - 如何为 Python 实现此导入语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51846548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com