Python 如何在另一个文件的类中调用函数?
我在data.py中有以下代码Python 如何在另一个文件的类中调用函数?,python,python-3.x,Python,Python 3.x,我在data.py中有以下代码 import numpy as np from scipy import stats class Data: def __init__(self, x, y): """ (Data, list, list) -> NoneType Create a new data object with two attributes: x and y. """ self.x = x
import numpy as np
from scipy import stats
class Data:
def __init__(self, x, y):
""" (Data, list, list) -> NoneType
Create a new data object with two attributes: x and y.
"""
self.x = x
self.y = y
def compute_least_squares_fit(self):
""" (Data) -> number, number
Return the intercept and slope of the simple linear regression fit
of the data.
"""
slope, intercept, r_value, p_value, std_err = stats.linregress(self.x,self.y)
return slope, intercept
def compute_SST(self):
""" (Data) -> number
Return the sum of squares total (SST).
"""
avg_y = np.mean(self.y)
squared_errors = (self.y - avg_y) ** 2
return np.sum(squared_errors)
我在另一个文件里有这个代码
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
from data import Data
class SLR:
def __init__(self, data):
""" (SLR, Data) -> NoneType
Create a new simple linear regression object from data,
with data data, intercept beta0, and slope beta1.
"""
如何使用从另一个文件到这个文件的截距和斜率?在
\uuuu init\uuuu
方法中,您有一个参数data
,它应该是data
的一个实例。现在调用data.compute\u least\u squares\u fit()
并使用返回值。谢谢,但是如何返回值呢?我可以直接使用截距和斜率变量吗?在方法中,您有一个参数data
,它应该是data
的一个实例。现在调用data.compute\u least\u squares\u fit()
并使用返回值。谢谢,但是如何返回值呢?我可以直接使用截距和斜率变量吗?