def main(): data = sp.genfromtxt('./data/web_traffic.tsv', delimiter='\t') x = data[:, 0] y = data[:, 1] x = x[~sp.isnan(y)] y = y[~sp.isnan(y)] fp1 = sp.polyfit(x, y, 1) print('Model parameters for fp1 %s' % fp1) f1 = sp.poly1d(fp1) print('This is the error rate for fp1 %f' % error(f1, x, y)) fp2 = sp.polyfit(x, y, 2) print('Model parameters for fp2 %s' % fp2) f2 = sp.poly1d(fp2) print('This is the error rate for fp2 %f' % error(f2, x, y)) plt.scatter(x, y,color= 'pink') plt.title('My first impression') plt.xlabel('Time') plt.ylabel('#Hits') plt.xticks([w * 7 * 24 for w in range(10)], ['week %i' % w for w in range(10)]) fx = sp.linspace(0, x[-1], 1000) plt.plot(fx, f1(fx), linewidth=3,color='cyan') plt.plot(fx, f2(fx), linewidth=3, linestyle='--',color= 'red') plt.legend(['d = %i' %f1.order, 'd = %i' %f2.order], loc='upper left') plt.autoscale(tight=True) plt.grid() plt.show()
def lagrange(self): '''Lagrange Interpolation. Unsuccessful because it's a linear equation, so doesn't get the target result that I want. The result is -3565+2421.41*(x)''' orig_stdout = sys.stdout f = file('lagrangeout.txt', 'w') sys.stdout = f tmp = scipy.poly1d([0]) result=scipy.poly1d([0]) for value,key in zip(self.complaint_allstate.values(),self.complaint_allstate.keys()): for i in value.keys(): numerator=scipy.poly1d([1]) denom = 1.0 for j in value.keys(): if (i != j): tmp = scipy.poly1d([1,-j]) numerator = numerator * tmp denom = denom * (i - j) tmp = (numerator/denom) * value.get(i) result = result + tmp print key + " : " print result print '\n' sys.stdout = orig_stdout f.close()
def _systopoly1d(sys): """Extract numerator and denominator polynomails for a system""" # Allow inputs from the signal processing toolbox if (isinstance(sys, scipy.signal.lti)): nump = sys.num denp = sys.den else: # Convert to a transfer function, if needed sys = _convert_to_transfer_function(sys) # Make sure we have a SISO system if (sys.inputs > 1 or sys.outputs > 1): raise ControlMIMONotImplemented() # Start by extracting the numerator and denominator from system object nump = sys.num[0][0] denp = sys.den[0][0] # Check to see if num, den are already polynomials; otherwise convert if (not isinstance(nump, poly1d)): nump = poly1d(nump) if (not isinstance(denp, poly1d)): denp = poly1d(denp) return (nump, denp)
def find_roots_simple(B=0.0, kr=0.0, kz=0.0, Omega=0.0, zeta=0.0, m=1.0, nu=2.98e-3, eta=2.57e3, rho=6.36, r1=7.06, r2=20.3): """Solve the local dispersion relation.""" pi = math.pi ktheta = m/(0.5*(r2 + r1)) k = sqrt(kr**2 + kz**2 + ktheta**2) va = B*1.0/math.sqrt(4.0*pi*rho) wa = kz*va #Alfven frequency \omega_A #gn = \gamma_n = \gamma + \eta k^2 = i\omega + \eta k**2 gn = scipy.poly1d([1, eta*k**2]) gv = scipy.poly1d([1, nu*k**2]) dr = (gn*gv + wa**2)**2 + 2*Omega**2*zeta*(kz**2/k**2)*gn**2 \ + 2*Omega**2*(zeta-2)*(kz**2/k**2)*wa**2 dr_roots = roots(dr) #Convert to \omega #Since \gamma t = -i \omega t, \omega = i \gamma dr_roots_return = sort(1j*dr_roots) return dr_roots_return
def trainingAndTesting(self): global train global x, y, xa, xb, ya, yb # separating training from testing data frac = 0.3 split_idx = int(frac * len(xb)) rangeX = range(len(xb)) listX = list(rangeX) logging.info("delta : %i", len(set(rangeX).difference(listX))) shuffled = sp.random.permutation(list(range(len(xb)))) test = sorted(shuffled[:split_idx]) train = sorted(shuffled[split_idx:]) fbt1 = sp.poly1d(sp.polyfit(xb[train], yb[train], 1)) fbt2 = sp.poly1d(sp.polyfit(xb[train], yb[train], 2)) print("fbt2(x)= \n%s" % fbt2) print("fbt2(x)-100,000= \n%s" % (fbt2 - 100000)) print("fbt2(x)= \n%s" % (fbt2)) fbt3 = sp.poly1d(sp.polyfit(xb[train], yb[train], 3)) fbt10 = sp.poly1d(sp.polyfit(xb[train], yb[train], 10)) fbt100 = sp.poly1d(sp.polyfit(xb[train], yb[train], 100)) print("Test errors for only the time after inflection point") for f in [fbt1, fbt2, fbt3, fbt10, fbt100]: print("Error d=%i: %f" % (f.order, self.error(f, xb[test], yb[test])))
def fitKvsPower(self, filename="PowerVGain.dat", zeroed="_0"): if not hasattr(self, "k_avg"): raise RuntimeError, "Must load all data and run calculate() before fitting" gain, self.power = loadtxt(filename, skiprows=1, unpack=True) if zeroed != None: gain_0, power_0 = loadtxt(filename.replace(".", zeroed + ".", 1), skiprows=1, unpack=True) self.power = self.power - power_0 savetxt(filename.replace(".", "_subtract."), self.power, fmt="%.5f") if self.gain.tolist() != gain.tolist(): raise ValueError, "Laser power was not measured over the same gains as calibration measurements:\n\ Laser gain: " + str( gain ) + "\nMeasurement gains: " + str( self.gain ) self.kfitX = polyfit(self.power, self.k_avg[:, 0], 1) self.kfitY = polyfit(self.power, self.k_avg[:, 1], 1) print "Fit parameters for X axis:" print poly1d(self.kfitX, variable="power") print "\nFit parameters for Y axis:" print poly1d(self.kfitY, variable="power")
def plotFullModel(dm, sacc): """ """ fig = plt.figure(figsize = (15, 5)) plt.suptitle("Sacc = %s" % sacc) ax1 = plt.subplot(141) lCols = ["blue", "red", "orange", "yellow", "green", "pink"] xVar = "sacc%s_ex" % sacc yVar = "sacc%s_ey" % sacc dm = dm.select("%s != ''" % xVar) dm = dm.select("%s != ''" % yVar) dm = dm.select("%s != -1000" % xVar) dm = dm.select("%s != -1000" % yVar) for a in dm.unique("realAngle"): _dm = dm.select("realAngle == %s" % a) col = lCols.pop() plt.scatter(_dm[xVar], _dm[yVar], color = col, marker = ".", label = a) plt.axvline(constants.xCen, color = gray[3], linestyle = '--') plt.axhline(constants.yCen, color = gray[3], linestyle = '--') ax2 = plt.subplot(142) pm = PivotMatrix(dm, ["stim_type", "realAngle"], ["file"], "xNorm1", colsWithin =True) pm.linePlot(fig = fig) pm.save("PM.csv") pm._print() plt.axhline(0, color = gray[3], linestyle = "--") ax3 = plt.subplot(143) plt.title("object") dmObj= dm.select("stim_type == 'object'") #slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(stimObj["ecc"], stimObj["xNorm1"]) x = dmObj["ecc"] y = dmObj["xNorm%s" % sacc] fit = scipy.polyfit(x,y,1) fit_fn = scipy.poly1d(fit) plt.plot(x,y, 'yo', x, fit_fn(x), '--k') ax4 = plt.subplot(144) plt.title("non-object") dmNo= dm.select("stim_type == 'non-object'") #slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(stimObj["ecc"], stimObj["xNorm1"]) x = dmNo["ecc"] y = dmNo["xNorm%s" % sacc] fit = scipy.polyfit(x,y,1) fit_fn = scipy.poly1d(fit) plt.plot(x,y, 'yo', x, fit_fn(x), '--k') plt.savefig("./plots/Full_model_004C_sacc%s.png" % sacc) plt.show()
def find_roots(B=0.0, kr=0.0, kz=0.0, Omega=0.0, zeta=0.0, m=0, nu=2.98e-3, eta=2.57e3, rho=6.36, r1=7.06, r2=20.3): #Parameters are in cgs units (Gauss, cm, 1/cm, cm^2/sec, gm/cm^3, etc) #We will be solving for the Doppler shifted frequency #\bar{\omega} = \omega - m\Omega pi = math.pi ktheta = m/(0.5*(r2+r1)) k = sqrt(kr**2 + kz**2 + ktheta**2) va = B*1.0/math.sqrt(4.0*pi*rho) wa = kz*va #Alfven frequency \omega_A wr = (zeta-2)*Omega*kr*ktheta/k**2 #Rossby wave frequency \omega_R kfrac = kz/k #wn = \omega_\eta = \bar{\omega} - i\eta k^2 #wv = \omega_\nu = \bar{\omega} - i\nu k^2 wn = scipy.poly1d([1, -1j*eta*k**2]) wv = scipy.poly1d([1, -1j*nu*k**2]) #Note that for all of these terms are start off with the operations #on the poly1d objects, because otherwise I think I've seen instances #where the operator overloading fails, and multiplies, adds, and #exponentiations don't treat the objects correctly. #Term 1: \omega_{\eta}(\omega_A^2 - \omega_{\eta}\omega_{\nu})^2 term1 = wn*(-wn*wv + wa**2)**2 #Term 2: -2\zeta\Omega^2(k_z^2/k^2)\omega_{\eta}^3 term2 = -wn**3*2.0*zeta*Omega**2*kfrac**2 #Term 3: 2(\zeta-2)\Omega^2\omega_A^2(k_z^2/k^2)\omega_{\eta} term3 = wn*2.0*(zeta-2)*Omega**2*wa**2*kfrac**2 #Term 4: i*\omega_R*(\omega_A^2 - \omega_{\nu}\omega_{\eta})* #(\omega_A^2 - \omega_{\eta}^2) #where \omega_R = (\zeta-2)\Omega k_r k_{\theta} / k^2 term4 = (-wv*wn + wa**2)*(-wn**2 + wa**2)*1j*wr dr = term1 + term2 + term3 + term4 #Minus sign here, because this whole thing was derived with #a bizarre assumed dependence as exp(\omega t - k\cdot x), when #a reasonable right-going wave is exp(k\cdot x - \omega t). dr_roots = -roots(dr) return sort(dr_roots)
def lagrangeInterpolation (x, gx): #setting result = 0 result = scipy.poly1d([0.0]) for i in range(0,len(x)): #number of polynomials L_k(x). temp_numerator = scipy.poly1d([1.0]) # resets temp_numerator such that a new numerator can be created for each i. denumerator = 1.0 #resets denumerator such that a new denumerator can be created for each i. for j in range(0,len(x)): if i != j: temp_numerator *= scipy.poly1d([1.0,-x[j]]) #finds numerator for L_i denumerator *= x[i]-x[j] #finds denumerator for L_i result += (temp_numerator/denumerator) * gx[i] #linear combination return result;
def save(self, filename='calibration.dat', delimiter='\t', raw=True): # save calculated values in comments comments=\ """# ky vs power: %s # kx vs power: %s\n""" % \ (str(poly1d(self.kfitX, variable='power')).strip(), str(poly1d(self.kfitY, variable='power')).strip() ) # patterns for col names of raw data lro_raw_headings = ['kxVar%d', 'kyVar%d', 'kxLRO%d', 'kyLRO%d'] var_raw_headings = ['kx%d', 'ky%d'] # col names for raw data if raw: apply_to = lambda mylist, value: [ x % value for x in mylist ] lro_head = [] var_head = [] for lronum, varnum in zip(self.__lro_file_list, self.__var_file_list): lro_head += apply_to(lro_raw_headings, lronum) var_head += apply_to(var_raw_headings, varnum) # col names for average values header = [ 'gain', 'power', 'kx_avg', 'ky_avg', 'kxLRO_avg', 'kyLRO_avg', 'kxvar_avg', 'kyvar_avg', 'ky_stokes' ] if raw: header += lro_head + var_head # Tuple of data to be saved in the same order as header listing data = (self.gain, self.power, self.k_avg, self.LRO_avg, self.variance_k_avg, self.stokes_avg) if raw: # Except first column (gain), append raw data loaded from each file data += (self.LRO_raw[:,1:], self.var_raw[:,1:]) # data variable must be reshaped so it can be stacked column ways when saved def my_shape(x): if x.ndim > 1: return x else: return reshape(x, (-1, 1)) save_data = hstack(map(my_shape, data)) with open(filename, 'w') as fh: fh.write(comments) fh.write( delimiter.join(header) + '\n') savetxt( fh, save_data, fmt='%.6e', delimiter=delimiter) print "Data saved to file %s" % filename
def lagrange(points): """ Lagrange Interpolation through a set of points """ tmp = scipy.poly1d([0]) result=scipy.poly1d([0]) for i in points.keys(): numerator=scipy.poly1d([1]) denom = 1.0 for j in points.keys(): if (i != j): tmp = scipy.poly1d([1,-j]) numerator = numerator * tmp denom = denom * (i - j) tmp = (numerator/denom) * points.get(i) result = result + tmp return result
def main(): data = sp.genfromtxt("web_traffic.tsv", delimiter="\t") plt.xkcd() x = data[:, 0] y = data[:, 1] x = x[~sp.isnan(y)] y = y[~sp.isnan(y)] fp1, _, _, _, _ = sp.polyfit(x, y, 1, full=True) # Here we try 3 degrees of freedom fp2, _, _, _, _ = sp.polyfit(x, y, 3, full=True) f1 = sp.poly1d(fp1) f2 = sp.poly1d(fp2) # We have an obvious inflection point between 3rd and 4th week inflection_in_hours = int(3.5 * 7 * 24) x_before_inflection = x[:inflection_in_hours] x_after_inflection = x[inflection_in_hours:] y_after_inflection = y[inflection_in_hours:] f_after = sp.poly1d(sp.polyfit(x_after_inflection, y_after_inflection, 1)) fx = sp.linspace(0, x[-1], 1000) fx_after = sp.linspace(len(x_before_inflection)+1, x[-1], 1000) plt.scatter(x, y, s=5) plt.title("Web traffic over the last month.") plt.xlabel("Time") plt.ylabel("Hits/hour") plt.xticks([w * 7 * 24 for w in range(10)], ['week {}'.format(w) for w in range(10)]) plt.autoscale(tight=True) plt.plot(fx, f1(fx), linewidth=2) plt.plot(fx, f2(fx), linewidth=2) plt.plot(fx_after, f_after(fx_after), linewidth=3) plt.legend(["d={}".format(f1.order), "d={}".format(f2.order), "d after inflection"], loc="upper left") # plt.grid(True, linestyle="-", color='0.75') plt.show()
def correct_slope(self,rank=4): index = np.linspace(0,self.nsamp-1,self.nsamp) trends = np.zeros((self.nsamp,self.norders)) i = 0 ''' for order in self.orders: fsubs = np.isfinite(order['flux']) pars = polyfit(index[fsubs][20:-20],order['flux'][fsubs][20:-20],deg=rank,w=order['uflux'][fsubs][20:-20]) trend = poly1d(pars) trend_samp = trend(index) trends[:,i] = trend_samp/np.median(trend_samp) i += 1 trend_mean = np.median(trends,axis=1) ''' fluxes = np.zeros((self.nsamp,self.norders)) for i in np.arange(self.norders): flux = self.orders[i]['flux'] flux = flux/np.median(flux) fluxes[:,i] = flux i += 1 trend_mean = np.nanmedian(fluxes,axis=1) fsubs = np.isfinite(trend_mean) pars = polyfit(index[fsubs],trend_mean[fsubs],deg=rank) trend_smooth = poly1d(pars)(index) for order in self.orders: order['flux'] /= trend_smooth
def two_line(x, y): x1 = x[ x<3*24*7] x2 = x[ x>=3*24*7] y1 = y[ x<3*24*7] y2 = y[ x>=3*24*7] fp1 = sp.polyfit(x1, y1, 1) fp2 = sp.polyfit(x2, y2, 1) f1= sp.poly1d(fp1) f2 = sp.poly1d(fp2) plotdata(x, y) from matplotlib import pyplot as pl x_1 = sp.linspace(0, x1[-1], 1000) x_2 = sp.linspace(x1[-1], x2[-1], 1000) pl.plot(x_1, f1(x_1), linewidth=3, color="blue") pl.plot(x_2, f2(x_2), linewidth=3, color="red") pl.show()
def norm_group(pos,trail,**kargs): """Takes the drain current for each file in group and builds an analysis file and works out the mean drain""" if "signal" in kargs: signal=kargs["signal"] else: signal="fluo" lfit=kargs["lfit"] rfit=kargs["rfit"] posfile=SA.AnalyseFile() posfile.metadata=pos[0].metadata posfile=posfile&pos[0].column(0) posfile.column_headers=['Energy'] for f in pos: print str(f["run"])+str(f.find_col(signal)) posfile=posfile&f.column(signal) posfile.add_column(lambda r:np.mean(r[1:]),"mean drain") ec=posfile.find_col('Energy') md=posfile.find_col('mean drain') posfile=SA.AnalyseFile(posfile) linearfit=scipy.poly1d(posfile.polyfit(ec,md,1,lambda x,y:lfit[0]<=x<=lfit[1])) posfile.add_column(lambda r:r[md]-linearfit(r[ec]),'minus linear') highend=posfile.mean('minus',lambda r:rfit[0]<=r[ec]<=rfit[1]) ml=posfile.find_col('minus linear') posfile.add_column(lambda r:r[ml]/highend,"normalised") if "group_key" in kargs: posfile[kargs["group_key"]]=pos.key return posfile
def averaging(self, data, Start = None, End = None, N = 1, m = 3): ''' Усереднення між заданими вузлами. m - порядок полінома N - кількість вузлів ''' x, y = data[:,0], data[:,1] #Обрізка в заданих межах if not Start is None: if 0 < Start < x.max(): x, y = x[x >= Start], y[x >= Start] if not End is None: if 0 < End <= x.max(): x, y = x[x <= End], y[x <= End] n = int(N) EQ = sp.poly1d( sp.polyfit(x, y, m) ) poly_Y = EQ( x ) Range = range(0,len(x),n) i = 0 xnew, ynew = [], [] for j in list(Range[1:])+[len(x)-1]: if j-i <=1: break x_temp = x[i:j].mean() xnew.append(x_temp) ynew.append( (y[i:j] - poly_Y[i:j]).mean() + EQ(x_temp)) i = j if self.showTmp: return Array(sp.array([xnew, ynew]).T, Type = data.Type, scale = data.scale), x, y, poly_Y else: return Array(sp.array([xnew, ynew]).T, Type = data.Type, scale = data.scale)
def AutoB_splineS(self, state, isSignal = True, senderType = 0, param = 0.95): '''Штучний підбір коефіцієнтів для b-сплайн інтерполяції''' Dict = { 'cAutoB_splineS' : (0, 'cB_splineS', 'cB_splineStep', 'cB_splineK'), 'sAutoB_splineS' : (1, 'sB_splineS', 'sB_splineStep', 'sB_splineK'), 'rAutoB_splineS' : (2, 'rB_splineS', 'rB_splineStep', 'rB_splineK')} senderName = '' if isSignal: senderName = self.sender().objectName() else: Names = ['c', 's', 'r'] senderName = Names[senderType]+'AutoB_splineS' active = (Dict[senderName][0],) + self.findChilds(QtGui.QDoubleSpinBox,Dict[senderName][1:]) data = self.getData(active[0]) if state: active[1].setEnabled(False) y = data[:,1] x = data[:,0] EQ = sp.poly1d( sp.polyfit(x, y, 3) ) poly_Y = EQ( x ) Y = y - poly_Y Step = float(active[2].value()) K = float(active[3].value()) try: print(str((1+Step/K**3)*param)) active[1].setValue(sp.std(Y)**2*len(y)*(1+Step/K**2)*param) except: print("AutoB_splineS: SmoothParamError") else: active[1].setEnabled(True)
def fit_linear(self, head_frac=0.15, tail_frac=0.01, improvement_threshold=10, order=1, show_fit=True): """ Find minimum number of reads in background set. Here sorted array is iteratively sliced from the end with bins having high reads values till sliced array shows 'good' linearity """ prev_residual = None error_improvement = improvement_threshold+1 sliced_sorted_vals = self.sortedArray[int(head_frac*len(self.sortedArray)):] rem_bins = int(0.01*len(self.sortedArray)) while error_improvement > improvement_threshold: sliced_sorted_vals = sliced_sorted_vals[:-rem_bins] x_vals = [i for i in range(len(sliced_sorted_vals))] pf, residuals, rank, sv, rcond = polyfit(x_vals, sliced_sorted_vals, order, full=True) if prev_residual is not None: error_improvement = prev_residual - residuals[0] prev_residual = residuals[0] linearEq = poly1d(pf) x_vals = [i for i in range(len(sliced_sorted_vals))] pred_y_vals = linearEq(x_vals) if show_fit is True: plt.scatter(x_vals, sliced_sorted_vals, s=2, alpha=0.1, c='r', lw=0) plt.plot(x_vals, pred_y_vals) plt.show() self.linearMaxVal = pred_y_vals[-1] return True
def predict(A): if len(A) > min(3, n): xtrain = np.linspace(1,len(A),len(A)) ytrain = A xtest = len(A) + 1 f = poly1d(polyfit(xtrain, ytrain, n)) return f(xtest).round() if all(f(xtrain).round() == ytrain) else None
def fit_indices(self, indices): order = self.values['Order'] if order: if order == -1: import fitfun def fitf(arg, y0,y1,y2,x1): n=len(indices) x=arg ya = (y1-y0)/x1*x + y0 yb = (y2-y1)/(n-x1)*(x-x1)+y1 return ya*(x<x1)+yb*(x>=x1) xx = n.arange(len(indices)) oo=fitfun.Optimizer(xx, indices) oo.set_function(fitf) oo.set_parameter_range('y0', min(indices),max(indices),0) oo.set_parameter_range('y1', min(indices),max(indices),0) oo.set_parameter_range('y2', min(indices),max(indices),0) oo.set_parameter_range('x1', 2.0, len(indices)-2.,len(indices)/2.) oo.optimize() #print oo.solutions #print 'old',indices.tolist() indices=fitf(arg=xx, **oo.solutions).astype('int') #print 'new',indices.tolist() else: #print 'old i', indices.tolist() x = range(len(indices)) fit_f= poly1d( polyfit( x,indices, self.values['Order']) ) indices = fit_f(x).round().astype('int') #print 'new i', indices.tolist() else: pass return indices
def getPoly(self, x, y, deg): f = self.polyfy(x, y, deg) fp = sp.poly1d(f) error = self.error(fp, x, y) # logging.info(" error : %i --- function deg : %i -- function : %s ", error, deg, fp) return fp
def show(self, **to_plot): marker = ('+', 's', 'o', '', 'x') colors = ('c', 'g', 'k', 'k', 'r') names = ('lro', 'var', 'avg', 'fit', 'stokes') line_style = map(lambda x,y: x+y, colors, marker) styles = dict( zip(names, line_style) ) for key in to_plot: if not styles.has_key(key): raise ValueError, "show() can only be asked to display data for %s" % ', '.join(names) styles.update(to_plot) #styles looks like {'lro': 'c+', 'var': 'gs', 'stokes': 'rx', 'avg': 'ko', 'fit': 'k' } figure() # Plot x axis stiffness vs power subplot(211) lineX = plot(self.power, self.LRO_avg[:,0], styles['lro'], self.power, self.variance_k_avg[:,0], styles['var'], \ self.power, self.k_avg[:,0], styles['avg'], self.power, poly1d(self.kfitX)(self.power), styles['fit']) map( lines.Line2D.set_label, lineX, names[:-1] ) map( lines.Line2D.set_markersize, lineX, [10] * len(names[:-1]) ) legend(loc='upper left') xlabel('Power (arb units)') ylabel('X stiffness (pN/nm)') # Plot y axis stiffness vs power subplot(212) lineY = plot(self.power, self.LRO_avg[:,1], styles['lro'], self.power, self.variance_k_avg[:,1], styles['var'], \ self.power, self.k_avg[:,1], styles['avg'], self.power, poly1d(self.kfitY)(self.power), styles['fit'], \ self.power, self.stokes_avg, styles['stokes'] ) # apply line labels map( lines.Line2D.set_label, lineY, names ) # up marker size to 2 map( lines.Line2D.set_markersize, lineY, [10] * len(names) ) legend(loc='upper left') xlabel('Power (arb units)') ylabel('Y stiffness (pN/nm)') return lineY
def Train_Data(x_train,y_train,Iteration): fp=[] for i in range(1,Iteration): fp = sp.polyfit(x_train, y_train, Iteration, full=True) #f = sp.poly1d(fp) #fp2, res2, rank2, sv2, rcond2 = sp.polyfit(x_train, y_train, 2, full=True) f = sp.poly1d(fp[i-1]) return fp,f
def example1(): x = np.arange(10) y = 2*np.random.randn(10) + x**2 xs = np.linspace(-0.25, 9.25, 200) # lin = np.polyfit(x, y, 1) quad = np.polyfit(x, y, 2) many = np.polyfit(x, y, 9) # plt.scatter(x, y) plt.plot(xs, poly1d(lin)(xs)) plt.plot(xs, poly1d(quad)(xs)) plt.plot(xs, poly1d(many)(xs)) plt.ylabel('Y') plt.xlabel('X') plt.legend(['Underfit', 'Good fit', 'Overfit']) return
def trend_removal(x, y): p0 = scipy.polyfit(x, (y-x), 1) p = scipy.poly1d(p0) x = true_periods dy = p(x) y = mps z = y - dy return z
def plot_graph(x, y, d): fp = sp.polyfit(x, y, d) fd = sp.poly1d(fp) fx = sp.linspace(0, x[-1], 1000) plt.plot(fx, fd(fx), linewidth=2) print(error(fd,x,y),"\n") temp = "d = " + str(d) legend.append(temp) plt.legend(legend, loc = "upper left")
def calibrate(self,rtkit,outliers,surrogates,linregs,rsq_threshold): missingirt = [] for rawspectrum in self.spectra: rt_calibration = [] irt_calibration = [] print "RT peptides used per run:" print "run\tpeptide\trt\tirt" for peptide in self.rt[rawspectrum]: peptide_sequence = self.spectrum_block_map[rawspectrum][peptide].sequence if peptide_sequence in rtkit.keys(): if rawspectrum in outliers: if peptide_sequence not in outliers[rawspectrum]: rt_calibration.append(self.rt[rawspectrum][peptide]) irt_calibration.append(rtkit[peptide_sequence]) print rawspectrum + "\t" + peptide_sequence + "\t" + str(self.rt[rawspectrum][peptide]) + "\t" + str(rtkit[peptide_sequence]) else: rt_calibration.append(self.rt[rawspectrum][peptide]) irt_calibration.append(rtkit[peptide_sequence]) print rawspectrum + "\t" + peptide_sequence + "\t" + str(self.rt[rawspectrum][peptide]) + "\t" + str(rtkit[peptide_sequence]) if len(rt_calibration) < 2: missingirt.append(rawspectrum) if len(rt_calibration) >= 2: if rawspectrum in linregs.keys(): print "Replacing iRT normalization of run " + rawspectrum + " with c: " + str(linregs[rawspectrum]['b']) + " m: " + str(linregs[rawspectrum]['a']) + "." self.a[rawspectrum] = linregs[rawspectrum]['a'] self.b[rawspectrum] = linregs[rawspectrum]['b'] self.rsq[rawspectrum] = 1.0 else: (self.a[rawspectrum],self.b[rawspectrum]) = scipy.polyfit(rt_calibration,irt_calibration,1) slope, intercept, r_value, p_value, std_err = stats.linregress(rt_calibration,irt_calibration) self.rsq[rawspectrum] = r_value**2 plt.figure() plt.title(rawspectrum + " (c: " + str(self.b[rawspectrum]) + ", m: " + str(self.a[rawspectrum]) + ")") fit_fn = scipy.poly1d((self.a[rawspectrum],self.b[rawspectrum])) plt.plot(rt_calibration,irt_calibration, 'b.',rt_calibration,fit_fn(rt_calibration),'-r') plt.savefig(rawspectrum+'.png') for host in surrogates.keys(): print "Replacing iRT normalization of run " + host + " with " + surrogates[host] + "." self.a[host] = self.a[surrogates[host]] self.b[host] = self.b[surrogates[host]] self.rsq[host] = self.rsq[surrogates[host]] missingirt = filter (lambda a: a != host, missingirt) if len(missingirt) > 0: print "Did you search for the true sequences?" print "Did you use a non-consensus and without best replicates summarization spectral library?" print "Did you add the Biognosys RT-kit to all of your samples?" print "The following runs don't contain peptides from the Biognosys RT-kit:",missingirt raise Exception("Error: At least one of your runs doesn't contain any peptides from the Biognosys RT-kit!") for rawspectrum in self.spectra: if self.rsq[rawspectrum] < rsq_threshold: raise Exception("Error: R-squared " + str(self.rsq[rawspectrum]) + " of run " + rawspectrum + " is below the threshold of " + str(rsq_threshold) + ".")
def get_1d_approx_func(self, x, y, dim=1): """ データ(x, y)の近似関数を算出する。 x: fと比較するデータの1カラム目 y: fと比較するデータの2カラム目 """ fp1, residuals, rank, sv, rcond = sp.polyfit(x, y, dim, full=True) return sp.poly1d(fp1)
def random_test(x,y,deg): #訓練用データとテスト用データにランダムに分ける frac = 0.7 #訓練データの割合 split_idx = int(frac * x.size) shuffled = sp.random.permutation(list(range(len(x)))) train = sorted(shuffled[:split_idx]) test = sorted(shuffled[split_idx:]) function = sp.poly1d(sp.polyfit(x[train],y[train],deg)) #print(function) print("f%i :error_value=%f" % (deg,error_value(function,x[test],y[test])))
def perform_poly_reg(x, y): x = np.asarray(x) y = np.asarray(y) fp4, residuals, rank, sv, rcond = sp.polyfit(x, y, 4, full=True) print("Model parameters: %s" % fp4) print residuals f4 = sp.poly1d(fp4) print(error(f4, x, y)) fx = sp.linspace(0, x[-1]) plt.plot(fx, f4(fx), linewidth=4) plt.legend(['d=%i' % f4.order], loc="upper left")
def approximation(x, y): d = 1 # степень полинома fp, residuals, rank, sv, rcond = sp.polyfit(x, y, d, full=True) # Модель f = sp.poly1d(fp) # аппроксимирующая функция # print('Коэффициент -- a %s '%round(fp[0],4)) # print('Коэффициент-- b %s '%round(fp[1],4)) # print('Коэффициент -- c %s '%round(fp[2],4)) # y1=[fp[0]*x[i]**2+fp[1]*x[i]+fp[2] for i in range(0,len(x))] # значения функции a*x**2+b*x+c # so=round(sum([abs(y[i]-y1[i]) for i in range(0,len(x))])/(len(x)*sum(y))*100,4) # средняя ошибка # print('Average quadratic deviation '+str(so)) fx = sp.linspace(x[0], x[-1] + 1, len(x)) # можно установить вместо len(x) большее число для интерполяции plt.plot(x, y, 'o', markersize=10) plt.plot(fx, f(fx), linewidth=2) plt.grid(True) plt.show()
def draw_graph(x, y, plt, colors): """ Рисование графика функции. """ print(x) # polyfit подбирает коэффициенты модели f2p, residuals, rank, sv, rcond = numpy.polyfit(x, y, 2, full=True) f2 = sp.poly1d(f2p) fx = numpy.linspace(0, x[-1], 500) plt.plot(fx, f2(fx), linewidth=2.0, color=colors) plt.scatter(x, y, s=40, c=colors) plt.autoscale(tight=True) return plt.grid(True, linestyle='-', color='0.75')
def rSquare(self, x, y, coeffs): results = {} # coefficients results['(sl,int)'] = coeffs.tolist() # r-squared p = scipy.poly1d(coeffs) # fit values, and mean yhat = [p(z) for z in x] ybar = sum(y) / len(y) ssreg = sum([(yihat - ybar)**2 for yihat in yhat]) sstot = sum([(yi - ybar)**2 for yi in y]) results['R^2'] = ssreg / sstot print "###################################################" print "(sl,int):", results['(sl,int)'] print "r^2:", results['R^2'] return results
def alt_norm(f, _, **kargs): """Just do the normalisation per file and report run number and normalisation constants""" run = f["run"] signal = kargs["signal"] lfit = kargs["lfit"] rfit = kargs["rfit"] ec = 0 md = f.find_col(signal) coeffs = f.polyfit(ec, md, 1, lambda x, y: lfit[0] <= x <= lfit[1]) linearfit = scipy.poly1d(coeffs) f.add_column(lambda r: r[md] - linearfit(r[ec]), "minus linear") highend = f.mean("minus", lambda r: rfit[0] <= r[ec] <= rfit[1]) ret = [run] ret.extend(coeffs) ret.append(highend) return ret
def huatu(index, datax, datay, degree, labelx, labely): plt.subplot(index) plt.scatter(datax, datay) plt.autoscale(tight=True) plt.grid() fp1 = sp.polyfit(datax, datay, degree) print fp1 #aa = polyval(fp1, 5) #print aa fStraight = sp.poly1d(fp1) print "Error of Curve3 line:", error(fStraight, datax, datay) #draw fitting straight line fx = sp.linspace(0, datax[-1], 10) # generate X-values for plotting plt.plot(fx, fStraight(fx), linewidth=4) #plt.legend(["d=%i" % fStraight.order], loc="upper left") plt.xlabel(labelx) plt.ylabel(labely)
def diffTaylor(v, i): coeffs = polyfit(v, i, 3) vFit = linspace(min(v), max(v), 100) iFit = polyval(coeffs, vFit) polynomial = poly1d(coeffs) # Second order didv = lambda ix: 3 * coeffs[0] * ( (ix)**2) + 2 * coeffs[1] * (ix) + coeffs[2] # Fifth order #didv = lambda ix: # 6*coeffs[0]*((ix)**5) + 5*coeffs[1]*((ix)**4) + 4*coeffs[2]*((ix)**3) + # 3*coeffs[3]*((ix)**2) + 2*coeffs[4]*(ix) + coeffs[5] rFit = divide(1, [float(didv(val)) for val in vFit]) return vFit, iFit, rFit
def example(): f = sp.polyfit([0, 1, 2], [-5, -4, 3], 3) p = sp.poly1d(f) x = range(5, 100) plt.figure(figsize=(14, 6)) plt.plot(x, p(x) + 50000, lw=2, ls='--', color='green') plt.plot(x, -p(x) - 50000, lw=2, ls='--', color='red') plt.xticks([]) plt.yticks([]) plt.gca().invert_xaxis() plt.axhline(0, ls='-', alpha=.2) plt.title(u'均值回归概念', fontsize=15) plt.ylabel(u'市场收益', fontsize=15) plt.xlabel(u'时间', fontsize=15) plt.text(70, 400000, u'高收益具有\n向下回归趋势', color='green', fontsize=14) plt.text(70, -500000, u'低收益具有\n向上回归趋势', color='red', fontsize=14) plt.show()
def main(): df = pd.read_csv('greenland-mass-change.csv') all_xs = df['year'] all_ys = df['mass change'] train = df[df['year'] < 2012] test = df[df['year'] > 2012] train_xs = train['year'] train_ys = train['mass change'] test_xs = test['year'] spline = interpolate.UnivariateSpline(all_xs, all_ys) # Found 3 experimentally. See climate_change_analysis_polynomial_fit.py coefficients = np.polyfit(train_xs, train_ys, 3) trend_fn = poly1d(coefficients) earliest = min(train['year']) latest = max(train['year']) seasonal_fluctuations = [] for n, t in enumerate(test_xs): start = (t - int(t)) + int(earliest) if start < earliest: start = start + 1 end = (t - int(t)) + int(latest) if end > latest: end = end - 1 steps = [(s + start) for s in range(int(end - start))] spline_samples = [spline(s) for s in steps] relative_trend = trend_fn(steps) samples = [s - r for s, r in zip(spline_samples, relative_trend)] seasonal_fluctuations.append((samples, steps)) for n, (f, s) in enumerate(seasonal_fluctuations): plt.plot(s, f) plt.title('Greenland mass change') plt.xlabel('Time') plt.ylabel('Mass') plt.grid(True) plt.show() plt.savefig('greenland-mass-change.png')
def cg(trainnum,times,lamata=1): createdata(trainnum,times) producenoise() #初始生成w for i in range(tail): W.append(0.1); for l in range(lamata): #迭代代价差小于设定时退出 while True: cost_first=0 for i in range(tail): cost_first+=l*(W[i]**2) for j in range(trainnum): temp=0 for q in range(tail): temp+=W[q]*(X[j]**q) if q==0: temp-=Y[j] cost_first+=temp**2/(2*trainnum) for i in range(tail): #求导数 t=2*l*W[i] for j in range(trainnum): temp=0 for q in range(tail): temp+=W[q]*(X[j]**q) if q==0: temp-=Y[j] t+=temp*i*W[i]*X[j]**(i-1)/trainnum W[i]=W[i]-step*t cost_second=0 for i in range(tail): cost_second+=l*(W[i]**2) for j in range(trainnum): temp=0 for q in range(tail): temp+=W[q]*(X[j]**q) if q==0: temp-=Y[j] cost_second+=temp**2/(2*trainnum) if cost_first-cost_second<10e-5: break f = sp.poly1d(W) fx=sp.linspace(-1,1,100)
def entropy_drift_analysis(sigma=2, color='b', color_p='g'): """why is convergence so difficult to obtain for, say, sigma = 2? Explore selection/mutation balance.""" n = 16 L = 16 matrix = sample_matrix(L, sigma) ringer = ringer_motif(matrix, n) mutants = [ iterate(mutate_motif, ringer, i) for i in trange(256) for j in range(10) ] dists = [ motif_hamming_distance(ringer, mutant) for mutant in tqdm(mutants) ] fs = [log_fitness(matrix, mutant, G) for mutant in tqdm(mutants)] fps = [] trials = 100 for mutant in tqdm(mutants): nexts = [] f = log_fitness(matrix, mutant, G) for i in range(trials): mutant_p = mutate_motif(mutant) fp = log_fitness(matrix, mutant_p, G) if log(random.random()) < fp - f: nexts.append(fp) else: nexts.append(f) fps.append(mean(nexts)) plt.subplot(3, 1, 1) plt.scatter(dists, fs, color=color, marker='.') plt.scatter(dists, fps, color=color_p, marker='.') #plt.semilogy() plt.subplot(3, 1, 2) plt.scatter(dists, [(f - fp) / f for (f, fp) in zip(fs, fps)], color=color, marker='.') plt.plot([0, len(fs)], [0, 0], linestyle='--', color='black') plt.subplot(3, 1, 3) diffs = [fp - f for f, fp in zip(fs, fps)] plt.scatter(fs, diffs, marker='.', color=color) interpolant = poly1d(polyfit(fs, diffs, 1)) plt.plot(*pl(interpolant, [min(fs), max(fs)])) plt.plot([min(fs), max(fs)], [0, 0], linestyle='--', color='black') minx, maxx = min(fs + fs), max(fs + fps)
def plot_fitting(fig): ax = fig.add_subplot(111) ax.plot(n_2D, potential_tot, "s", label="MD Data") power_matrix = numpy.vstack((n_2D**4, n_2D**3, n_2D**2, n_2D, numpy.ones_like(n_2D))).T degs = [2, 3, 4] n_2D_plot = numpy.linspace(-5, 5, 100) for deg in degs: # param_fit = scipy.polyfit(n_2D, potential_tot, deg) param_fit, _, _, _ = numpy.linalg.lstsq(power_matrix[:, 4-deg:-1], potential_tot) print(deg, param_fit) poly_f = scipy.poly1d(numpy.hstack((param_fit, 0))) fit_data = poly_f(n_2D_plot) label_axis = "$" + "+".join(["{0:.3f}x^{1}".format(param_fit[i], deg-i) for i in range(deg)]) + "$" ax.plot(n_2D_plot, fit_data, label=label_axis) ax.set_xlim(-4, 4) ax.set_xlabel(r"$\sigma_{\mathrm{2D}}$ ($10^{13}$ $e\cdot$cm$^{-2}$)") ax.set_ylabel(r"$\Delta \Phi$ (mJ$\cdot$m$^{-2}$)") ax.legend(loc=0) fig.tight_layout()
def Plot(n, x, y): import scipy as sp import matplotlib.pyplot as plt #assuming data is per hour data = sp.genfromtxt("web_traffic.tsv", delimiter="\t") x = x[~sp.isnan(y)] y = y[~sp.isnan(y)] plt.scatter(x, y, marker='+', color='g') plt.title("Web traffic") plt.xlabel("time") plt.ylabel("hits/hr") plt.xticks([w * 7 * 24 for w in range(10)], ['week %i' % w for w in range(10)]) fpn = sp.polyfit(x, y, n) fn = sp.poly1d(fpn) fx = sp.linspace(0, x[-1], 1000) #generate x values for plotting plt.plot(fx, fn(fx), color='k', linewidth=3) plt.legend(["d=%i" % fn.order], loc="upper center") plt.grid() plt.show()
def plot_models(): plt.scatter(x, y, c='r', s=10) plt.xlabel('Time/Hours') plt.ylabel('Requests') plt.title('Number of requests per hour') plt.xticks([w * 7 * 24 for w in range(10)], ['week %i' % w for w in range(10)]) plt.grid(True) plt.autoscale(tight=True) fx = sp.linspace(0, x[-1], 1000) legends = [] errors = [] for i in [1, 2, 3, 10, 100]: f = sp.poly1d(sp.polyfit(x, y, i)) plt.plot(fx, f(fx), linewidth=2) errors.append(error(f, x, y)) legends.append(f.order) plt.legend(["d=%i" % o for o in legends], loc='upper left') return errors
def calculate(number, b): result = scipy.poly1d([0.0]) for i in range(500, 10100, 500): if i >= number & i < 9500: if b == True: aux = int((i / 500) - 1) result = polinomiosLatitud[aux] break else: aux = int((i / 500) - 1) result = polinomiosLongitud[aux] break if i >= 9500: if b == True: result = polinomiosLatitud[18] break else: result = polinomiosLongitud[18] break return result(number)
def _calc_blc_wedge(deg=4, wavelength=2.1e-6): """ This function determines the desired sigma coefficients required to achieve a wedge from 2 to 6 lam/D. It returns the coefficients of a polynomial fit that maps from nlambda/D to sigma. """ import scipy r = np.linspace(2, 6, 161) difflim = wavelen / 6.5 * 180. * 60 * 60 / np.pi sigs = [_width_blc(difflim * ri) for ri in r] pcs = scipy.polyfit(r, sigs, deg) p = scipy.poly1d(pcs) plt.plot(r, sigs, 'b') plt.plot(r, p(r), "r--") diffs = (sigs - p(r)) print("Poly fit:" + repr(pcs)) print(" fit rms: " + str(diffs.std()))
def polys_coeffs_check(self, spline): polys = [poly1d(coeffs) for coeffs in spline.coeffs] for poly, (qi, qf), (vi, vf), (ai, af), (ti, tf) in \ zip(polys, pairwise(spline.q), pairwise(spline.v), pairwise(spline.a), pairwise(spline.t)): self.assertAlmostEqual(poly(ti), qi, self.PLACES) self.assertAlmostEqual(poly.deriv(1)(ti), vi, self.PLACES) self.assertAlmostEqual(poly.deriv(2)(ti), ai, self.PLACES) self.assertAlmostEqual(poly(tf), qf, self.PLACES) self.assertAlmostEqual(poly.deriv(1)(tf), vf, self.PLACES) self.assertAlmostEqual(poly.deriv(2)(tf), af, self.PLACES) # Check that poly's are continuous at boundaries. for (poly_a, poly_b), t in zip(pairwise(polys), spline.t[1:-1]): self.assertAlmostEqual(poly_a(t), poly_b(t), self.PLACES) # pos self.assertAlmostEqual( poly_a.deriv(1)(t), poly_b.deriv(1)(t), self.PLACES) # vel self.assertAlmostEqual( poly_a.deriv(2)(t), poly_b.deriv(2)(t), self.PLACES) # accel
def fit_lanes(image, points, angle_low_thresh, angle_high_thresh, color=(0, 0, 255), thickness=1): if len(points) <= 0: return x = [p[0] for p in points] y = [p[1] for p in points] # print("Num utils: X:" + str(x)) # print("Num utils: Y:" + str(y)) f = sp.poly1d(sp.polyfit(x, y, 1)) start = (x[0], int(f(x[0]))) end = (x[-1], int(f(x[-1]))) # print("Num utils: start:" + str(start)) # print("Num utils: end:" + str(end)) angle = NumUtils.get_angle(start, end) if angle_low_thresh <= angle <= angle_high_thresh: cv2.line(image, start, end, color, thickness)
def onclick(event): global X, Y, w1 def xx(): x = sp.sort(X) for i, s in enumerate(list(sp.asarray(X).argsort())): if x[i] == x[i - 1]: return X.remove(X[s]), Y.remove(Y[s]), xx() if event.button == 1 and event.inaxes: X.append(event.xdata) Y.append(event.ydata) w1.set_data(X, Y) fig.canvas.draw() plt.show() else: plt.disconnect(cid) xx() nx = len(X) x = sp.poly1d([1, 0]) L = 0 for i in sp.arange(nx): pom = 1 for j in sp.hstack((sp.arange(i), sp.arange(i + 1, nx))): pom *= (x - X[j]) / (X[i] - X[j]) L += pom * Y[i] print( "\nWielomian lagrange interpolujacy {} punkt ma postac:\n\tx\ty\n {}\n nL:\n {} " .format(nx, sp.stack((X, Y), axis=1), L)) xw = sp.linspace(min(X), max(X), 1000) yw = L(xw) w2.set_data(xw, yw) ax.set_title("lagrange interpolacja {} pktow".format(nx)) ax.set_xlabel('x') ax.set_ylabel('y') ax.axis('equal') ax.legend() fig.canvas.draw()
def modeling1(): x, y = loaddata() #polyorder = sp.polyfit(x, y, 1) polyorder, residuals, _, _, _ = sp.polyfit(x, y, 1, full=True) #print('반환된 차수 {}'.format(polyorder)) print('잔차 {}'.format(residuals)) model1 = sp.poly1d(polyorder) #print(model1) l = np.linspace(x.min(), x.max(),1000) plt.plot(l, model1(l)) plt.scatter(x, y) plt.xlabel('x') plt.ylabel('y') plt.autoscale(tight=True) plt.grid() plt.show() print('1.542->{}, 3.5->{}'.format(model1(1.542), model1(3.5))) print('오차 {:,}'.format(error(model1, x, y))) x = np.array([[n] for n in x])
def modeling3(): x, y = loaddata() # change data y[x<-0.5] -= 200 y[np.logical_and(x>1, x<1.5)] += 50 y[x>1.5] += 550 # change order order_number = 6 l = np.linspace(x.min(), x.max(), 1000) fs = [sp.poly1d(sp.polyfit(x, y, n)) for n in range(1, order_number+1)] for n in range(0, order_number): plt.plot(l, fs[n](l), label=str(n+1)) print('{} 차수의 오차 -> {:,}'.format(n+1, error(fs[n], x, y))) plt.scatter(x, y) plt.xlabel('x') plt.ylabel('y') plt.autoscale(tight=True) plt.legend(loc='upper left') plt.grid() plt.show()
def plot2D(self): x = self.x y = self.y state_means = self.state_means cm = plt.get_cmap('jet') colors = np.linspace(0.1, 1, len(x)) # Plot data points using colormap sc = plt.scatter(x, y, s=30, c=colors, cmap=cm, edgecolor='k', alpha=0.7) cb = plt.colorbar(sc) cb.ax.set_yticklabels([str(p.date()) for p in x[::len(x) // 9].index]) # Plot every fifth line step = 100 xi = np.linspace(x.min() - 5, x.max() + 5, 2) colors_l = np.linspace(0.1, 1, len(state_means[::step])) for i, beta in enumerate(state_means[::step]): plt.plot(xi, beta[0] * xi + beta[1], alpha=.2, lw=1, c=cm(colors_l[i])) # Plot the OLS regression line plt.plot(xi, poly1d(np.polyfit(x, y, 1))(xi), '0.4') plt.title(self.dependent + ' vs. ' + self.independent) plt.show()
def show(self): """Show the plot.""" for x, y, color, legend, marker in zip(self.xs, self.ys, self.colors, self.legends, self.dots): f2p = sp.polyfit(x, y, 1) f2 = sp.poly1d(f2p) fx = sp.linspace(0, x[-1], 1000) plt.figure(figsize=(12, 8), num=self.figureName) plt.plot(fx, f2(fx), linewidth=int(self.showLine), color=color, label=legend) plt.title(self.title) plt.xlabel(self.xlabel) plt.ylabel(self.ylabel) plt.scatter(x, y, color=color, marker=marker) plt.legend(loc='lower right') plt.grid(True, linestyle='-', color='0.75') plt.show()
def Hurwitzpoly_g(h, k, verbose=False): '''Inputs: k is the order of f(p)=p**k h is a poly1d object ''' #step 1: generate the even polynomial H(-p**2)=h(p)h(-p) hminus = poly1d([(-1)**(i) * coeff for i, coeff in enumerate(h.coeffs[::-1])][::-1]) H = h * hminus if verbose == True: print "H=", H #Step 2: Generate G(-p**2)=H(-p**2)+(-1)**k vec = zeros(len(H.coeffs)) vec[-(2 * k + 1)] = (-1)**k G = H + poly1d(vec) if verbose == True: print "G=", G #Generate the vector X and compute the root of G(-p**2) G2 = poly1d((G.coeffs[::-2])[::-1]) X = poly1d([(-1)**(i) * coeff for i, coeff in enumerate(G2.coeffs[::-1])][::-1]) #Compute the RHP and LHP roots of G(-p**2) Xr = X.roots pr = sqrt(-Xr) if verbose == True: print "pr=", pr prm = -pr #Step 4:Generate the monic polynomial g'(p) C = poly1d(prm, True) #Step 5: Generate Scattering Hurwitz polynomial g(p) Cof = sqrt(abs(G.coeffs[0])) if verbose == True: print "Cof=", Cof g = poly1d(C * Cof) return (g)
plt.scatter(x, y) plt.title("Web traffic over the last month") plt.xlabel("Time") plt.ylabel("Hits/hour") plt.xticks([w * 7 * 24 for w in range(10)], ['week %i' % w for w in range(10)]) plt.autoscale(tight=True) plt.grid() fp1, residuals, rank, sv, rcond = sp.polyfit(x, y, 1, full=True) #fp1,residuals,rank,sv,rcond #(array([2.59619213, 989.02487106]),array([3.17389767e+08]),2,array([1.36699071,0.36240365]), 1.6320278461989801e-13) print("Model parameters: %s" % fp1) print(residuals) #Approximate error f1 = sp.poly1d(fp1) # 2.596 x + 989 print(error(f1, x, y)) fx = sp.linspace( 0, x[-1], 1000 ) # generate X-values for plotting. 1000 is for having 1000 values from 0-743 plt.plot(fx, f1(fx), linewidth=4) plt.legend(["d=%i" % f1.order], loc="upper left") f2p = sp.polyfit(x, y, 2) print(f2p) f2 = sp.poly1d(f2p) print(error(f2, x, y)) fx = sp.linspace( 0, x[-1], 1000
assert array_equal(la.norm(A, sp.inf), max(sp.sum(A, axis=1))) # norm 1 == max column sum: assert array_equal(la.norm(A, 1), max(sp.sum(A, axis=0))) # norm -1 == min column sum: assert array_equal(la.norm(A, -1), min(sp.sum(A, axis=0))) if '## polynomials': # 1x^2 + 2x + 3 p = sp.poly1d([1, 2, 3]) if '## get info': assert sp.array_equal(p.coeffs, sp.array([1, 2, 3])) assert sp.array_equal(p.order, 2) # Evaluate: assert array_equal(p([1, 2]), sp.array([6, 11])) # Roots: assert array_equal(p(p.r), sp.zeros(p.order))
def cardcons(x): templist = [-x[i] for i in range(len(x))] poly = poly1d(templist, True) return poly.c
def fit_power_parameters(bookmark, large): f''' # Fit Power Parameters to {bookmark} This notebook allows you to calculate power parameters (CdA - air resistance, Crr - rolling resistance) from bookmarked activities. Beforehand you should have generated the bookmark by running > python -m ch2.data.coasting or similar to identify sections of activities with little pedalling. See that module for more information. The `large` parameter means that each bookmark is taken in its entirety. The alternative is that they are divided into small sub-samples reflecting the data sample rate. ''' ''' $contents ''' ''' ## Load Data Open a connection to the database and load the data we require. ''' s = session('-v 5') large = strtobool(large) route = Statistics(s, bookmarks=bookmarks(s, bookmark)). \ by_name(ActivityReader, N.LATITUDE, N.LONGITUDE, N.SPHERICAL_MERCATOR_X, N.SPHERICAL_MERCATOR_Y, N.DISTANCE, N.ELEVATION, N.SPEED, N.CADENCE) route.sort_index(inplace=True) # bookmarks are not sorted by time if large: route, max_gap = bookend(route), None else: max_gap = 10 route = add_differentials(route, max_gap=max_gap) if large: route = route.iloc[1::2] route.describe() ''' ## Add Energy Calculations ''' weight = 64 + 12 # weight of rider + bike / kg todo - extract weight from db route = add_energy_budget(route, weight) route = add_cda_estimate(route) route = add_crr_estimate(route, weight) route.describe() ''' ## Plot Constraints The calculations above added an energy budget for each "step" in the data. These included values for CdA and Crr that would "explain" the decrease in energy (taking each alone - so the CdA is that required for all energy lost to air resistance, the Crr is that required for all energy lost to rolling resistance). But we know that both CdA and Crr could be important. So what we want is a linear combination of the two. For example, maybe the energy loss is 90% due to CdA and 10% due to Crr. All these possible linear combinations lie on a line that joins 100% CdA and 0% Crr with 0% CdA and 100% Crr. So the plot below shows all possible combinations of CdA and Crr. And what we are looking for is the most common value. So we want to know where the plot is darkest / the lines are most dense. ''' output_notebook() f = figure(plot_width=500, plot_height=400) clean = route.loc[route[N.DELTA_ENERGY] < 0].dropna() cs = pd.DataFrame({ N.CDA: [(0, cda) for cda in clean[N.CDA]], N.CRR: [(crr, 0) for crr in clean[N.CRR]] }) f.multi_line(xs=N.CDA, ys=N.CRR, source=cs, line_alpha=0.1, line_color='black') f.xaxis.axis_label = 'CdA' f.yaxis.axis_label = 'Crr' show(f) ''' ## CdA Only If we ignore Crr then we can estimate CdA by looking at the relative number of constraints of CdA where Crr is zero. We do this by fitting to binned data. The peak in the fit(s) gives the value of CdA if Crr is unimportant. ''' bins = np.linspace(0, 1.5, 30) width = bins[1] - bins[0] counts = clean[N.CDA].groupby(pd.cut(clean[N.CDA], bins)).size() print(counts.describe()) cda = pd.DataFrame({ N.CDA: 0.5 * (bins[:-1] + bins[1:]), 'n': counts.values }) f = figure(plot_width=900, plot_height=300) f.quad(top=counts, left=bins[:-1] + 0.1 * width, right=bins[1:] - 0.1 * width, bottom=0) for order in range(2, 20, 2): coeff = sp.polyfit(cda[N.CDA], cda['n'], order) p = sp.poly1d(coeff) print(order, fmin(lambda x: -p(x), 0.6, disp=0)[0]) f.line(x=cda[N.CDA], y=p(cda[N.CDA]), line_color='orange') show(f) ''' ## Sample Constraints If we want to include Crr then we need to find a way to measure the "peak" in the messy line plot above. To do this we convert to a collection of points and then fit a 2D density function. Conversion to points is done by selecting points at random on each line. (You might think that shorter lines should generate less points. The way I see it, each line is an observation that constrains CdA and Crr. Each observation has equal weight, so each line generates a point.) Random points avoids any systematic patterns from uniform sampling and allows re-runs to give some idea of noise. ''' def sample(): clean.loc[:, 'random'] = np.random.random(size=len(clean)) clean.loc[:, 'x'] = clean[N.CDA] * clean['random'] clean.loc[:, 'y'] = clean[N.CRR] * (1 - clean['random']) return clean.loc[:, ['x', 'y']] s = pd.concat([sample() for _ in range(100 if large else 10)]) print(s.describe()) f = figure(plot_width=600, plot_height=600) f.scatter(x='x', y='y', source=s) show(f) ''' ## Smooth, Find Maximum We generate and plot a Gaussian kernel density estimate. See https://towardsdatascience.com/simple-example-of-2d-density-plots-in-python-83b83b934f67 You may want to play around with bandwidth by supplying a second argument to gaussian_kde. See https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gaussian_kde.html ''' kernel = sp.stats.gaussian_kde(s.transpose()) xmin, xmax = 0, 1 ymin, ymax = 0, 0.02 xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j] xy = np.vstack([xx.ravel(), yy.ravel()]) smooth = np.reshape(kernel(xy), xx.shape) fig = plt.figure(figsize=(8, 8)) ax = fig.gca() ax.set_xlim(xmin, xmax) ax.set_ylim(ymin, ymax) ax.contourf(xx, yy, smooth, cmap='coolwarm') cset = ax.contour(xx, yy, smooth, colors='k') ax.clabel(cset, inline=1, fontsize=10) ax.set_xlabel('CdA') ax.set_ylabel('Crr') plt.title('2D Gaussian Kernel density estimation') '''
def seasonal_curve(train_xs, train_ys): coefficients = np.polyfit(train_xs, train_ys, 1) return poly1d(coefficients)
def trend_curve(train_xs, train_ys): # Found 3 experimentally. See climate_change_analysis_polynomial_fit.py coefficients = np.polyfit(train_xs, train_ys, 3) return poly1d(coefficients)
plt.show() def error(f, x, y): return sp.sum((f(x) - y)**2) data = sp.genfromtxt( '../BuildingMachineLearningSystemsWithPython/ch01/data/web_traffic.tsv', delimiter='\t') x = data[:, 0] y = data[:, 1] x = x[~sp.isnan(y)] y = y[~sp.isnan(y)] show_models(x, y) f1 = sp.poly1d(sp.polyfit(x, y, 1)) f2 = sp.poly1d(sp.polyfit(x, y, 2)) f3 = sp.poly1d(sp.polyfit(x, y, 3)) f4 = sp.poly1d(sp.polyfit(x, y, 4)) f5 = sp.poly1d(sp.polyfit(x, y, 5)) f10 = sp.poly1d(sp.polyfit(x, y, 10)) f100 = sp.poly1d(sp.polyfit(x, y, 100)) print u'Ошибка на всем диапазоне' for f in [f1, f2, f3, f4, f5, f10, f100]: models = [] print u'Error d=%2d: %20f' % (f.order, error(f, x, y)) models.append(f) show_models(x, y, models) inflection = 3.5 * 7 * 24 xa = x[:inflection] ya = y[:inflection]