我试图用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'
我该怎么解决这个问题?
发布于 2018-04-17 02:14:21
没有一个名为adspy_shared_utilities的模块,但这是material.You课程中保存的一些脚本,应该将脚本保存在保存python文件的同一个目录中。
发布于 2018-12-31 11:41:16
没有这样的模块。你可以用下面的代码让数据可视化-
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')
这将给出如下所示的输出数字
发布于 2019-06-27 12:09:01
相反,您可以将文件adspy_shared_utilities.py
直接放在脚本或木星笔记本目录中。这将直接导入鉴赏,不会出现任何错误。
https://stackoverflow.com/questions/48019360
复制相似问题