Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python';数组'的索引太多;_Python_Dataframe_Numpy - Fatal编程技术网

Python';数组'的索引太多;

Python';数组'的索引太多;,python,dataframe,numpy,Python,Dataframe,Numpy,我使用以下代码来可视化决策树函数所做的预测。然而,我得到一个错误:索引器:数组的索引太多。我是python新手,有人能就这个错误提供建议吗 散点图数据散点图卫星数据。如果存在植被,则地块中的一点为“绿色”,否则为“灰色”。 输入: x_df-大小为nx2的数据帧,每行是一个位置,每列是一个坐标 y_系列-长度N的系列,每个条目为0(无植被)或1(植被) ax-要打印的轴 返回: ax-带有散点图的轴 下面是x_-train和y_-train是如何创建的 def scatter_plot_data

我使用以下代码来可视化决策树函数所做的预测。然而,我得到一个错误:索引器:数组的索引太多。我是python新手,有人能就这个错误提供建议吗

散点图数据散点图卫星数据。如果存在植被,则地块中的一点为“绿色”,否则为“灰色”。 输入: x_df-大小为nx2的数据帧,每行是一个位置,每列是一个坐标 y_系列-长度N的系列,每个条目为0(无植被)或1(植被) ax-要打印的轴 返回: ax-带有散点图的轴

下面是x_-train和y_-train是如何创建的

def scatter_plot_data(x_df, y_series, ax):

# convert x_df and y_series into numpy arrays
    x = x_df.values
    y = y_series.values
    
    ax.scatter(x[y == 1, 0], x[y == 1, 1], alpha=0.2, c='green', label='vegetation')
    ax.scatter(x[y == 0, 0], x[y == 0, 1], alpha=0.2, c='gray', label='nonvegetation')
    ax.set_xlim([0, 1])
    ax.set_ylim([0, 1])
    ax.set_xlabel('Latitude')
    ax.set_ylabel('Longitude')
    ax.legend(loc='best')
    return ax


    def plot_decision_boundary(x_df, y_series, model, ax, plot_boundary_only=False):
    
    # convert x_df and y_series into numpy arrays
    x = x_df.values
    y = y_series.values
    
    # Plot data
    if not plot_boundary_only:
        ax.scatter(x[y == 1, 0], x[y == 1, 1], alpha=0.2, c='green', label='vegetation')
        ax.scatter(x[y == 0, 0], x[y == 0, 1], alpha=0.2, c='gray', label='non-vegetation')
    
    # Create mesh
    interval = np.arange(0,1,0.01)
    n = np.size(interval)
    x1, x2 = np.meshgrid(interval, interval)
    x1 = x1.reshape(-1, 1)
    x2 = x2.reshape(-1, 1)
    xx = np.concatenate((x1, x2), axis=1)
    
    # Predict on mesh points
    yy = model.predict(xx)    
    yy = yy.reshape((n, n))

    # Plot decision surface
    x1 = x1.reshape(n, n)
    x2 = x2.reshape(n, n)
    if not plot_boundary_only:
        ax.contourf(x1, x2, yy, alpha=0.1, cmap='Greens')
    ax.contour(x1, x2, yy, colors='black', linewidths=0.1)
    ax.set_xlim([0, 1])
    ax.set_ylim([0, 1])
    ax.set_xlabel('Latitude')
    ax.set_ylabel('Longitude')
    ax.legend(loc='best')
    return ax
---------------------

    # set up to create two plots in the same image
    fig, ax = plt.subplots(1, 3, figsize=(15, 5))
    
    # visualize the data on the first plot (ax[0])
    scatter_plot_data(x_train, y_train, ax[0])
    ax[0].set_title('Training Data')
    
    #  plot the training data and decision tree boundary on the second plot (ax[1])
    plot_decision_boundary(x_train, y_train, tree, ax[1])
    ax[1].set_title('Decision Boundary on the Training Data')
    
    #  plot the test data and decision tree boundary on the third plot (ax[2])
    plot_decision_boundary(x_test, y_test, tree, ax[2])
    ax[2].set_title('Decision Boundary on the Test Data')

最后,错误是:

    IndexError                                Traceback (most recent call last)
    <ipython-input-12-51049b1cd664> in <module>
          3 
          4 # visualize the data on the first plot (ax[0])
    ----> 5 scatter_plot_data(x_train, y_train, ax[0])
          6 ax[0].set_title('Training Data')
          7 
    
    <ipython-input-11-0719bb156aa2> in scatter_plot_data(x_df, y_series, ax)
         16     y = y_series.values
         17 
    ---> 18     ax.scatter(x[y == 1, 0], x[y == 1, 1], alpha=0.2, c='green', label='vegetation')
         19     ax.scatter(x[y == 0, 0], x[y == 0, 1], alpha=0.2, c='gray', label='nonvegetation')
         20     ax.set_xlim([0, 1])
    
    IndexError: too many indices for array
索引器错误回溯(最近一次调用)
在里面
3.
4#可视化第一个绘图上的数据(ax[0])
---->5散点图数据(x列、y列、ax[0])
6 ax[0]。设置标题(“培训数据”)
7.
在散点图数据中(x_df,y_系列,ax)
16 y=y_系列数值
17
--->18最大散射(x[y==1,0],x[y==1,1],α=0.2,c='绿色',标签='植被〕)
19 ax.散射(x[y==0,0],x[y==0,1],α=0.2,c='gray',label='NonVegation')
20 ax.set_xlim([0,1])
索引器:数组的索引太多

实际的MCVE总是比代码转储更有用。您负责自己的基本调试,包括检查数组尺寸、搜索错误消息等。回溯表明,这种情况下的错误来自
x\u列
y\u列
数组,然而,你实际上没有想到任何地方向我们展示这些数组是如何创建的。我添加了一个图像,其中包含我用来创建x_train的前5行代码,y_train数组形状在某个地方不匹配,请尝试在出错前检查
x
y
一行。我正在添加上传到github的数据集的链接,我不确定这是否有用。但是,数据集看起来很好。[连结]