получаю сообщение об ошибке :: слишком много индексов для массива - PullRequest
0 голосов
/ 28 февраля 2020

Я получаю сообщение об ошибке "IndexError: слишком много индексов для массива", я новичок в phyton, я только знаю, что это как-то связано с размером массива, но не знаю, как это исправить. Пожалуйста, помогите

Переменная Y, которую я использую в этом, взята из этого

X = df.iloc[:, 3].values.reshape(-1, 1) #Life expectanacy
Y = df.iloc[:, 6].values.reshape(-1, 1) #Alcohol
    def compute_abs_difference_matrix(Y):
        abs_difference_matrix = np.array([[0,1,2,3], [2,3,4]])
        n_samples = Y.shape[0]
        # compute the absolute difference matrix
        # and remember to return the matrix
        # INSERT YOUR CODE HERE

        for i in range(n_samples):
            for j in range(n_samples):
                abs_difference_matrix[i, j] = abs(Y[i] - Y[j])

        return abs_difference_matrix
   # compute the absolute difference matrix
   abs_difference_matrix = compute_abs_difference_matrix(Y_pred)

   # visualise the matrix
   # INSERT YOUR CODE HERE
   fig, ax = plt.subplots(figsize=(10, 10))
   ax.set_title("Absolute difference matrix for Y_pred")
   cax = ax.imshow(abs_difference_matrix)
   cbar = fig.colorbar(cax)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-79-d9df7ea6b677> in <module>
      6 
      7 # compute the absolute difference matrix
----> 8 abs_difference_matrix = compute_abs_difference_matrix(Y_pred)
      9 
     10 # visualise the matrix

<ipython-input-77-0adc297bd3d4> in compute_abs_difference_matrix(Y)
     15     for i in range(n_samples):
     16         for j in range(n_samples):
---> 17             abs_difference_matrix[i, j] = abs(Y[i] - Y[j])
     18 
     19     return abs_difference_matrix

IndexError: too many indices for array

1 Ответ

0 голосов
/ 28 февраля 2020

У вас это работает?

X = df.iloc[:, 3].values.reshape(-1, 1) #Life expectanacy
Y = df.iloc[:, 6].values.reshape(-1, 1) #Alcohol

def compute_abs_difference_matrix(Y):
    n_samples = Y.shape[0]
    abs_difference_matrix = np.empty([n_samples,n_samples])
    # compute the absolute difference matrix
    # and remember to return the matrix
    # INSERT YOUR CODE HERE

    for i in range(n_samples):
        for j in range(n_samples):
            abs_difference_matrix[i][j] = abs(Y[i] - Y[j])

    return abs_difference_matrix

# compute the absolute difference matrix
abs_difference_matrix = compute_abs_difference_matrix(Y_pred)

# visualise the matrix
# INSERT YOUR CODE HERE
fig, ax = plt.subplots(figsize=(10, 10))
ax.set_title("Absolute difference matrix for Y_pred")
cax = ax.imshow(abs_difference_matrix)
cbar = fig.colorbar(cax)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...