GVKun编程网logo

如何修复AttributeError:模块'numpy'没有属性'square'(attributeerror:module has no attribute)

9

在本文中,您将会了解到关于如何修复AttributeError:模块'numpy'没有属性'square'的新资讯,同时我们还将为您解释attributeerror:modulehasnoattrib

在本文中,您将会了解到关于如何修复AttributeError:模块'numpy'没有属性'square'的新资讯,同时我们还将为您解释attributeerror:module has no attribute的相关在本文中,我们将带你探索如何修复AttributeError:模块'numpy'没有属性'square'的奥秘,分析attributeerror:module has no attribute的特点,并给出一些关于AttributeError-'numpy.ndarray'对象没有属性'drop'、AttributeError: 'numpy.float64' 对象没有属性 '_id'、AttributeError:''numpy.ndarray'对象没有属性'rolling''仅在过滤CSV数据后出现、AttributeError:'str'对象没有属性'isnumeric'的实用技巧。

本文目录一览:

如何修复AttributeError:模块'numpy'没有属性'square'(attributeerror:module has no attribute)

如何修复AttributeError:模块'numpy'没有属性'square'(attributeerror:module has no attribute)

我已经将numpy更新为1.14.0。我使用Windows10。我尝试运行代码,但出现此错误:

AttributeError:模块“ numpy”没有属性“ square”

这是我的进口商品:

%matplotlib inlineimport matplotlib.pyplot as pltimport tensorflow as tfimport numpy as npfrom sklearn.metrics import confusion_matriximport math

答案1

小编典典

我删除了numpy.py,然后更新了我的numpy,它起作用了!

AttributeError-'numpy.ndarray'对象没有属性'drop'

AttributeError-'numpy.ndarray'对象没有属性'drop'

如何解决AttributeError-''numpy.ndarray''对象没有属性''drop''?

对于当前的项目,我计划在包含数字数据的CSV集合上运行scikit-learn随机梯度助推器算法。

调用脚本的X = Germany.drop(''Status'',axis=''columns'')时,我收到了AttributeError: ''numpy.ndarray'' object has no attribute ''drop''

我认为此错误可能与以下事实有关:我正在转换CSV数据pd.to_numeric,这也可能会转换字符串标题。有没有可以进行此操作的智能调整?

CSV数据具有以下结构:

enter image description here

相应的代码如下:

Germany = pd.read_csv(''./Germany_filtered.csv'',index_col=0)
Germany = Germany.fillna("")
Germany = pd.to_numeric(Germany.columns.str,errors=''coerce'')
Germany.head()

X = Germany.drop(''Status'',axis=''columns'')
y = Germany[''Status'']

解决方法

In [167]: df = pd.DataFrame(np.arange(12).reshape(3,4),columns=[''a'',''b'',''c'',''d''])

drop在数据框上可以正常工作:

In [168]: df.drop(''c'',axis=''columns'')
Out[168]: 
   a  b   d
0  0  1   3
1  4  5   7
2  8  9  11

to_numeric产生一个numpy数组:

In [169]: x = pd.to_numeric(df.columns.str,errors=''coerce'')
In [170]: x
Out[170]: 
array(<pandas.core.strings.StringMethods object at 0x7fef602862b0>,dtype=object)
In [171]: type(x)
Out[171]: numpy.ndarray

在进入head之前,它应该抱怨drop

In [172]: x.head()
Traceback (most recent call last):
  File "<ipython-input-172-830ed5e65d76>",line 1,in <module>
    x.head()
AttributeError: ''numpy.ndarray'' object has no attribute ''head''

In [173]: x.drop()
Traceback (most recent call last):
  File "<ipython-input-173-6d3a33341569>",in <module>
    x.drop()
AttributeError: ''numpy.ndarray'' object has no attribute ''drop''

to_numeric文档怎么说?我没有与之合作,但显然您不想将其传递给df.columns.str对象。我尚未使用此功能,但让我们尝试将其传递给数据框:

In [176]: x = pd.to_numeric(df,errors=''coerce'')
Traceback (most recent call last):
  File "<ipython-input-176-d095b0166b8f>",in <module>
    x = pd.to_numeric(df,errors=''coerce'')
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/tools/numeric.py",line 139,in to_numeric
    raise TypeError("arg must be a list,tuple,1-d array,or Series")
TypeError: arg must be a list,or Series

因此,让我们传递一列/系列:

In [177]: x = pd.to_numeric(df[''a''],errors=''coerce'')
In [178]: x
Out[178]: 
0    0
1    4
2    8
Name: a,dtype: int64

结果Series可以在同一列或新列中分配回数据框:

In [179]: df[''a''] = x
In [180]: df
Out[180]: 
   a  b   c   d
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

现在在我的示例框架中,无需进行此转换,但它应该可以为您提供一些帮助。


让我们尝试真正的字符串转换:

In [195]: df[''a''] = [''00'',''04'',''LS'']
In [196]: df
Out[196]: 
    a  b   c   d
0  00  1   2   3
1  04  5   6   7
2  LS  9  10  11

链接的答案无济于事:

In [197]: pd.to_numeric(df.columns.str,errors=''coerce'')
Out[197]: 
array(<pandas.core.strings.StringMethods object at 0x7fef602862b0>,dtype=object)

但是我的版本确实产生了一个数字系列:

In [198]: pd.to_numeric(df[''a''],errors=''coerce'')
Out[198]: 
0    0.0
1    4.0
2    NaN
Name: a,dtype: float64

AttributeError: 'numpy.float64' 对象没有属性 '_id'

AttributeError: 'numpy.float64' 对象没有属性 '_id'

如何解决AttributeError: ''numpy.float64'' 对象没有属性 ''_id''?

我正在为我的训练回合进行自定义,但是在计算 loss_value 时出现此错误。我该怎么做才能修复它?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

AttributeError:''numpy.ndarray'对象没有属性'rolling''仅在过滤CSV数据后出现

AttributeError:''numpy.ndarray'对象没有属性'rolling''仅在过滤CSV数据后出现

如何解决AttributeError:''''numpy.ndarray''对象没有属性''rolling''''仅在过滤CSV数据后出现?

如果我按照下面给出的方法传递csv数据的值,则会产生输出。

data = pd.read_csv("abc.csv")
avg = data[''A''].rolling(3).mean() 
print(avg)

但是,如果按照下面给出的方法传递值,则会产生错误。

dff=[]
dff1=[]
dff1=abs(data[''A''])

b,a = scipy.signal.butter(2,0.05,''highpass'')
dff = scipy.signal.filtfilt(b,a,dff1) 

avg = dff.rolling(3).mean() 
print(avg)

错误是:

AttributeError:''numpy.ndarray''对象没有属性''rolling''

我不明白,代码有什么问题?

应用dff = pd.Dataframe(dff)后出现新问题。顶部显示一个意外的零。

enter image description here

这是什么原因?如何摆脱这个问题?

解决方法

rolling是对熊猫SeriesDataFrame的功能。 Scipy对这些一无所知,并生成Numpy ndarray作为输出。它可以接受数据框和序列作为 input ,因为Pandas类型可以在需要时模仿ndarray。

解决方案可能就像使用

将ndarray重新包装为数据框一样简单。
dff = pd.Dataframe(dff)

AttributeError:'str'对象没有属性'isnumeric'

AttributeError:'str'对象没有属性'isnumeric'

如何解决AttributeError:''str''对象没有属性''isnumeric''?

str.isnumeric()仅在Python 3上可用。该错误表明您使用的是Python 2,而只有Python 2unicode.isnumeric()存在。

您应该真正使用str.isdecimal()或更好地使用 异常处理

def p2f(x):
    try:
        return float(x.strip(''%''))/100
    except ValueError:
        return 0.0 if x in (''SUPP'', ''NEW'', ''LOWCOV'', ''NA'', '''') else x

.isnumeric()匹配float()不接受的BMP中的430个Unicode代码点,并且有些代码点.isdigit()返回true也不能转换。

您可以生成自己的表进行检查:

for i in range(2 ** 16):
    c = chr(i)
    if c.isnumeric() or c.isdigit() or c.isdecimal():
        try:
            f = float(c)
        except ValueError:
            f = ''<not convertible>''
        di, de, nu = (''\u2705'' if test() else ''\u274c'' for test in (c.isdigit, c.isdecimal, c.isnumeric))
        print(f''{c!a:<6} {c}\tdigit: {di}   decimal: {de}   numeric: {nu}  float: {f}'')

产生如下输出:

''0''    0    digit: ✅   decimal: ✅   numeric: ✅  float: 0.0
''1''    1    digit: ✅   decimal: ✅   numeric: ✅  float: 1.0
''2''    2    digit: ✅   decimal: ✅   numeric: ✅  float: 2.0
''3''    3    digit: ✅   decimal: ✅   numeric: ✅  float: 3.0
''4''    4    digit: ✅   decimal: ✅   numeric: ✅  float: 4.0
''5''    5    digit: ✅   decimal: ✅   numeric: ✅  float: 5.0
''6''    6    digit: ✅   decimal: ✅   numeric: ✅  float: 6.0
''7''    7    digit: ✅   decimal: ✅   numeric: ✅  float: 7.0
''8''    8    digit: ✅   decimal: ✅   numeric: ✅  float: 8.0
''9''    9    digit: ✅   decimal: ✅   numeric: ✅  float: 9.0
''\xb2'' ²    digit: ✅   decimal: ❌   numeric: ✅  float: <not convertible>
''\xb3'' ³    digit: ✅   decimal: ❌   numeric: ✅  float: <not convertible>
''\xb9'' ¹    digit: ✅   decimal: ❌   numeric: ✅  float: <not convertible>
''\xbc'' ¼    digit: ❌   decimal: ❌   numeric: ✅  float: <not convertible>
''\xbd'' ½    digit: ❌   decimal: ❌   numeric: ✅  float: <not convertible>
''\xbe'' ¾    digit: ❌   decimal: ❌   numeric: ✅  float: <not convertible>

并且您会发现只有该decimal列的所有不可转换代码点都有叉号。

如果要isdecimal()在Python 2中使用,则必须先将字节字符串解码为Unicode。

解决方法

有点困惑,因为我很肯定我以前曾经做过这项工作。

我创建了以下方法…

def p2f(x):
    if x.strip(''%'').isnumeric():
        return float(x.strip(''%''))/100
    elif x in [''SUPP'',''NEW'',''LOWCOV'',''NA'','''']:
        return 0.0
    else:
        return x

但是当我在导入的CSV文件上运行它时,会产生此错误:

AttributeError: ''str'' object has no attribute ''isnumeric''

尽管我可以看到这isnumericstr文档中的一个属性:

https://pandas.pydata.org/pandas-
docs/stable/generation/pandas.Series.str.isnumeric.html?highlight=isnumeric#pandas.Series.str.isnumeric

除非我没有正确解释信息?

今天关于如何修复AttributeError:模块'numpy'没有属性'square'attributeerror:module has no attribute的讲解已经结束,谢谢您的阅读,如果想了解更多关于AttributeError-'numpy.ndarray'对象没有属性'drop'、AttributeError: 'numpy.float64' 对象没有属性 '_id'、AttributeError:''numpy.ndarray'对象没有属性'rolling''仅在过滤CSV数据后出现、AttributeError:'str'对象没有属性'isnumeric'的相关知识,请在本站搜索。

本文标签: