menu

Questions & Answers

My array in Python suddenly turns into zero (int)

I have this code here

    res_x7, predict_x7 = [], []
for i in range(1, len(df.columns) - 1):

    if i != 5:
        formula = 'y ~ x1 + x7 + ' + str(col_names[i])
        res_x7.append(sm.OLS.from_formula(formula = formula, data=train_data).fit())
        predict_x7.append(res_x7[i-1].predict(test_data))

        print(res_x7[i - 1].t_test(np.identity(4)))
        print('\n'*3)
    
    else:
        res_x7.append(0)
        predict_x7=0

where df.columns is (x1, x2, x3 ... x8), when i run my code on the seventh iteration (final one) the array

[[83 1.7] [83 1.2] ... [83 1.1]]

turns to 0 and this error occurs

predict_x7.append(res_x7[i-1].predict(test_data))
^^^^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'append'

I have no idea why.

Comments:
2023-01-20 00:30:06
In the last line you have predict_x7=0, probably meant to append a zero value instead?
Answers(0) :