Keras error on model.fit(): TypeError: loop of ufunc does not support argument 0 of type str which has no callable rint method

I am implementing a multi-class classification machine learning model with Keras for NLP. I’m not sure whether this context is useful, but due to class imbalances, I created a class_weight array to be implemented in the model.fit() function as follows:

class_weight = {1: 3.,
                2: 2.,
                3: 1.5,
                4: 1.,
                5: 3.,
                6: 3.
                }

I then implemented my model.fit() function as follows:

history = model.fit(X_train, y_train,
                    epochs = 100,
                    verbose = False,
                    validation_data = (X_val, y_val),
                    batch_size = 10,
                    class_weight = class_weight,
                    callbacks = [early_stopping]
                   )

Running this, however, resulted with the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[583], line 9
      1 class_weight = {1: 3.,
      2                 2: 2.,
      3                 3: 1.5,
   (...)
      6                 6: 3.
      7                 }
----> 9 history = model.fit(X_train, y_train,
     10                     epochs = 100,
     11                     verbose = False,
     12                     validation_data = (X_val, y_val),
     13                     batch_size = 10,
     14                     class_weight = class_weight,
     15                     callbacks = [early_stopping]
     16                    )

File ~AppDataLocalanaconda3Libsite-packageskerassrcutilstraceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119     filtered_tb = _process_traceback_frames(e.__traceback__)
    120     # To get the full stack trace, call:
    121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
    123 finally:
    124     del filtered_tb

File ~AppDataLocalanaconda3Libsite-packagesnumpycorefromnumeric.py:3360, in round(a, decimals, out)
   3269 @array_function_dispatch(_round_dispatcher)
   3270 def round(a, decimals=0, out=None):
   3271     """
   3272     Evenly round to the given number of decimals.
   3273 
   (...)
   3358 
   3359     """
-> 3360     return _wrapfunc(a, 'round', decimals=decimals, out=out)

File ~AppDataLocalanaconda3Libsite-packagesnumpycorefromnumeric.py:68, in _wrapfunc(obj, method, *args, **kwds)
     59     return bound(*args, **kwds)
     60 except TypeError:
     61     # A TypeError occurs if the object does have such a method in its
     62     # class, but its signature is not identical to that of NumPy's. This
   (...)
     66     # Call _wrapit from within the except clause to ensure a potential
     67     # exception has a traceback chain.
---> 68     return _wrapit(obj, method, *args, **kwds)

File ~AppDataLocalanaconda3Libsite-packagesnumpycorefromnumeric.py:45, in _wrapit(obj, method, *args, **kwds)
     43 except AttributeError:
     44     wrap = None
---> 45 result = getattr(asarray(obj), method)(*args, **kwds)
     46 if wrap:
     47     if not isinstance(result, mu.ndarray):

TypeError: loop of ufunc does not support argument 0 of type str which has no callable rint method

The error is obviously in the model.fit() function, but I’m not sure where it originates and how to fix it. To try to alleviate the issue, I converted y_train and y_val into int types with astype(). However, this resulted in a different error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[587], line 9
      1 class_weight = {1: 3.,
      2                 2: 2.,
      3                 3: 1.5,
   (...)
      6                 6: 3.
      7                 }
----> 9 history = model.fit(X_train, y_train,
     10                     epochs = 100,
     11                     verbose = False,
     12                     validation_data = (X_val, y_val),
     13                     batch_size = 10,
     14                     class_weight = class_weight,
     15                     callbacks = [early_stopping]
     16                    )

File ~AppDataLocalanaconda3Libsite-packageskerassrcutilstraceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119     filtered_tb = _process_traceback_frames(e.__traceback__)
    120     # To get the full stack trace, call:
    121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
    123 finally:
    124     del filtered_tb

File ~AppDataLocalanaconda3Libsite-packagespandascoreseries.py:1040, in Series.__getitem__(self, key)
   1037     return self._values[key]
   1039 elif key_is_scalar:
-> 1040     return self._get_value(key)
   1042 # Convert generator to list before going through hashable part
   1043 # (We will iterate through the generator there to check for slices)
   1044 if is_iterator(key):

File ~AppDataLocalanaconda3Libsite-packagespandascoreseries.py:1156, in Series._get_value(self, label, takeable)
   1153     return self._values[label]
   1155 # Similar to Index.get_value, but we do not fall back to positional
-> 1156 loc = self.index.get_loc(label)
   1158 if is_integer(loc):
   1159     return self._values[loc]

File ~AppDataLocalanaconda3Libsite-packagespandascoreindexesbase.py:3798, in Index.get_loc(self, key)
   3793     if isinstance(casted_key, slice) or (
   3794         isinstance(casted_key, abc.Iterable)
   3795         and any(isinstance(x, slice) for x in casted_key)
   3796     ):
   3797         raise InvalidIndexError(key)
-> 3798     raise KeyError(key) from err
   3799 except TypeError:
   3800     # If we have a listlike key, _check_indexing_error will raise
   3801     #  InvalidIndexError. Otherwise we fall through and re-raise
   3802     #  the TypeError.
   3803     self._check_indexing_error(key)

KeyError: 0

Please advise!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật