def natural_cubic_spline_fit_reg(x, y, dof=5, alpha=0.01): if not np.any(np.isnan([x, y])): basis = cr(x, df=dof, constraints="center") model = Ridge(alpha=alpha).fit(basis, y) return model.predict(basis) else: # Insurance against NAN # Solution: Fit only non-nan pairs, leave rest as NAN print("Warning, have NAN") iNanX = np.isnan(x) iNanY = np.isnan(y) iNan = np.logical_or(iNanX, iNanY) yRez = np.copy(y) yRez[iNan] = np.nan yRez[~iNan] = natural_cubic_spline_fit_reg(x[~iNan], y[~iNan], dof=dof, alpha=alpha) return yRez
### There is a chance that a polygon is repeated twice? ### X = curr_field['doy'] y = curr_field['EVI'] ############################################# ### ### Smoothen ### ############################################# # # Spline # x_basis = cr( X, df=freedom_df, constraints='center' ) # Generate spline basis with "freedom_df" degrees of freedom model = LinearRegression().fit(x_basis, y) # Fit model to the data spline_pred = model.predict(x_basis) # Get estimates # # savitzky # # differences are minor, but lets keep using Pythons function # my_savitzky_pred = rc.savitzky_golay(y, window_size=Sav_win_size, order=sav_order) savitzky_pred = scipy.signal.savgol_filter(y, window_length=Sav_win_size, polyorder=sav_order)
### ### There is a chance that a polygon is repeated twice? ### X = a_field['doy'] y = a_field['NDVI'] freedom_df = 7 ############################################# ### ### Smoothen ### ############################################# # Generate spline basis with "freedom_df" degrees of freedom x_basis = cr(X, df=freedom_df, constraints='center') # Fit model to the data model = LinearRegression().fit(x_basis, y) # Get estimates y_hat = model.predict(x_basis) ############################################# ### ### find peaks ### ############################################# # peaks_LWLS_1 = peakdetect(LWLS_1[:, 1], lookahead = 10, delta=0) # max_peaks = peaks_LWLS_1[0] # peaks_LWLS_1 = form_xs_ys_from_peakdetect(max_peak_list = max_peaks, doy_vect=X)
def powerlaw_random(x, gamma): return np.power(x, gamma) + 0.0001 * np.random.normal(0, 1, len(x)) plt.close('all') x = np.linspace(0, 0.5, 100) y = powerlaw(x, 2 / 3) y_noise = powerlaw_random(x, 2 / 3) param, pcov = curve_fit(powerlaw, x, y_noise) plt.figure() plt.plot(x, y, 'k') plt.scatter(x, y_noise, color='r', alpha=0.5) n_obs = len(x) df = 10 # Generate spline basis with 10 degrees of freedom x_basis = cr(x, df=df, constraints='center') # Fit model to the data model = LinearRegression().fit(x_basis, y_noise) # Get estimates y_hat = model.predict(x_basis) # plt.scatter(x, y) plt.plot(x, y_hat, 'r') plt.title(f'Natural cubic spline with {df} degrees of freedom')
for i in range(0, len(header), 1): if header[i] != 'DIS': print(header[i]) xx = data_dictionary[header[i]][:, 0] # print(x) index = np.intersect1d(DIS_array[:, 0], xx, return_indices=True) yy = data_dictionary[header[i]][:, 1] - DIS_array[index[1], 1] loc = np.where(np.abs(yy) > 1e-10)[0] x = xx[loc] y = data_dictionary[header[i]][loc, 1] df = len(x) - 1 # Generate spline basis with 10 degrees of freedom x_basis = cr(x, df=df, constraints='center') # Fit model to the data model = RANSACRegressor().fit(x_basis, y) # Get estimates y_hat = model.predict(x_basis) # plt.scatter(x, y) # plt.plot(array_sorted[:,0], y_hat, c[i]) xplot = np.linspace(np.min(x), np.max(x), 1000) x_basis_large = cr(xplot, df=df, constraints='center') y_hat = model.predict(x_basis_large) cs = CubicSpline(x, y) yplot = cs(xplot) # # Compare estimated coefficients