AttributeError:“DataFrame”对象没有属性“concat”

问题描述 投票:0回答:1

我正在做一个人工神经网络项目,通过医疗保险数据集建立成本预测模型。这是代码:

def FunctionFindBestParams(X_train, y_train, X_test, y_test):
    

    batch_size_list=[5, 10, 15, 20]
    epoch_list  =   [5, 10, 50, 100]

    import pandas as pd
    SearchResultsData=pd.DataFrame(columns=['TrialNumber', 'Parameters', 'Accuracy'])
    
    TrialNumber=0
    for batch_size_trial in batch_size_list:
        for epochs_trial in epoch_list:
            TrialNumber+=1
            
            model = Sequential()
            model.add(Dense(units=5, input_dim=X_train.shape[1], kernel_initializer='normal', activation='relu'))
            model.add(Dense(units=5, kernel_initializer='normal', activation='relu'))
            model.add(Dense(1, kernel_initializer='normal'))
            model.compile(loss='mean_squared_error', optimizer='adam')
            model.fit(X_train, y_train ,batch_size = batch_size_trial, epochs = epochs_trial, verbose=0)
            MAPE = np.mean(100 * (np.abs(y_test-model.predict(X_test))/y_test))
            
            print(TrialNumber, 'Parameters:','batch_size:', batch_size_trial,'-', 'epochs:',epochs_trial, 'Accuracy:', 100-MAPE)
            
            SearchResultsData=SearchResultsData.concat(pd.DataFrame(data=[[TrialNumber,     str(batch_size_trial)+'-'+str(epochs_trial), 100-MAPE]], 
columns=['TrialNumber', 'Parameters', 'Accuracy'] ))
    return(SearchResultsData)

ResultsData=FunctionFindBestParams(X_train, y_train, X_test, y_test)

我已经使用过

append()
concat()
功能,但它们都不起作用。有人可以帮我解决这个问题吗?提前非常感谢您。

python neural-network attributeerror
1个回答
0
投票

问题在于

concat
不是 DataFrame 实例方法,而是 pandas 类方法。正确的使用方法是这样的:

SearchResultsData = pd.concat([SearchResultsData, pd.DataFrame(data=[[TrialNumber, str(batch_size_trial)+'-'+str(epochs_trial), 100-MAPE]], columns=['TrialNumber', 'Parameters', 'Accuracy'])], ignore_index=True)

pd.concat
函数将DataFrame列表或字典作为输入。我还添加了
ignore_index=True
参数,以避免保留连接的 DataFrame 中的旧索引值。

© www.soinside.com 2019 - 2024. All rights reserved.