English 中文(简体)
AttributeError: numpy.int64 物体无从属性
原标题:AttributeError: numpy.int64 object has no attribute startswith

当我试图在一只大笔记本中形成一种与海出生的分辨率时,我会发现这一错误。

Here s the end of the stack trace:

/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.pyc in get_legend_handles_labels(self, legend_handler_map)
   4317             label = handle.get_label()
   4318             #if (label is not None and label !=    and not label.startswith( _ )):
-> 4319             if label and not label.startswith( _ ):
   4320                 handles.append(handle)
   4321                 labels.append(label)

AttributeError:  numpy.int64  object has no attribute  startswith 

我进口:

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

import math

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style( whitegrid )
%matplotlib inline

from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import train_test_split

from sklearn import metrics

import statsmodels.api as sm

我的守则如下:

df = sm.datasets.fair.load_pandas().data
df[ had_affair ] = df.affairs.apply(lambda x: 1 if x != 0 else 0)
sns.factorplot( age , data=df, hue= had_affair , palette= coolwarm )

问题似乎是,使用<代码>hue的Im栏是一种ger,而不是一种str。 使用<代码>df[有_affair_str] = df.had_affair.apply(str),然后将had_affair_str作为我的hue,使错误消失,但在线辅导一米在使用这一准确代码时没有发现任何错误。 这是否是一个已知的mat子或海生问题? 我的包裹是否过时?

这里是我的素养包的版本:

ipython==3.1.0
numpy==1.9.2
pandas==0.16.1
matplotlib==1.4.3
seaborn==0.5.1
scikit-learn==0.16.1
statsmodels==0.6.1

编辑:

<代码>df.info(>>:

<class  pandas.core.frame.DataFrame >
Int64Index: 6366 entries, 0 to 6365
Data columns (total 11 columns):
rate_marriage      6366 non-null float64
age                6366 non-null float64
yrs_married        6366 non-null float64
children           6366 non-null float64
religious          6366 non-null float64
educ               6366 non-null float64
occupation         6366 non-null float64
occupation_husb    6366 non-null float64
affairs            6366 non-null float64
had_affair         6366 non-null int64
had_affair_str     6366 non-null object
dtypes: float64(9), int64(1), object(1)
memory usage: 596.8+ KB
问题回答

校正期望您的标签系列的类型had_affair成为反对/扼杀,但是一种 n。 注

你们可以强行改变 n。 第64条:

df[ had_affair ] = df[ had_affair ].astype(str)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签