python - 如何修复fit()方法中的“"TypeError: object of type ' CategoricalDtype' has no len()”问题?

标签 python python-3.x scikit-learn sklearn-pandas

我正在 Kaggle 上尝试泰坦尼克号数据集。我使用 dropna() 来保持简单,并且还删除了几列。但是,当我调用 fit() 方法时,我收到“TypeError: object of type 'CategoricalDtype' has no len()”消息。

我尝试使用不同的分类器,但出现相同的错误。我猜我的数据准备有问题。


    df = pd.read_csv('train.csv')
    df.drop('PassengerId', axis=1, inplace=True)
    df.drop('Ticket', axis=1, inplace=True)
    df.drop('Cabin', axis=1, inplace=True)
    df.drop('Embarked', axis=1, inplace=True)
    df=df.dropna()
    mapping = {'male': 0, 'female': 1}
    df = df.replace({'Sex': mapping})
    paramlist=['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare']
    X, y = df[paramlist], df.Survived.astype('category')
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    classifier = KNeighborsClassifier(n_neighbors=3)
    classifier.fit(X_train, y_train)
    y_pred = classifier.predict(X_test)
    confusion_table = confusion_matrix(y_test, y_pred)

print("KNN regression using: "+ str(paramlist))
print(confusion_table)
print(classification_report(y_test, y_pred))
print(f"Accuracy: {accuracy_score(y_test, y_pred)}")
print("=======================================")


```python

Error trace -
  File "<ipython-input-2-e6618b2ba888>", line 1, in <module>
    runfile('C:/Users/vvnat/Documents/Vaibhav/Dropbox/Kaggle/Titanic/Titanic.py', wdir='C:/Users/vvnat/Documents/Vaibhav/Dropbox/Kaggle/Titanic')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/vvnat/Documents/Vaibhav/Dropbox/Kaggle/Titanic/Titanic.py", line 222, in <module>
    classifier.fit(X_train, y_train)

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\neighbors\base.py", line 891, in fit
    X, y = check_X_y(X, y, "csr", multi_output=True)

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 759, in check_X_y
    dtype=None)

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 480, in check_array
    if hasattr(array, "dtypes") and len(array.dtypes):

TypeError: object of type 'CategoricalDtype' has no len()

最佳答案

尝试在train_test_split()之前制作X和y np.arrays,id est X = np.array(X) 和 y = np.array(y)

关于python - 如何修复fit()方法中的“"TypeError: object of type ' CategoricalDtype' has no len()”问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55562121/

相关文章:

python-3.x - 类型错误 : can't pickle dict_items objects

python - 为什么使用中间变量的代码比没有的代码更快?

python - 多元线性回归 scikit-learn 和 statsmodel

python - 使用 matplotlib.pcolormesh() 将颜色设置为透明的正确方法?

python - 使用 Python/PIL 或类似工具来缩小空格

Python导入错误: No module named <myPackage>

Python 程序读取一个文本文件,然后将其内容写入另一个文件,具有不同大小的边距,由用户输入

Python 的 asyncio 是同步工作的

python - Scikit-learn 导入约定

python - 如何在 scikit 学习回归中不标准化目标数据