johnlemon97
johnlemon97

Reputation: 49

Python error arrays used as indices must be of integer (or boolean) type

Hello I have the following script:

import numpy as np
import matplotlib.pyplot as plt

m = int(input("lenght of t and b.  m = "))

t = 27 * np.random.random( (m, 1) )
b = 27 * np.random.random( (m, 1) )

A = np.ones( (m, 1) )  
A = np.hstack( (A, t) )

x = np.linalg.solve( np.dot(A.T, A), np.dot(A.T, b) ) 
print("x = \n", x) 

t1 = np.linspace ( min(t), max(t), 50) 
b1 = np.polyval(x[::-1, t1]) 
plt.plot(t1, b1)

plt.plot(t, b, "ro")
plt.xlabel("t")
plt.ylabel("b")
plt.show() 

I'm trying to do a plot but i'm getting the following error "IndexError: arrays used as indices must be of integer (or boolean) type" At the line:

b1 = np.polyval(x[::-1, t1])

How can I solve this please?

Upvotes: 0

Views: 787

Answers (2)

nferreira78
nferreira78

Reputation: 1174

This error may also happen when the DataFrame or numpy array has NaN values. It can be fixed like this:

df.fillna(0)

Or any other suitable fillna() strategy

Upvotes: 1

Zeinab Mardi
Zeinab Mardi

Reputation: 131

write like this:

b1 = np.polyval(x[::-1] ,t1)

Upvotes: 0

Related Questions