为什么会出现'DataFrame' object has no attribute 'dtype'
users=["User1","User2","User3","User4","User5"]items=["ItemA","ItemB","ItemC","ItemD","ItemE",]
datasets=[
[1,0,1,0,1],
[1,1,0,0,1],
[0,1,0,1,0],
[1,0,1,0,1],
[1,1,1,0,1]
...
...
]
import pandas as pd
df=pd.DataFrame(datasets,columns=items,index=users)
from sklearn.metrics import jaccard_score
jaccard_score(df['ItemA'],df['ItemB'])
from sklearn.metrics.pairwise import pairwise_distances
user_similar=1-pairwise_distances(df,metric='jaccard')
请教一下各位大哥,为什么会有如下报错,是这个pairwise_distances问题但是我实在无法解决
AttributeError Traceback (most recent call last)> ~\AppData\Local\Temp/ipykernel_39128/2253043940.py in <module>
> ----> 1 user_similar=1-pairwise_distances(df,metric='jaccard')
> 2 user_similar=pd.DataFrame(user_similar,columns=users,index=users)
> 引用
> ~\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\metrics\pairwise.py in pairwise_distances(X, Y, metric, n_jobs, force_all_finite, **kwds)
> 1871 dtype = bool if metric in PAIRWISE_BOOLEAN_FUNCTIONS else None
> 1872
> -> 1873 if dtype == bool and (X.dtype != bool or (Y is not None and Y.dtype != bool)):
> 1874 msg = "Data was converted to boolean for metric %s" % metric
> 1875 warnings.warn(msg, DataConversionWarning)
> 引用
> ~\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
> 5485 ):
> 5486 return self[name]
> -> 5487 return object.__getattribute__(self, name)
> 5488
> 5489 def __setattr__(self, name: str, value) -> None:
> 引用
> AttributeError: 'DataFrame' object has no attribute 'dtype'
回答:
我找到解决方法了,user_similar=1-pairwise_distances(df,metric='jaccard')
这个语句中,pairwise_distances无法实现metric='jaccard‘这个方法。我是看那个网上推荐系统的视频敲得,但是这个比较旧的视频。现在能支持的是elucidian和l1,l2正则方法
回答:
你这个结论是不正确的,从出错来看,问题在于你传入了 df
这种 pd.DataFrame
对象,而这种对象是没有 dtype
这个属性的(这个属性一般是 numpy
中用的),因而报错。你应该将其转换为 numpy
中的 ndarray
类型,比如 user_similar = 1 - pairwise_distances(df.values, metric='jaccard')
之类的。另外,你确定你看的视频中是你那种写法吗?
以上是 为什么会出现'DataFrame' object has no attribute 'dtype' 的全部内容, 来源链接: utcz.com/p/938222.html