簡體   English   中英

數據框fit_transform拋出錯誤,看似錯誤

[英]Dataframe fit_transform throwing error with seemingly incorrect error

我在Python中運行給定的行:

df = df.apply(lambda x: d[x.name].fit_transform(x))

並得到以下錯誤:

~/anaconda3/envs/python3/lib/python3.6/site-packages/numpy/lib/arraysetops.py in _unique1d(ar, return_index, return_inverse, return_counts)
278 
279     if optional_indices:
--> 280         perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
281         aux = ar[perm]
282     else:

TypeError: ("'<' not supported between instances of 'str' and 'float'", 'occurred at index name')

我的文件中的任何地方都沒有字符“ <”,所以不確定錯誤是什么嗎?

Python的新手,因此非常感謝您對如何理解這些錯誤的任何見解。

我認為這可能是因為您沒有將干凈或正確的數據傳遞給fit_transform 在評論中不回答我的問題就很難說( ddf = df.apply(lambda x: d[x.name].fit_transform(x))什么??

我接受了一些虛擬數據,並舉例說明了如何使用apply將fit_transform應用於數據fit_transform

import random
import pandas as pd
import numpy as np

# Random dummy data
s = "Crime Type Summer|Crime Type Winter".split("|")
j = {x: [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)] for x in s}
df = pd.DataFrame(j)

# Instantiate the vectorizer for use in the lambda function.
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer()

# Now we can call the transform directly in the lambda function.
df = df.apply(lambda x: cv.fit_transform(df[x.name].values))

這成功完成,並提供:

Crime Type Summer      (0, 1)\t1\n  (1, 4)\t1\n  (2, 2)\t1\n  (2, 3...
Crime Type Winter      (0, 5)\t1\n  (1, 0)\t1\n  (2, 0)\t1\n  (3, 5...
dtype: object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM