Jul-09-2017, 08:37 AM
When trying to do a linear discriminant analysis I always get this error:
My code looks like this:
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.