代码之家  ›  专栏  ›  技术社区  ›  JonTargaryen

Python脚本“预期2D数组,改为1D数组”中出错:?

  •  107
  • JonTargaryen  · 技术社区  · 7 年前

    我在跟踪 this tutorial 要进行ML预测:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib import style
    
    style.use("ggplot")
    from sklearn import svm
    
    x = [1, 5, 1.5, 8, 1, 9]
    y = [2, 8, 1.8, 8, 0.6, 11]
    
    plt.scatter(x,y)
    plt.show()
    
    X = np.array([[1,2],
                 [5,8],
                 [1.5,1.8],
                 [8,8],
                 [1,0.6],
                 [9,11]])
    
    y = [0,1,0,1,0,1]
    X.reshape(1, -1)
    
    clf = svm.SVC(kernel='linear', C = 1.0)
    clf.fit(X,y)
    
    print(clf.predict([0.58,0.76]))
    

    我使用的是Python 3.6,我得到一个错误“预期的是2D数组,而实际的是1D数组:” 我认为这个脚本适用于旧版本,但我不知道如何将其转换为3.6版本。

    X.reshape(1, -1)
    
    11 回复  |  直到 5 年前
        1
  •  202
  •   Ofer Sadan    5 年前

    你只需要提供 predict 方法具有相同的2D数组,但具有一个要处理的值(或多个)。简而言之,你只需更换

    [0.58,0.76]
    

    具有

    [[0.58,0.76]]
    

    它应该会起作用。

    预测 与训练数据维数相同的数据( X )是的。

    十、 (每个值有2个)并在中显示正确的响应 y 预测 使用新的值,我们的程序期望相同的-a

        2
  •  21
  •   stackoverflowuser2010    7 年前

    在阵列上运行预测时会出现问题 [0.58,0.76] .在呼叫之前,通过重塑问题来解决问题 predict() :

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib import style
    
    style.use("ggplot")
    from sklearn import svm
    
    x = [1, 5, 1.5, 8, 1, 9]
    y = [2, 8, 1.8, 8, 0.6, 11]
    
    plt.scatter(x,y)
    plt.show()
    
    X = np.array([[1,2],
                 [5,8],
                 [1.5,1.8],
                 [8,8],
                 [1,0.6],
                 [9,11]])
    
    y = [0,1,0,1,0,1]
    
    clf = svm.SVC(kernel='linear', C = 1.0)
    clf.fit(X,y)
    
    test = np.array([0.58, 0.76])
    print test       # Produces: [ 0.58  0.76]
    print test.shape # Produces: (2,) meaning 2 rows, 1 col
    
    test = test.reshape(1, -1)
    print test       # Produces: [[ 0.58  0.76]]
    print test.shape # Produces (1, 2) meaning 1 row, 2 cols
    
    print(clf.predict(test)) # Produces [0], as expected
    
        3
  •  11
  •   Zoe - Save the data dump 张群峰    5 年前

    我使用以下方法。

    reg = linear_model.LinearRegression()
    reg.fit(df[['year']],df.income)
    
    reg.predict([[2136]])
    
        4
  •  6
  •   devsaw    6 年前

    除了我想要预测的实例的数据类型是 panda.Series

    我只需要预测一个输入实例。我是从我的一部分数据中提取的。

    df = pd.DataFrame(list(BiogasPlant.objects.all()))
    test = df.iloc[-1:]       # sliced it here
    

    在这种情况下,需要将其转换为一维数组,然后 reshape

     test2d = test.values.reshape(1,-1)
    

    docs values 将序列转换为numpy数组。

        5
  •  3
  •   Akber Iqbal    6 年前

    print(clf.predict(np.array[[0.58,0.76]]))
    
        6
  •  3
  •   UseR10085 dieghernan    3 年前

    只需在双方括号之间插入参数:

    regressor.predict([[values]])
    

        7
  •  1
  •   Philzen    5 年前

    reg.predict([[3300]]) .

    API过去允许标量值,但现在需要提供2D数组。

        8
  •  0
  •   samuelru    5 年前

    if type(X) is Series:
        X = X.to_frame()
    
        9
  •  0
  •   Mustapha Babatunde    3 年前

    只需用两个方括号括住你的numpy对象,反之亦然。

    例如:

    如果最初您的 x = [8,9,12,7,5]

    将其更改为 x = [ [8,9,12,7,5] ] .

    这应该可以解决维度问题

        10
  •  0
  •   Miguel Tomás    3 年前

    你可以这样做:

    np。数组(x)[:,无]

        11
  •  -1
  •   Chahat Agarwal    6 年前

    自变量和因变量的X和Y矩阵分别从int64类型转换为数据帧,以便将其从一维数组转换为二维数组。。