dimension is 100 but corresponding boolean dimension is 101

来源:3-5 神经网络对数据实现分类(下)

qq_燕窝_2

2019-03-11 11:18

IndexError                                Traceback (most recent call last) 

<ipython-input-45-a65b21b8df17> in <module>

 ----> 1 plot_decision_regions(X,y,ppn,resolution=0.02)       

2 plt.xlabel('花径长度')       

3 plt.ylabel('花瓣长度')      

4 plt.legend(loc='upper left')      

5 plt.show()

<ipython-input-44-85f310d48d44> in plot_decision_regions(X, y, classifier, resolution)     

30      

31     for idx,cl in enumerate(np.unique(y)):

---> 32         plt.scatter(x=X[y==cl,0],y=X[y==cl,1],alpha=0.8,c=cmap(idx),marker=marker[idx],label=cl)

IndexError: boolean index did not match indexed array along dimension 0; dimension is 100 but corresponding boolean dimension is 101

写回答 关注

2回答

  • Du1in9
    2020-07-20 11:48:10

    望采纳~~

    import numpy as np
    import pandas as pd 
    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.colors import ListedColormap
    
    class Perceptron(object):
        def __init__(self, eta = 0.01, n_iter = 10):
            self.eta = eta
            self.n_iter = n_iter
        def fit(self, X, y):
            self.w_ = np.zeros(1 + X.shape[1])
            self.errors_ = []
            for _ in range(self.n_iter):
                errors = 0
                for xi, target in zip(X, y):
                    update = self.eta * (target - self.predict(xi))
                    self.w_[1:] += update * xi
                    self.w_[0] += update
                    errors += int(update != 0.0)
                    self.errors_.append(errors)
        def net_input(self, X):
            return np.dot(X, self.w_[1:]) + self.w_[0]
        def predict(self, X):
            return np.where(self.net_input(X) >= 0.0, 1, -1)
            
    file = "C:/Users/YYDL/Desktop/data.csv"
    df = pd.read_csv(file, header = None)
    y = df.loc[0:100, 4].values
    y = np.where(y == 'Iris-setosa', -1, 1)
    X = df.iloc[0:100, [0, 2]].values
    ppn = Perceptron(eta=0.1, n_iter=10)
    ppn.fit(X, y)
    
    def plot_decision_region(X, y, classifier, resolution=0.02):
        marker = ('s', 'x', 'o', 'v')
        colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
        cmap = ListedColormap(colors[:len(np.unique(y))])
        x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max()
        x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max()
        print(x1_min, x1_max)
        print(x2_min, x2_max)
        xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution))
        print(np.arange(x1_min, x1_max, resolution).shape)
        print(np.arange(x1_min, x1_max, resolution))
        print(xx1.shape)
        print(xx1)
        print(np.arange(x2_min, x2_max, resolution).shape)
        print(np.arange(x2_min, x2_max, resolution))
        print(xx2.shape)
        print(xx2)
    plot_decision_region(X, y, ppn, resolution=0.02)


  • Parva
    2019-03-28 15:09:45

    y = df.loc[0:100, 4].values改为y = df.iloc[0:100, 4].values

    看出区别了吗?loc前面多个i。不然y的维度为101。

    当然你也可以直接改成y = df.loc[0:99, 4].values

机器学习-实现简单神经网络

人工智能时代,你准备好成为抓住机遇的那百分之二吗。

66747 学习 · 182 问题

查看课程

相似问题