def seg(x, y, name_, num): xyData = segment.DataContainer(x, y) # Create a segmenter instance which fill fit 2 straight lines segmenter = segment.TopDown(segment.LinearRegression, num) # segmenter = segment.BottomUp(segment.LinearRegression, num) # segmenter = segment.TopDown(segment.LineThroughEndPoints, num) # do the fitting fits = segmenter.segment(xyData) fitarr = [] idx = 0 for fit_ in fits.fits: # print(fit) # print(fit.data) # print(fit.coeff[0]) # print(fit.coeff[1]) # print((fit.data.y[len(fit.data.y) - 1] - fit.data.y[0]) / (fit.data.x[len(fit.data.x) - 1] - fit.data.x[0])) fitarr.append([ fit_.data, len(fit_.data), fit_.coeff[0], fit_.coeff[1], fit_, fits ]) # print(fits) # fits.plot() # segment.plt.show() # exit() # segment.plt.savefig(name_ + "_segments") # segment.plt.close() return fitarr
def seg(x, y, name_, num): xyData = segment.DataContainer(x, y) segmenter = segment.TopDown(segment.LinearRegression, num) fits = segmenter.segment(xyData) fitarr = [] idx = 0 for fit_ in fits.fits: fitarr.append([ fit_.data, len(fit_.data), fit_.coeff[0], fit_.coeff[1], fit_, fits ]) return fitarr
def testTopDown(self): s = segment.TopDown(segment.LinearRegression, 2) s.segment(self.data)
#!/usr/bin/env python from __future__ import print_function import segment # Create some data data = segment.DataContainer([0, 1, 2, 3, 4], [0, 1, 2, 1, 0]) # Create a segmenter instance which fill fit 2 straight lines segmenter = segment.TopDown(segment.LinearRegression, 2) # do the fitting fits = segmenter.segment(data) # extract the two lines line1, line2 = fits.fits # Print a summary print "I have fitted two lines to the data given in this table:" for x, y in zip(data.x, data.y): print(x, y) print "The overall error is", fits.error print "The fits are as follows:" for i, fit in enumerate(fits.fits): print(i + 1, "-", fit) # plot a nice graph fits.plot() segment.plt.show()