Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
<big> know there are ans to the following q but none of them working for me. so could someone help me?
context:-
I was coding a neural net and I was getting an error "Failed to convert a NumPy array to a Tensor (Unsupported object type int)"
this model was made to predict based on user-provided 7 values and would predict 4 values.</big>
I

```




```
when I try to do this ```data = data.astype(float)```
I get an error ValueError: could not convert string to float: 'Unknown'
when I try to do this ```data = data.astype(int)```
I get an error pandas.errors.IntCastingNaNError: Cannot convert non-finite values (NA or inf) to integer



full traceback :-
```
Traceback (most recent call last):
File "c:\Users\kani2\Desktop\Forest-Fire-Prediction-Website-master\forest_fire.py", line 61, in <module>
log_reg.fit(x_train,y_train,epochs=10,shuffle=True,batch_size=3)
File "C:\Users\kani2\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\kani2\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\constant_op.py", line 102, in convert_to_eager_tensor
return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).
```
Python
from sklearn.model_selection import train_test_split
import warnings
import pickle
import pandas as pd

import matplotlib as mp
from tensorflow.keras.layers import Dense
from keras import Sequential
import tensorflow as tf
from tensorflow.keras import regularizers
from tensorflow import keras
warnings.filterwarnings("ignore")
###########################imports done#############################################

data = pd.read_csv("datset.csv")


#################### data imported ################################################
label_encode = {"City": {'Ahmedabad':0,'Aizawl':1, 'Amaravati':2, 'Amritsar':3, 'Bengaluru':4, 'Bhopal':5,'Brajrajnagar':6, 'Chandigarh':7, 'Chennai':8, 'Coimbatore':9, 'Delhi':10, 'Ernakulam':11,'Gurugram':12, 'Guwahati':13, 'Hyderabad':14, 'Jaipur':15, 'Jorapokhar':16, 'Kochi':17 ,'Kolkata':18,'Lucknow':19, 'Mumbai':20 ,'Patna':21, 'Shillong':22, 'Talcher':23, 'Thiruvananthapuram':24,'Visakhapatnam':25}}

data.replace(label_encode,inplace=True)
abel_encode = {"Region": {'5. Western' :0,'2. North Eastern':1, '1. Northern':2 ,'6. Southern':3, '3. Central':4,'4. Eastern':5}}

data.replace(label_encode,inplace=True)
label_encode = {"State": {'Gujarat':0, 'Mizoram':1 ,'Andhra Pradesh':2 ,'Punjab':3, 'Karnataka':4,'Madhya Pradesh':5, 'Odisha':6, 'Chandigarh':7, 'Tamil Nadu':8, 'Delhi':9 ,'Kerala':10,'Haryana':11, 'Assam':12 ,'Telangana':13, 'Rajasthan':14 ,'Jharkhand':15, 'West Bengal':16,'Uttar Pradesh':17, 'Maharashtra':18, 'Bihar':19, 'Meghalaya':20}}

data.replace(label_encode,inplace=True)
label_encode = {"Month": {'01. Jan':1, '02. Feb':2, '03. Mar':3, '04. Apr':4, '05. May':5, '06. Jun':6, '07. Jul':7,'08. Aug':8, '09. Sep':9 ,'10. Oct':10, '11. Nov':11, '12. Dec':12}}

data.replace(label_encode,inplace=True)
label_encode = {"Season": {'1. Winter':0, '2. Summer':1, '3. Monsoon':2, '4. Post-Monsoon':3}}

data.replace(label_encode,inplace=True)
label_encode = {"Weekday_or_weekend": {'Weekday':0, 'Weekend':1}}

data.replace(label_encode,inplace=True)

########################values replaced############################################
x_values = data[['City','State','Region','Month','Year','Season','Weekday_or_weekend']]
y_values = data[['PM2.5','CO','SO2','AQI']]

x_train, x_test, y_train, y_test = train_test_split(x_values,y_values,test_size=0.15,random_state=1)


###################values split ###################################################
log_reg = Sequential()

log_reg.add(Dense(10,input_dim=7,activation='relu'))

log_reg.add(Dense(20,activation='relu'))
log_reg.add(Dense(10,activation='relu'))

log_reg.add(Dense(4,kernel_initializer='normal',activation='relu'))

log_reg.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
log_reg.summary()
log_reg.fit(x_train,y_train,epochs=10,shuffle=True,batch_size=3)

####################### model made ###################################################
pickle.dump(log_reg,open('model.pkl','wb'))
model=pickle.load(open('model.pkl','rb'))


#*********************** end ****************

What I have tried:

i hav tried seeing ans in google tho none help me
Posted
Updated 6-Apr-22 21:24pm
Comments
Richard MacCutchan 7-Apr-22 3:28am    
Which line causes the error?
[no name] 7-Apr-22 12:22pm    
Most (AI/ML) models deal with floats and doubles. I suspect you're passing ints where you shouldn't.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900