Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas attribute error : no attribute 'Factor' found

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

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()
like image 512
Yantra Avatar asked Feb 10 '14 22:02

Yantra


People also ask

How do I fix pandas attribute error?

The most likely cause of the error is having a local file named pandas.py which shadows the official pandas module. Make sure you haven't misspelled DataFrame as class names are case-sensitive. Make sure to rename your local file to something other than pandas.py to solve the error.

How do you solve a DataFrame object has no attribute?

If you try to call concat() on a DataFrame object, you will raise the AttributeError: 'DataFrame' object has no attribute 'concat'. You have to pass the columns to concatenate to pandas. concat() and define the axis to concatenate along.

Is there a limit to pandas DataFrame?

The short answer is yes, there is a size limit for pandas DataFrames, but it's so large you will likely never have to worry about it. The long answer is the size limit for pandas DataFrames is 100 gigabytes (GB) of memory instead of a set number of cells.


1 Answers

In newer versions of pandas, the Factor is called Categorical instead. Change your line to:

df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
like image 115
David Robinson Avatar answered Oct 11 '22 15:10

David Robinson