如何修复“ AttributeError:'模块'对象没有属性'x'”?

时间:2019-01-07 04:51:42

标签: python spyder attributeerror

我想看一个非常简单的情节。但是在“ def”中出现“ x”错误。请让我知道为什么。

Python 2.7 / spyder

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() 

我收到的输出是:

  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'

1 个答案:

答案 0 :(得分:3)

Numpy没有任何“ x”。点运算符用于调用该类的函数。我认为您正在尝试return a*x+b