简体   繁体   English

如何修复“AttributeError:‘module’对象没有属性‘x’”?

[英]how to fix "AttributeError: 'module' object has no attribute 'x' "?

I want to see a very simple plot.我想看一个非常简单的情节。 But I get an error for 'x' in 'def'.但是我在'def'中收到'x'的错误。 Please let me know why.请告诉我原因。

Python 2.7 / spyder Python 2.7 / 间谍

import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import pandas as pd
import math

def func(x,a,b,): 
    return a*np.x+b

x=np.linspace(-1,1,100)
y=func(x,1,1)
plt.plot(x,y,linewidth=3, color='red')
plt.show() 

Output I receive is :我收到的输出是:

  runfile('I:/K/python/least square_HW.py', wdir='I:/K/python') Traceback (most recent call last):

  File "<ipython-input-19-c03c1aa079d4>", line 1, in <module> runfile('I:/K/python/least square_HW.py', wdir='I:/K/python')
  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile execfile(filename, namespace)
  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 93, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "I:/K/python/least square_HW.py", line 18, in <module> y=func(x,1,1)
  File "I:/K/python/least square_HW.py", line 14, in func return a*np.x+b
        AttributeError: 'module' object has no attribute 'x'

Numpy does not have any 'x'. Numpy 没有任何“x”。 The dot operator is used to call function of that class.点运算符用于调用该类的函数。 I think you are trying return a*x+b .我认为您正在尝试return a*x+b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM