首页 > 解决方案 > python函数的语法

问题描述

我不断收到一个错误,指出

File "<ipython-input-26-83f725266158>", line 44
return predictions, indexStartPosition
     ^
SyntaxError: invalid syntax

我不确定为什么这条返回线会导致错误。谢谢您的帮助!!

# function to make predictions, inputs: number of predictions to be made (# must be a factor of 96),
# the index start position where the predicitions (# must be greater than 6, because 6 values are
# used to make each prediction)
def makePredictions(numOfPredictions, indexStartPosition, trainX):    
#         stepSize = number of predictions made before real data is read in to make the next prediction
#         step size has to be a factor of 96
    stepSize = 4;
#         empty numpy array to hold predictions
    predictions = np.empty((0,1), float)
#         numOfPredictionLoops = iterator that holds the number of times the proceeding loop is performed until
#         all the predictions are made
    numOfPredictionLoops = int(numOfPredictions / stepSize)
#         outer for loop that reads in real data to make first prediction
    for j in range(numOfPredictionLoops):
#             empty matrix to hold real data and predictions
        origAndPredictions = [];
#             read in the correct row of real data from the trainX array and save it as temp3, based on the 
#             indexStartPosition and how many times the program has gone through the outer loop
        temp3 = trainX[int(j * stepSize) + indexStartPosition - 7]
#             append this row matrix to the origAndPredictions matirx
        origAndPredictions.append(temp3)
#             convert matrix to numpy array
        origAndPredictions = np.array(origAndPredictions)
#             inner loop: makes predictions, saves the values, creates new rows to make future predictions
        for i in range(stepSize):
#                 temp variable that is a row matrix, hold the six values to be used to make the prediction
            temp = [[origAndPredictions[i,0],origAndPredictions[i,1],origAndPredictions[i,2],origAndPredictions[i,3],origAndPredictions[i,4],origAndPredictions[i,5]]]
#                 convert temp from a matrix to a numpy array
            temp = np.array(temp)
#                 use built in keras model to make predictions, save in temp variable
            temp1 = model.predict(temp)
#                 append latest prediction to the column array holding all the predictions
            predictions = np.append(predictions, temp1, axis=0)
#                 empty row matrix used to create the next row used to make predictions
            temp2 = []
#                 remove first value from row, slide next five forward a spot, and append the latest prediction
#                 this row matrix will be used to make the next prediction
            temp2 = [[origAndPredictions[i,1],origAndPredictions[i,2],origAndPredictions[i,3],origAndPredictions[i,4],origAndPredictions[i,5],predictions[((j*stepSize) + i),0]]]
#                 covernt matrix to numpy array
            temp2 = np.array(temp2)
#                 add this array to the end of array 
            origAndPredictions = np.vstack((origAndPredictions, temp2)

    return predictions, indexStartPosition

标签: pythonfunction

解决方案


你的第二行是

origAndPredictions = np.vstack((origAndPredictions, temp2)

)最后错过了一个。

Python 在函数调用完成之前找到返回值。

一般来说——当你遇到语法错误时,问题通常出在发现错误之前的代码中——而且问题通常是不平衡的括号、括号或引号。(至少对我来说...)


推荐阅读