![](/img/trans.png)
[英]Using class from another file in python package and conflicts in __init__()
[英]Error with the __init__ file from the fit package
我正在嘗試運行我在 GitHub 上找到的代碼來實現分段線性表示。 問題是fit包的init文件似乎有錯誤。 這是代碼:
from matplotlib.pylab import gca, figure, plot, subplot, title, xlabel, ylabel, xlim,show
from matplotlib.lines import Line2D
import segment
import fit
Pull = pandas.read_csv('pull_kpi.csv', parse_dates=['pull_date'])
data = Pull[['pull_date', 'qty_late']]
from matplotlib.pylab import gca, figure, plot, subplot, title, xlabel, ylabel, xlim,show
from matplotlib.lines import Line2D
import segment
import fit
def draw_plot(data,plot_title):
plot(range(len(data)),data,alpha=0.8,color='red')
title(plot_title)
xlabel("Samples")
ylabel("Signal")
xlim((0,len(data)-1))
def draw_segments(segments):
ax = gca()
for segment in segments:
line = Line2D((segment[0],segment[2]),(segment[1],segment[3]))
ax.add_line(line)
with open("example_data/16265-normalecg.txt") as f:
file_lines = f.readlines()
data = [float(x.split("\t")[2].strip()) for x in file_lines[100:320]]
max_error = 0.005
#sliding window with regression
figure()
segments = segment.slidingwindowsegment(data, fit.regression, fit.sumsquared_error, max_error)
draw_plot(data,"Sliding window with regression")
draw_segments(segments)
#bottom-up with regression
figure()
segments = segment.bottomupsegment(data, fit.regression, fit.sumsquared_error, max_error)
draw_plot(data,"Bottom-up with regression")
draw_segments(segments)
#top-down with regression
figure()
segments = segment.topdownsegment(data, fit.regression, fit.sumsquared_error, max_error)
draw_plot(data,"Top-down with regression")
draw_segments(segments)
#sliding window with simple interpolation
figure()
segments = segment.slidingwindowsegment(data, fit.interpolate, fit.sumsquared_error, max_error)
draw_plot(data,"Sliding window with simple interpolation")
draw_segments(segments)
#bottom-up with simple interpolation
figure()
segments = segment.bottomupsegment(data, fit.interpolate, fit.sumsquared_error, max_error)
draw_plot(data,"Bottom-up with simple interpolation")
draw_segments(segments)
#top-down with simple interpolation
figure()
segments = segment.topdownsegment(data, fit.interpolate, fit.sumsquared_error, max_error)
draw_plot(data,"Top-down with simple interpolation")
draw_segments(segments)
show()
該錯誤似乎是無效的語法,Python 說它發生在“raise exc_type, exc_val, exc_tb”這一行。
這是創建錯誤的初始化文件的一部分:
# File I/O methods
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if not exc_val:
return self.close()
raise exc_type, exc_val, exc_tb
有人可以幫忙嗎?
問題是您安裝的來自fit
( GitHub ) 的 fit 包與 Python 3 不兼容(事實上,最近一次更新是在 8 年前,無論如何)。 (其他人對 Python 3 也有同樣的問題。 )
然而,這不是你想要的包——它甚至不導出sumsquared_error
類的東西。
這些導出可以在fit.py
中找到,該存儲庫可能是您改編代碼的地方。
換句話說:
pip uninstall fit
– 這不是您要找的機器人fit.py
和wrappers.py
從該存儲庫復制到您的項目目錄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.