首页 > 编程问答 >交叉验证函数返回“未知标签类型:(array([0.0, 1.0], dtype=object),)”

交叉验证函数返回“未知标签类型:(array([0.0, 1.0], dtype=object),)”

时间:2024-07-21 05:31:14浏览次数:10  
标签:python machine-learning cross-validation sklearn-pandas

以下是完整的错误:

`---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[33], line 2
      1 gnb = GaussianNB()
----> 2 cv = cross_val_score(gnb,X_train,y_train,cv=5, error_score = 'raise')
      3 print(cv)
      4 print(cv.mean())

File /opt/conda/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:515, in cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch, error_score)
    512 # To ensure multimetric format is not supported
    513 scorer = check_scoring(estimator, scoring=scoring)
--> 515 cv_results = cross_validate(
    516     estimator=estimator,
    517     X=X,
    518     y=y,
    519     groups=groups,
    520     scoring={"score": scorer},
    521     cv=cv,
    522     n_jobs=n_jobs,
    523     verbose=verbose,
    524     fit_params=fit_params,
    525     pre_dispatch=pre_dispatch,
    526     error_score=error_score,
    527 )
    528 return cv_results["test_score"]

File /opt/conda/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:266, in cross_validate(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch, return_train_score, return_estimator, error_score)
    263 # We clone the estimator to make sure that all the folds are
    264 # independent, and that it is pickle-able.
    265 parallel = Parallel(n_jobs=n_jobs, verbose=verbose, pre_dispatch=pre_dispatch)
--> 266 results = parallel(
    267     delayed(_fit_and_score)(
    268         clone(estimator),
    269         X,
    270         y,
    271         scorers,
    272         train,
    273         test,
    274         verbose,
    275         None,
    276         fit_params,
    277         return_train_score=return_train_score,
    278         return_times=True,
    279         return_estimator=return_estimator,
    280         error_score=error_score,
    281     )
    282     for train, test in cv.split(X, y, groups)
    283 )
    285 _warn_or_raise_about_fit_failures(results, error_score)
    287 # For callabe scoring, the return type is only know after calling. If the
    288 # return type is a dictionary, the error scores can now be inserted with
    289 # the correct key.

File /opt/conda/lib/python3.10/site-packages/sklearn/utils/parallel.py:63, in Parallel.__call__(self, iterable)
     58 config = get_config()
     59 iterable_with_config = (
     60     (_with_config(delayed_func, config), args, kwargs)
     61     for delayed_func, args, kwargs in iterable
     62 )
---> 63 return super().__call__(iterable_with_config)

File /opt/conda/lib/python3.10/site-packages/joblib/parallel.py:1918, in Parallel.__call__(self, iterable)
   1916     output = self._get_sequential_output(iterable)
   1917     next(output)
-> 1918     return output if self.return_generator else list(output)
   1920 # Let's create an ID that uniquely identifies the current call. If the
   1921 # call is interrupted early and that the same instance is immediately
   1922 # re-used, this id will be used to prevent workers that were
   1923 # concurrently finalizing a task from the previous call to run the
   1924 # callback.
   1925 with self._lock:

File /opt/conda/lib/python3.10/site-packages/joblib/parallel.py:1847, in Parallel._get_sequential_output(self, iterable)
   1845 self.n_dispatched_batches += 1
   1846 self.n_dispatched_tasks += 1
-> 1847 res = func(*args, **kwargs)
   1848 self.n_completed_tasks += 1
   1849 self.print_progress()

File /opt/conda/lib/python3.10/site-packages/sklearn/utils/parallel.py:123, in _FuncWrapper.__call__(self, *args, **kwargs)
    121     config = {}
    122 with config_context(**config):
--> 123     return self.function(*args, **kwargs)

File /opt/conda/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:686, in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, return_n_test_samples, return_times, return_estimator, split_progress, candidate_progress, error_score)
    684         estimator.fit(X_train, **fit_params)
    685     else:
--> 686         estimator.fit(X_train, y_train, **fit_params)
    688 except Exception:
    689     # Note fit time as time until error
    690     fit_time = time.time() - start_time

File /opt/conda/lib/python3.10/site-packages/sklearn/naive_bayes.py:267, in GaussianNB.fit(self, X, y, sample_weight)
    265 self._validate_params()
    266 y = self._validate_data(y=y)
--> 267 return self._partial_fit(
    268     X, y, np.unique(y), _refit=True, sample_weight=sample_weight
    269 )

File /opt/conda/lib/python3.10/site-packages/sklearn/naive_bayes.py:427, in GaussianNB._partial_fit(self, X, y, classes, _refit, sample_weight)
    424 if _refit:
    425     self.classes_ = None
--> 427 first_call = _check_partial_fit_first_call(self, classes)
    428 X, y = self._validate_data(X, y, reset=first_call)
    429 if sample_weight is not None:

File /opt/conda/lib/python3.10/site-packages/sklearn/utils/multiclass.py:420, in _check_partial_fit_first_call(clf, classes)
    413             raise ValueError(
    414                 "`classes=%r` is not the same as on last call "
    415                 "to partial_fit, was: %r" % (classes, clf.classes_)
    416             )
    418     else:
    419         # This is the first call to partial_fit
--> 420         clf.classes_ = unique_labels(classes)
    421         return True
    423 # classes is None and clf.classes_ has already previously been set:
    424 # nothing to do

File /opt/conda/lib/python3.10/site-packages/sklearn/utils/multiclass.py:107, in unique_labels(*ys)
    105 _unique_labels = _FN_UNIQUE_LABELS.get(label_type, None)
    106 if not _unique_labels:
--> 107     raise ValueError("Unknown label type: %s" % repr(ys))
    109 if is_array_api:
    110     # array_api does not allow for mixed dtypes
    111     unique_ys = xp.concat([_unique_labels(y) for y in ys])

ValueError: Unknown label type: (array([0.0, 1.0], dtype=object),)`

