我正在尝试使用adspy
包绘制我的KNN分类器的决策边界,但每当我使用此包时,它都不会导入。我已经使用conda
提示下载了几次但没有发生任何事情。
包含错误消息的代码:
from adspy_shared_utilities import plot_fruit_knn
plot_fruit_knn(X_train, y_train, 5, 'uniform')
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-7-ddf0c07df9f1> in <module>()
----> 1 from adspy_shared_utilities import plot_fruit_knn
2
3 plot_fruit_knn(X_train, y_train, 5, 'uniform')
ModuleNotFoundError: No module named 'adspy_shared_utilities'
我该如何解决这个问题?
答案 0 :(得分:1)
没有名为adspy_shared_utilities的模块,但这是一些与课程材料一起保存的脚本。您应该将脚本保存在保存python文件的同一目录中。
答案 1 :(得分:1)
没有这样的模块。 您可以使用以下代码使数据可视化-
import matplotlib.cm as cm
from matplotlib.colors import ListedColormap, BoundaryNorm
import matplotlib.patches as mpatches
import matplotlib.patches as mpatches
X = df[['mass', 'width', 'height', 'color_score']]
y = df['fruit_label']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
def plot_fruit_knn(X, y, n_neighbors, weights):
X_mat = X[['height', 'width']].values
y_mat = y.values
# Create color maps
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF','#AFAFAF'])
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF','#AFAFAF'])
clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weights)
clf.fit(X_mat, y_mat)
# Plot the decision boundary by assigning a color in the color map
# to each mesh point.
mesh_step_size = .01 # step size in the mesh
plot_symbol_size = 50
x_min, x_max = X_mat[:, 0].min() - 1, X_mat[:, 0].max() + 1
y_min, y_max = X_mat[:, 1].min() - 1, X_mat[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, mesh_step_size),
np.arange(y_min, y_max, mesh_step_size))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
# Plot training points
plt.scatter(X_mat[:, 0], X_mat[:, 1], s=plot_symbol_size, c=y, cmap=cmap_bold, edgecolor = 'black')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
patch0 = mpatches.Patch(color='#FF0000', label='apple')
patch1 = mpatches.Patch(color='#00FF00', label='mandarin')
patch2 = mpatches.Patch(color='#0000FF', label='orange')
patch3 = mpatches.Patch(color='#AFAFAF', label='lemon')
plt.legend(handles=[patch0, patch1, patch2, patch3])
plt.xlabel('height (cm)')
plt.ylabel('width (cm)')
#plt.title("4-Class classification (k = %i, weights = '%s')" % (n_neighbors, weights))
plt.show()
plot_fruit_knn(X_train, y_train, 5, 'uniform')
这将给出输出图,如下所示 enter image description here
答案 2 :(得分:0)
相反,您可以将文件adspy_shared_utilities.py
直接放在脚本中或Jupyter笔记本目录中。这将直接导入adspy,而不会出现任何错误。
答案 3 :(得分:0)
如果要查找脚本,请将下面的adspy_shared_utilities代码复制到与python脚本相同的文件夹中
a