Perzeptron算法代码错误Python 3

2024-09-28 03:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读塞巴斯蒂安·拉什卡的德语书《机器学习与Python》。 我在windows机器上使用anaconda和spyder(包括ipython控制台)

在第三章中,他依赖于一种基于“珀兹普顿模型”的算法。 按照作者的说明,代码应如下所示:

from sklearn.metrics import accuracy_score
print('Korrektklassifizierungsrate: %.2f' % accuracy_score(y_test, y_pred))

from matplotlib.colors import ListedColormap
def plot_decision_region(X, y, classifier, resolution=0.02):

    # Markierungen und Farben einstellen  
    markers = ('s', 'x', 'o', '^', 'v')
    colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
    cmap = ListedColormap(colors[:len(np.unique(y))])

    #Plotten der Entscheidungsgrenze
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, \
         resolution), np.arange(x2_min, x2_max, resolution))
    Z = classifier.predict(np.array([xx1.ravel(), \
                                     xx2.ravel()]).T)

    Z = Z.reshape(xx1.shape)
                  plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap),
                  plt.xlim(xx1.min(), xx1.max())
                  plt.ylim(xx2.min(), xx2.max())

    #Plotten aller Exemplare
    for idx, cl in enumerate(np.unique(y)):
        plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
                    alpha=0.8, c=cmap(idx),
                    marker=markers[idx], label=cl)

    #Exemplare der Testdatenmenge hervorheben
    if test_idx:
        X_test, y_test = X[test_idx, :], y[test_idx]
        plt.scatter(X_test[:, 0], X_test[:, 1], c='',
                    alpha=1.0, linewidths=1, marker='o' s=55, label='test set')

X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined = np.hstack((y_train, y_test))
plot_decision_regions(X=X_combined_std,
                      y=y_combined,
                      classifier=ppn,
                      test_idx=range(105,150))
plt.xlabel('Länge des Blütenblatts [standardisiert]')
plt.ylabel('Breite des Blütenblatts [standardisiert]')
plt.legend(loc='upper left')
plt.show()
  **File "<ipython-input-1-e576c1f255dd>", line 19
    plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap),
    ^
IndentationError: unexpected indent**

所以,我不确定默认值是什么。我真的很想理解错误,如果有人能帮助我,我会很感激的。有没有可能,它和cmap=cmap的方程有关

敬礼 费利克斯


Tags: testalphaclnppltminmaxcmap
1条回答
网友
1楼 · 发布于 2024-09-28 03:22:55

从19到21的未缩进行

Z = Z.reshape(xx1.shape)
plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap),
plt.xlim(xx1.min(), xx1.max())
plt.ylim(xx2.min(), xx2.max())

相关问题 更多 >

    热门问题