我正在为一个赋值编写代码,该赋值要求您从.txt文件导入数据,并创建两个类类型来查找最小二乘回归值和其他相关值。数据是BMI与收缩压的对比,这两个数据将被读入两个不同的列表,x值和y值。你知道吗
我正试图为这些类运行不同函数的测试,这时我的shell窗口突然关闭了。重新打开后,我发现我无法运行其中一个类的代码,也无法运行将数据读入列表的部分代码。这是错误消息:ModuleNotFoundError:没有名为'data'的模块
这是.py文件中用于将代码读入列表的代码。我不认为列表是问题所在,但我不确定是什么:
from data import Data
from linear_regression import SLR
def read_data(input_file_name):
""" (str) -> list, list
Read data from the path specified by the string input_file.
Data looks like:
18 120
20 110
22 120
25 135
Output two lists, one for each column of data.
"""
list1 = []
list2 = []
read = open(input_file_name, 'r')
read2 = read.readlines()
lines = [line.strip() for line in read2]
for new_line in lines:
list1.append(float(new_line.split(' ')[0]))
list2.append(float(new_line.split(' ')[1]))
return list1, list2
filename = 'bmi_vs_sbp.txt'
x, y = read_data(filename)
dat = Data(x,y)
lm = SLR(dat)
print(lm)
对于没有运行的类,我认为实际函数本身没有问题,但是在定义类和函数之前,最上面的是什么:
from data import Data
class SLR:
def __init__(self, data):
""" (SLR, Data) -> NoneType
Create a new simple linear regression object from data,
with three attributes: data, beta0, and beta1.
"""
self.data = data
self.beta0, self.beta1 = data.compute_least_squares_fit()
def predict(self, x):
""" (SLR, number) -> number
Return predicted value at x.
"""
x_new = self.x = x
y_new = self.beta0 + self.beta1*x_new
return y_new
def compute_residuals(self):
""" (SLR) -> list
Return the residuals in a list of numbers.
"""
residuals = []
for i in range(len(self.data.x)):
residuals.append(self.data.y[i] - self.beta0 - self.beta1*self.data.x[i])
return residuals
def compute_SSResid(self):
""" (SLR) -> number
Return the sum of square residuals (SSResid).
"""
residuals = self.compute_residuals()
SSResid = 0
for i in range(len(self.data.x)):
SSResid += pow(residuals[i],2)
return SSResid
def compute_residual_sd(self):
""" (SLR) -> number
Return the residual standard deviation.
"""
residuals = self.compute_residuals()
residual_sd = pow(self.compute_SSResid()/(len(self.data.x)-2), 1/2)
return residual_sd
def compute_rsquared(self):
""" (SLR) -> number
Return the R-squared of the SLR fit.
"""
rsquared = 1 - (self.compute_SSResid()/self.data.compute_SST())
return rsquared
def __str__(self):
""" (SLR) -> str
Return a string representation of an SLR in this format:
Least squares fit on 10 data points.
Intercept = 45.584331
Slope = 3.465523
Residual standard deviation = 13.051139
R-squared = 0.731364
"""
return print('Least squares fit on {} data points.''\n'
'Intercept = {:.6f}''\n'
'Slope = {:.6f}''\n'
'Residual standard deviation = {:.6f}''\n'
'R-squared = {:.6f}'.format(self.data.num_obs(), self.beta0, self.beta1, self.compute_residual_sd(), self.compute_rsquared()))
导入模块代码是分配中给定的代码之一。第一个类Data需要导入,以便Data中定义的函数可以在SLR中使用。数据函数仍在运行,我已经尝试重新启动,但仍然收到错误消息,没有名为Data的模块。任何帮助都将不胜感激。你知道吗
当代码第一行中的import语句生成时,python解释器将查找该名称的文件/类。假设这是您的文件夹结构:
如果shell位于工作区中,并且运行
$ python python/SLR.py
,那么python将首先查看PYTHONPATH,然后查看当前所在的文件/类。在本例中,这将是workspace
—没有名为data.py
的文件,因此它会因导入错误而崩溃。如果从python文件夹调用$ python SLR.py
,data.py
就在那里,执行起来没有问题。你知道吗可能发生的情况是,上次执行程序时,您与保存
data.py
的文件位于同一文件夹中,当它崩溃时,您从其他地方调用了代码。你知道吗解决此问题的最佳方法可能是将保存
data
内容的文件夹的位置添加到PYTHONPATH中,如链接中所述。你知道吗相关问题 更多 >
编程相关推荐