我正在尝试在 Kaggle 上的泰坦尼克号太空船项目中实现交叉验证,您可以在此处找到我的完整笔记本 我无法运行交叉验证功能。我已经摆弄了几个小时,但一直找不到解决方案。 我按照

Ken Jee 的泰坦尼克号项目示例 完成了我自己的 泰坦尼克号预测提交 在此代码中,交叉验证函数可以成功运行,我尝试将其重新用于泰坦尼克号太空船项目。 如果您需要任何其他详细信息,请告诉我。我对机器学习很陌生,所以我提前感谢您的耐心。

我已经尝试摆脱空值,验证我的训练和测试列表的数据类型是否相同,并且我已经尝试将分类变量映射到数值变量。

I've tried getting rid of null values, verified that the data types for my training and test lists are the same, and I've tried mapping categorical variables to numerical variables.


遇到的错误“ValueError:未知标签类型:(array([0.0, 1.0], dtype=object),)”表明的目标变量( y_train )数据类型存在问题。尽管该数组包含数字,但其数据类型为 object ,这表示它可能包含混合类型或被视为字符串。Scikit-learn 期望目标变量为数值类型(通常为整数)用于分类。

以下是解决此问题的几个步骤:

  1. 检查数据类型: 在调用 cross_val_score 函数之前,请打印 y_train 的数据类型以确认其是否为 object python print(y_train.dtype)

  2. 转换数据类型: 如果数据类型为 object ,则需要将其转换为数值类型,例如 int float 。可以使用 astype 方法执行此操作: python y_train = y_train.astype(int) 如果的目标变量包含字符串标签(例如“是”、“否”),则需要先使用 LabelEncoder 将它们转换为数值: python from sklearn.preprocessing import LabelEncoder le = LabelEncoder() y_train = le.fit_transform(y_train)

  3. 在数据源处查找问题: 错误的数据类型可能表示数据加载或预处理过程中存在问题。仔细检查从CSV文件或其他数据源加载数据的方式,并确保目标列包含预期类型的值。

  4. 检查空值: 确保的目标变量 y_train 中没有空值或NaN值。可以使用以下方法处理空值:

  5. 删除包含空值的行。
  6. 使用平均值、中位数或最频繁值等策略填充空值。

