pandas 熊猫属性错误:找不到属性“因子”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21689423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:41:09  来源:igfitidea点击:

pandas attribute error : no attribute 'Factor' found

pythonpython-2.7pandas

提问by Yantra

I'm trying to run code provided by yhat in their article about random forests in Python, but I keep getting following error message:

我正在尝试运行 yhat在他们关于 Python 中的随机森林的文章中提供的代码,但我不断收到以下错误消息:

File "test_iris_with_rf.py", line 11, in <module>
    df['species'] = pd.Factor(iris.target, iris.target_names)
AttributeError: 'module' object has no attribute 'Factor'

Code:

代码:

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np

iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
print df
print iris.target_names
df['is_train'] = np.random.uniform(0, 1, len(df)) <= .75

df['species'] = pd.Factor(iris.target, iris.target_names)

df.head()

回答by David Robinson

In newer versions of pandas, the Factoris called Categoricalinstead. Change your line to:

在较新版本的Pandas中,改为Factor调用Categorical。将您的线路更改为:

df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)

回答by Andrew Rosenfeld

Categorical variables seems to be one of the more active areas of development in pandas, so I believe it's changed yet again in pandas 0.15.0 :

分类变量似乎是 pandas 中更活跃的发展领域之一,所以我相信它在 pandas 0.15.0 中再次发生了变化:

df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)

(I lacked sufficient reputation to add this as a comment on David Robinson's answer)

(我缺乏足够的声誉来将其添加为对大卫罗宾逊回答的评论)

回答by S?ren

def factor(series):
    #input should be a pandas series object
    dic = {}
    for i,val in enumerate(series.value_counts().index):
        dic[val] = i
    return [ dic[val] for val in series.values ]