西仓云 2019-07-15 20:04 采纳率: 0%
浏览 2222

吐血!ValueError: shapes (278,6751) and (0,) not aligned: 6751 (dim 1) != 0 (dim 0)

def predict(self, features, method, parameters=[], labelValidation=[], classifier=[]):
""" To compute the model parameters given the feature matrix and the
labels of a training dataset

    INPUTS:
        parameters: 1D np.array, the parameters of the model
        features -- 2D np.array, feature matrix containung the 
        features extracting from the dataset
        labelValidation -- 1D np.array, the labels used for validation

    OUTPUTS:
        predictedData: 1D np.array, the predicted labels
    """
    # Number of 3D MRI images in the dataset:
    nbSamples = features.shape[0]

    if method in ["Random Forest", "AdaBoost", "Bagging", "Gradient Boosting"]:
        predictedData = classifier.predict_proba( features )[:,1]
        #print(predictedData)

    elif method == "SVM":
        predictions = classifier.predict( features )
        predictedData = np.zeros(len(predictions))

        # We change the values predicted by the support vector machine 
        # classifier: 0-->0.1 and 1-->0.9. This allows reducing the 
        # logistic score:
        for i in range (len(predictions)):
            if predictions[i] == 0:
                predictedData[i] = 0.1  
            else:
                predictedData[i] = 0.9

    elif method in ["Multi SVM", "Multi Random Forest"]:
        predictedData = classifier.predict( features )

    else:
        # The first column of the feature matrix should be the bias:
        bias = np.ones([nbSamples,1])
        features = np.concatenate((bias, features), axis=1)

        # Prediction of the model for the given dataset:
        predictedData = features.dot( parameters )

    # Computation of the error of the predicted data:
    if len(labelValidation) > 0:
        if method in ["Random Forest", "SVM", "AdaBoost", "Bagging", "Gradient Boosting"]:
            error = log_loss(labelValidation, predictedData)

        elif method in ["Multi Random Forest", "Multi SVM"]:
            predictionsMultiple = np.zeros([len(labelValidation), 3])
            truePrediction = np.zeros([len(labelValidation), 3])
            for i  in range(len(labelValidation)):
                predictionsMultiple[i,:] = self.classToLabels[\
                                    predictedData[i],:]

                # True labels:
                truePrediction[i,:] = self.classToLabels[\
                                    labelValidation[i],:]


            # Score function:
            error = np.mean(np.abs(predictionsMultiple - truePrediction))


        else:
            error = (np.mean((predictedData - labelValidation)**2)) 

        return predictedData, error
    else:
        return predictedData

modelParameters = data2Predict.buildClassifier(featureMatrix, \
             label, method = "RidgeCV")

# Prediction of the data using the model parameters:

MSESelf = data2Predict.predict(featureMatrix,modelParameters ,\
                              labelValidation = label)

图片说明

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-07-15 23:03
    关注

    print (featureMatrix.shape)
    print (modelParameters.shape)
    输出下,两个维度不匹配,看看是不是特征矩阵的两个维度写反了。
    6751应该是6751条记录,278应该是模型的维度。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败