本文主要是介绍获得两类相关点之间的线性关系,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
例如我是有两幅图,但是他们的单位不一样(一个温度,一个央斯基)原则上他们只是相差一个因子。可以利用一些线性拟合的关系来获得那个因子。其实代码和那个计算谱指数差不多,只是做了一些容易犯错误的修改(斜率的计算上)
from astropy.io import fits as pf
from astropy.wcs import WCS
import sys
from copy import deepcopy
from astropy.coordinates import SkyCoord
from reproject import reproject_interp
import matplotlib.pyplot as plt
import numpy as np
from astropy import units as u
import math
from statistics import mode
from astropy.convolution import convolve, Gaussian2DKernel
from astropy.modeling import Fittable1DModel, Parameter, models, fitting#例如data和data_cite 记得先平滑到一个分辨率上
cal_spectral(data[mark1==1].flatten()[::1], data_cite[mark1==1].flatten()[::1], 1.248, 1.248)def cal_alpha(data1,data2):n = data1.sizex = np.zeros(n) y = np.zeros(n)kk = 0for i in range(n):if np.isnan(data1[i]) or np.isnan(data2[i]): continuex[kk] = data1[i]y[kk] = data2[i]kk += 1if kk<10: return np.nan,np.nan,np.nan,np.nanp_init = models.Polynomial1D(1)fit_p = fitting.LinearLSQFitter()p = fit_p(p_init, x[:kk], y[:kk])a, b = p.c0.value, p.c1.valuereturn a, bdef cal_spectral(data1, data2, freq1, freq2):a1, b1 = cal_alpha(data1, data2)a2, b2 = cal_alpha(data2, data1)print(b1, 1/b2)alpha_1 = (b1 + 1/b2) / 2.d_alpha_1 = abs(1/b2 - b1)a1_p = a1 * 1.b1_p = b1 * 1.a2_p = -a2/b2b2_p = 1./b2#画图plt.rcParams.update({'font.size': 25})fig = plt.figure(figsize=(15,11))ax = fig.add_subplot(1, 1, 1)ax.plot(data1, data2, 'o')ax.plot(data1, a1_p + b1_p * data1)ax.plot(data1, a2_p + b2_p * data1)ss = r'$-$' + '%4.2f' % abs(alpha_1) + r'$\pm$' + '%4.2f' % d_alpha_1ax.text(0.1, 0.9, ss, horizontalalignment='left', verticalalignment='center', transform=ax.transAxes)ax.set_xlabel('T (K, at %5.3f GHz)' % freq1)ax.set_ylabel('T(K, Extrapolating E. M. Berkhuijsen observ-\n-ational data to a frequency of 1.248 GHz)')ax.xaxis.labelpad = 15ax.yaxis.labelpad = 8plt.savefig('Test_Temperature calibration.png', bbox_inches = 'tight')plt.show()
这篇关于获得两类相关点之间的线性关系的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!