Error Object arrays are not currently supported Python

550    Asked by Aashishchaursiya in Python , Asked on Apr 23, 2021
import numpy as np
import math
time_decay_parameter = 0.055
h_column1_formula = np.repeat(1, 10)
h_column2_formula = []
h_column3_formula = []
for maturity in range(1,10):
h_column2_formula.append( (1-math.exp(- 
time_decay_parameter*maturity))/(time_decay_parameter*maturity))
h_column3_formula.append(((1-math.exp(- 
time_decay_parameter*maturity))/(time_decay_parameter*maturity))- 
math.exp(-time_decay_parameter*maturity))
h_column2_formula_array = np.asarray(h_column2_formula)
h_column3_formula_array = np.asarray(h_column3_formula)
h_matrix = np.array([h_column1_formula,h_column2_formula_array,h_column3_formula_array]).T
print(h_matrix)
ht_h = h_matrix.T@h_matrix
Getting error:
TypeError                                 Traceback (most recent call last)
in ()
     17 print('HMATRIX n')
     18 print(h_matrix)
---> 19 ht_h = h_matrix.T@h_matrix
TypeError: Object arrays are not currently supported

Answered by Emma Cornish

The problem you have is that when you are trying to build your h_matrix array, it finds that it's rows don't all have the same length. The h_column1_formula array contains 10 as length, the others are length 9.

So rather than building an array with a numeric data type, it uses dtype=object, since that supports arbitrary contents (including arrays of different sizes). The matrix multiplication complains about this, as it doesn't support object arrays, only numeric ones.

To fix this “object arrays are not currently supported” error, you probably need to change some of the code you're using to build the matrix. Either you need to range call in the loop or the np.repeat call should be changed so that the numbers line up properly.

I'd suggest either:

  h_column1_formula = np.repeat(1, 9) # create an array with 9 values

OR

  for maturity in range(1,11): # loop 10 times, 1-10


Your Answer

Interviews

Parent Categories