高斯和scipy之和的曲线拟合 [英] curve fitting by a sum of gaussian with scipy

查看:339
本文介绍了高斯和scipy之和的曲线拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做生物信息学,我们将小RNA映射到mRNA上。我们具有每个mRNA上蛋白质的映射坐标,并计算蛋白质与mRNA结合的位置与小RNA结合的位点之间的相对距离。

I'm doing bioinformatics and we map small RNA on mRNA. We have the mapping coordinate of a protein on each mRNA and we calculate the relative distance between the place where the protein bound the mRNA and the site that is bound by a small RNA.

我获得以下数据集:

dist    eff
-69 3
-68 2
-67 1
-66 1
-60 1
-59 1
-58 1
-57 2
-56 1
-55 1
-54 1
-52 1
-50 2
-48 3
-47 1
-46 3
-45 1
-43 1
0   1
1   2
2   12
3   18
4   18
5   13
6   9
7   7
8   5
9   3
10  1
13  2
14  3
15  2
16  2
17  2
18  2
19  2
20  2
21  3
22  1
24  1
25  1
26  1
28  2
31  1
38  1
40  2

当我绘制数据时,我有3张照片:1张在3 -4
附近,另一张在20附近,最后一张在-50附近。

When i plot the data, i have 3 pics : 1 at around 3 -4 another one around 20 and a last one around -50.

我尝试三次样条插值,但是没有对于我的数据来说效果很好。

I try cubic spline interpolation, but it does'nt work very well for my data.

我的想法是用高斯和对曲线进行拟合。
例如,在我的情况下,在点5,20和-50处估计3高斯曲线。

My idea was to do curve fitting with a sum of gaussians. For example in my case, estimate 3 gaussian curve at point 5,20 and -50.

我怎么做?

我查看了scipy.optimize.curve_fit(),但是如何以精确的间隔拟合曲线?
如何添加曲线以具有一条曲线?

I looked at scipy.optimize.curve_fit(), but how can i fit the curve at precise intervalle ? How can i add the curve to have one single curve ?

推荐答案

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats
import scipy.optimize

data = np.array([-69,3, -68, 2, -67, 1, -66, 1, -60, 1, -59, 1,
                 -58, 1, -57, 2, -56, 1, -55, 1, -54, 1, -52, 1,
                 -50, 2, -48, 3, -47, 1, -46, 3, -45, 1, -43, 1,
                 0, 1, 1, 2, 2, 12, 3, 18, 4, 18, 5, 13, 6, 9,
                 7, 7, 8, 5, 9, 3, 10, 1, 13, 2, 14, 3, 15, 2,
                 16, 2, 17, 2, 18, 2, 19, 2, 20, 2, 21, 3, 22, 1,
                 24, 1, 25, 1, 26, 1, 28, 2, 31, 1, 38, 1, 40, 2])
x, y = data.reshape(-1, 2).T

def tri_norm(x, *args):
    m1, m2, m3, s1, s2, s3, k1, k2, k3 = args
    ret = k1*scipy.stats.norm.pdf(x, loc=m1 ,scale=s1)
    ret += k2*scipy.stats.norm.pdf(x, loc=m2 ,scale=s2)
    ret += k3*scipy.stats.norm.pdf(x, loc=m3 ,scale=s3)
    return ret


params = [-50, 3, 20, 1, 1, 1, 1, 1, 1]

fitted_params,_ = scipy.optimize.curve_fit(tri_norm,x, y, p0=params)

plt.plot(x, y, 'o')
xx = np.linspace(np.min(x), np.max(x), 1000)
plt.plot(xx, tri_norm(xx, *fitted_params))
plt.show()

>>> fitted_params
array([ -60.46845528,    3.801281  ,   13.66342073,   28.26485602,
          1.63256981,   10.31905367,  110.51392765,   69.11867159,
         63.2545624 ])

因此您可以看到您对三个峰值函数的想法与实际数据不太一致。

So you can see your idea of the three peaked function doesn't agree too much with your real data.

这篇关于高斯和scipy之和的曲线拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