def test_valid(self): self.assertTrue( Polynomial('1 1').approx_equal(interpolate([(1, 1), (2, 2)]))) self.assertTrue( Polynomial('2 1').approx_equal( interpolate([(-1, 1), (0, 0), (1, 1)]))) self.assertTrue( Polynomial('3 1').approx_equal( interpolate([(-1, -1), (0, 0), (1, 1), (2, 8)]))) self.assertTrue( Polynomial('3 0.0154 2 -0.8939 1 7.1818 0 -8.9092').approx_equal( interpolate([(-1, -17), (3, 5), (2, 2), (50, 8)]), abs_tol=1e-3))
range_start = 2 range_end = 10 step_size = (range_end - range_start) / n # X and Y values calculation using chebyshev polynomial x_range_cheb = chebyshev_range(n, range_start, range_end) y_range_cheb = [fn_to_eval(x) for x in x_range_cheb] result = calculate_matrix(n, x_range_cheb, y_range_cheb) # Shows f(x) and interpolation graphs plt.plot(np.arange(range_start, range_end, 0.01), [fn_to_eval(x) for x in np.arange(range_start, range_end, 0.01)], 'b', label='f(x)') plt.plot( np.arange(range_start, range_end, 0.01), [interpolate(result, x) for x in np.arange(range_start, range_end, 0.01)], 'r', label=f'{n} points interpolation function') plt.plot(np.arange(range_start, range_end, 0.01), [loss(result, x) for x in np.arange(range_start, range_end, 0.01)], 'g', label='Loss') plt.scatter(x_range_cheb, y_range_cheb, color='g', label='Chebyshev points') plt.title('f(x) interpolation using Chebyshev x') plt.xlabel('x') plt.ylabel('f(x)') plt.ylim(top=8, bottom=-1) plt.grid(True) plt.legend()
from functions import interpolate # Solution to PA1 (3) if __name__ == '__main__': try: coord_list = input('Please specify a set of points: ').split(' ') pts = [] for x, y in zip(coord_list[0::2], coord_list[1::2]): pts.append((float(x), float(y))) print('Interpolating over points: {{{}}}'.format(','.join( map(lambda x: '({}, {})'.format(x[0], x[1]), pts)))) poly = interpolate(pts) print('Result: ', str(poly)) except Exception as e: print(e)
u'B4', u'B5', u'B7'], inplace = True) #################### ###### INTERPOLATION ###################### # fill missing rows with Nan Values # to be interpolated over later dfMaster.drop_duplicates(subset=['pixel', 'date'], keep='first', inplace=True) time_range = pd.date_range(dfMaster.date.min().to_timestamp(),\ dfMaster.date.max().to_timestamp(), freq='M')\ .to_period('M') dfMaster.index = dfMaster.date dfMaster = dfMaster.groupby('pixel').apply(lambda x: x.reindex(time_range)) dfMaster = interpolate(df=dfMaster, columns=['NDVI', 'EVI', 'NDWI1', 'NDWI2']) # fill other nan columns with appropriate stuff ##################################### ################################### ####### MORE DATA ####### ################################### ####################################### dfMaster2 = None dfMaster2 = pd.DataFrame() data_path = '../../Data/AgMet2/Monthly/' print "Loading EM spectral dataframes..." for f in glob.glob(data_path + 'monthlyLSclass' + crop_id + '*'): dfMaster2 = pd.concat((dfMaster2, pd.read_csv(f)))
from functions import fn_to_eval, approximate, interpolate, loss, np import matplotlib.pyplot as plt x_range = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] y_range = [ -8.9003, -15.417, -0.6314, 1.93806, 7.63665, 13.1566, 15.3685, 15.025, 8.08982, 4.60416, -3.4688, -2.1801 ] n = len(x_range) row = 12 result = approximate(n, x_range, y_range, row) plt.scatter(x_range, y_range, label='Month`s temp. avg.') plt.plot(x_range, [interpolate(result, x) for x in x_range], 'r', label='Temp. interpolation') plt.grid(True) plt.legend() plt.xlabel('Month') plt.ylabel('Temp. avg.') plt.title("2007 Finland temperature") plt.show()
from functions import fn_to_eval, calculate_matrix, interpolate, loss, np import matplotlib.pyplot as plt x_range = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] y_range = [-8.9003,-15.417,-0.6314,1.93806,7.63665,13.1566,15.3685,15.025,8.08982,4.60416,-3.4688,-2.1801] n = len(x_range) result = calculate_matrix(n, x_range, y_range) # Shows f(x) and interpolation graphs plt.scatter(x_range, y_range, label='Month`s temp. avg.') plt.plot(x_range, [interpolate(result,x) for x in x_range], 'r', label='Temp. interpolation') plt.xticks(x_range) plt.xlabel('Month') plt.ylabel('Temp. avg.') plt.title("2007 Finland temperature") plt.grid(True) plt.legend() plt.show()