问题类型错误:参数必须是字符串或数字

问题描述

只有一个分类列,我想对它进行编码,它在 notebook 上运行良好,但是当它上传到 aicrowd 平台时,就会造成这个问题。 总共有 3 个分类特征,一个是目标特征,一个是 id 行,在训练中排除它们后,我只剩下一个特征。

df[['intersection_pos_rel_centre']]
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
df[['intersection_pos_rel_centre']]=le.fit_transform(df[['intersection_pos_rel_centre']])
df[['intersection_pos_rel_centre']]

我的错误

Selecting runtime language: python
[NbConvertApp] Converting notebook predict.ipynb to notebook
[NbConvertApp] Executing notebook with kernel: python
Traceback (most recent call last):
  File "/opt/conda/bin/jupyter-nbconvert",line 11,in <module>
    sys.exit(main())
  File "/opt/conda/lib/python3.8/site-packages/jupyter_core/application.py",line 254,in launch_instance
    return super(Jupyterapp,cls).launch_instance(argv=argv,**kwargs)
  File "/opt/conda/lib/python3.8/site-packages/traitlets/config/application.py",line 845,in launch_instance
    app.start()
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/nbconvertapp.py",line 350,in start
    self.convert_notebooks()
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/nbconvertapp.py",line 524,in convert_notebooks
    self.convert_single_notebook(notebook_filename)
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/nbconvertapp.py",line 489,in convert_single_notebook
    output,resources = self.export_single_notebook(notebook_filename,resources,input_buffer=input_buffer)
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/nbconvertapp.py",line 418,in export_single_notebook
    output,resources = self.exporter.from_filename(notebook_filename,resources=resources)
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/exporters/exporter.py",line 181,in from_filename
    return self.from_file(f,resources=resources,**kw)
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/exporters/exporter.py",line 199,in from_file
    return self.from_notebook_node(nbformat.read(file_stream,as_version=4),**kw)
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/exporters/notebook.py",line 32,in from_notebook_node
    nb_copy,resources = super().from_notebook_node(nb,line 143,resources = self._preprocess(nb_copy,resources)
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/exporters/exporter.py",line 318,in _preprocess
    nbc,resc = preprocessor(nbc,resc)
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/preprocessors/base.py",line 47,in __call__
    return self.preprocess(nb,resources)
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/preprocessors/execute.py",line 79,in preprocess
    self.execute()
  File "/opt/conda/lib/python3.8/site-packages/nbclient/util.py",line 74,in wrapped
    return just_run(coro(*args,**kwargs))
  File "/opt/conda/lib/python3.8/site-packages/nbclient/util.py",line 53,in just_run
    return loop.run_until_complete(coro)
  File "/opt/conda/lib/python3.8/asyncio/base_events.py",line 616,in run_until_complete
    return future.result()
  File "/opt/conda/lib/python3.8/site-packages/nbclient/client.py",line 553,in async_execute
    await self.async_execute_cell(
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/preprocessors/execute.py",line 123,in async_execute_cell
    cell,resources = self.preprocess_cell(cell,self.resources,cell_index)
  File "/opt/conda/lib/python3.8/site-packages/nbconvert/preprocessors/execute.py",line 146,in preprocess_cell
    cell = run_sync(NotebookClient.async_execute_cell)(self,cell,index,store_history=self.store_history)
  File "/opt/conda/lib/python3.8/site-packages/nbclient/util.py",in just_run
    return loop.run_until_complete(coro)
  File "/opt/conda/lib/python3.8/site-packages/nest_asyncio.py",line 98,in run_until_complete
    return f.result()
  File "/opt/conda/lib/python3.8/asyncio/futures.py",line 178,in result
    raise self._exception
  File "/opt/conda/lib/python3.8/asyncio/tasks.py",line 280,in __step
    result = coro.send(None)
  File "/opt/conda/lib/python3.8/site-packages/nbclient/client.py",line 852,in async_execute_cell
    self._check_raise_for_error(cell,exec_reply)
  File "/opt/conda/lib/python3.8/site-packages/nbclient/client.py",line 760,in _check_raise_for_error
    raise CellExecutionError.from_cell_and_msg(cell,exec_reply_content)
nbclient.exceptions.CellExecutionError: An error occurred while executing the following cell:
------------------
df[['intersection_pos_rel_centre']]
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
df[['intersection_pos_rel_centre']]=le.fit_transform(df[['intersection_pos_rel_centre']])
df[['intersection_pos_rel_centre']]
------------------


TypeError: argument must be a string or number

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)