Python Forum
ValueError: shape mismatch: value array of shape...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: shape mismatch: value array of shape...
#1
When trying to do a linear discriminant analysis I always get this error:

Error:
ValueError: shape mismatch: value array of shape (47,) could not be broadcast to indexing result of shape (47,1)
So I think my problem is that there are two arrays, of which one is 1d and the other 2d. My problem is that I dont know in which line creates this problem and how to fix it.

My code looks like this:

for i in range(prices.shape[0]):
   prices.values[i,:]=prices.values[i,:]>prices.values[i-1,:]

tscv = TimeSeriesSplit(n_splits=10)

X = merged['X']
y = merged['Y']

X=X.values.reshape(-1,1)
y=y.values.reshape(-1,1)

lda=LinearDiscriminantAnalysis()

acc=[]
prediction=np.zeros(y.shape)
for train_index, test_index in tscv.split(X):
   skip_size = len(next(tscv.split(X))[0])
   X_train, X_test = X[train_index], X[test_index]
   y_train, y_test = y[train_index], y[test_index]
   lda.fit(X_train.astype(int),y_train.astype(int))
   y_pred=lda.predict(X_test)
   prediction[test_index]=y_pred
   acc.append(metrics.accuracy_score(y_test, y_pred))
   print('Acc: %.10f' % metrics.accuracy_score(y_test, y_pred))
To find the line I already analyzed the shapes, which look like this:

print(y_pred.shape)
Output:
(47,) (47,) (47,) (47,) (47,) (47,) (47,) (47,) (47,) (47,)
print(prediction.shape)
Output:
(525, 1)
print("TRAIN:", train_index.shape, "TEST:", test_index.shape)
Output:
TRAIN: (55,) TEST: (47,) TRAIN: (102,) TEST: (47,) TRAIN: (149,) TEST: (47,) TRAIN: (196,) TEST: (47,) TRAIN: (243,) TEST: (47,) TRAIN: (290,) TEST: (47,) TRAIN: (337,) TEST: (47,) TRAIN: (384,) TEST: (47,) TRAIN: (431,) TEST: (47,) TRAIN: (478,) TEST: (47,)
When trying to execute prediction[test_index]=y_pred or print the shape, I get the error message.
Reply
#2
prediction[test_index] has shape (47, 1), while y_pred has shape (47,). Either define your prediction as 1D array (with np.zeros(y.shape[0]), or reshape your y_pred to (-1, 1).
Reply
#3
Thank you very much, but with both approaches I get this error message:

Error:
ValueError: Can't handle mix of continuous and binary
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find Mismatch Between Two Strings Omid 8 4,985 Oct-19-2020, 02:41 PM
Last Post: perfringo
  Project 4 Shape Calculator FreeCodeCamp btownboy 5 2,759 Sep-14-2020, 01:38 AM
Last Post: btownboy
  Turtle Graphics Help - Filling each shape in Nate 10 7,124 Nov-27-2018, 08:40 AM
Last Post: Nate
  ValueError: setting an array element with a sequence ying9309 2 7,599 Jul-15-2018, 07:04 AM
Last Post: ying9309
  Array NaN values? ValueError: continuous format is not supported metalray 3 9,504 Jan-05-2018, 08:56 AM
Last Post: metalray
  reshaping numpy array ValueError: total size of new array must be unchanged metalray 0 5,867 Jul-15-2017, 05:41 PM
Last Post: metalray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020