通过执行这些步骤,应该能够解决“未知标签类型”错误并成功使用 cross_val_score 函数。

标签:python,machine-learning,cross-validation,sklearn-pandas
From: 78773942

相关文章

  • Python:对很高维的矩阵进行对角化?
    目前我正在研究一个涉及对角化矩阵以获得特征值和特征向量的问题。但现在我想将问题扩展到200,000x200,000的尺寸。我查找了如何将矩阵存储在numpy中,有人建议使用PyTables。看起来很有希望。但我想知道哪里有工具可以帮助对PyTables中的矩阵存储进行对角化。......
  • 除了curses之外,是否有一个python包可以轻松控制终端的输出?
    我现在正在处理一些小项目,我对GUI的偏好是终端中漂亮的文本界面。我宁愿不强迫用户处理Windowscurses二进制文件,所以我正在寻找不同的选项。我已经发现了asciimatics,但我想考虑所有可能的选择。如果有人有任何经验或知道解决此用例的包,我将不胜感激。谢谢你说的没错......
  • 当值来自函数 python unittest 时,如何模拟全局变量
    我必须在python中模拟全局变量,但变量值来自另一个函数。当我导入文件时,这个函数正在运行,但我想要那里的模拟值。secrets.pyimporttracebackimportloggingimportboto3importosimportjsonlogger=logging.getLogger()logger.setLevel(logging.INFO)secret_......
  • 使用 python print 和 gdb 时出现 BrokenPipeError
    我正在尝试在Linux中运行应用程序并使用Python生成输入:python3-c'print(".....")'|./someapp但出现下一个错误:Exceptionignoredin:<_io.TextIOWrappername='<stdout>'mode='w'encoding='utf-8'>BrokenPipeError:......
  • python 舰队容器
    我正在尝试使用容器在flet中制作一个菜单,它应该是半透明的,但其中的项目不是。我尝试将opacity=1分配给元素,但没有成功-它们与容器一样透明感谢任何帮助我的代码:nickname=ft.TextField(label="xxx",hint_text="xxx")column=ft.Column(controls=[nickname......
  • Python应用程序跨子包共享的配置文件
    我正在构建一个应用程序来控制一些硬件。我在包中实现了不同类型的硬件:电机和测量设备。我的文件结构如下:name_of_my_app/__init__.pymain.pyconfig.iniCONFIG.pymotors/__init__.pyone_kind_of_motor.pymeasurement_devices/......
  • python中时间序列数据的梯度计算
    我正在尝试编写一个函数,它可以从最适合下面的线返回梯度dataframe在浏览了谷歌的几个资源之后,我仍然不确定这是如何完成的。我明白最佳拟合线的计算公式为:y=mx+b将因变量(y)设置为foos,将自变量(x)设置为DateTimeDatafram......
  • 调试用 C 编写的 Python 扩展
    我非常熟悉编写C代码,并且很擅长编写Python代码。我正在尝试学习如何用C编写可以从OSX10.15.7上的Python-3.9.X调用的模块。我已经得到了几个“helloworld”类型的示例,但是对于复杂的示例,我正在努力弄清楚如何调试我编写的C扩展。MWE:src/add.c//......
  • 具有块大小选项的 Python pandas read_sas 因索引不匹配而失败并出现值错误
    我有一个非常大的SAS文件,无法容纳在我的服务器内存中。我只需要转换为镶木地板格式的文件。为此,我使用pandas中chunksize方法的read_sas选项分块读取它。它主要是在工作/做它的工作。除此之外,一段时间后它会失败并出现以下错误。此特定SAS文件有794......
  • 使用 requests 包 python 时打开文件太多
    我正在使用Pythonrequests包向API发出大量请求。然而,在某些时候,我的程序由于“打开的文件太多”而崩溃。当我明确关闭我的会话时,我真的不知道这是怎么回事。我使用以下代码:importrequestsimportmultiprocessingimportnumpyasnps=requests.session()s.keep......