【问题标题】:Calculate the rate of change of a parameter, with given x and y coordinates使用给定的 x 和 y 坐标计算参数的变化率
【发布时间】:2020-11-26 14:36:34
【问题描述】:

我有 X 和 Y 坐标以及感兴趣的参数的对应值。我想知道这个参数的变化率。

以下是数据摘录:

+-----------+------------+-----------+
|     X     |     Y      | Parameter |
+-----------+------------+-----------+
| 503830.38 | 4443799.09 |     12.78 |
| 503837.37 | 4443809.46 |     12.36 |
| 503840.75 |  4443792.1 |      9.48 |
| 503844.36 | 4443819.82 |     13.05 |
| 503847.74 | 4443802.47 |      8.95 |
| 503851.11 | 4443785.11 |      8.33 |
| 503851.35 | 4443830.18 |     13.84 |
| 503854.73 | 4443812.83 |      9.05 |
|  503858.1 | 4443795.48 |      8.27 |
| 503858.34 | 4443840.55 |     14.28 |
| 503861.47 | 4443778.12 |      5.27 |
| 503861.72 | 4443823.19 |      9.12 |
| 503865.09 | 4443805.84 |      8.22 |
| 503865.33 | 4443850.91 |     12.56 |
| 503868.46 | 4443788.49 |      5.32 |
+-----------+------------+-----------+

我尝试了 numpy.gradient (np.gradient(np.array(df))) 函数并为完整数据集创建了一个绘图,但我无法真正理解生成的元组是什么。该函数的解释表明它创建了关于所有轴的渐变,并给出了 2D 的示例。但是,我想要的是能够指定感兴趣的方向。例如,考虑到数据是地理坐标,我想定义参数变化率的方向是S或SE方向。

【问题讨论】:

    标签: python coordinates gradient


    【解决方案1】:

    这并不完全是微不足道的。您有一个二维函数 P=P(X,Y) 在坐标网格上的多个点中定义,并且您想要计算 dP/dX 和 dP/dY (这本身将取决于计算它们的点),更一般地说,任何方向的 dP/d(direction)

    所以这是我能想到的最简单的解决方案,hopfulyl 让您朝着正确的方向前进。基本上这个想法是拟合一个线性函数 P ~ c1 X + c2 * Y 然后 c1,c2 可以解释为 P 分别相对于 X 和 Y 的变化率

    首先我们加载数据和一些导入

    import matplotlib.pyplot as plt
    from io import StringIO
    import pandas as pd
    import numpy as np
    import math
    
    data = StringIO(
    """
    X,Y,Parameter
    503830.38,4443799.09,12.78
    503837.37,4443809.46,12.36
    503840.75, 4443792.1, 9.48
    503844.36,4443819.82,13.05
    503847.74,4443802.47, 8.95
    503851.11,4443785.11, 8.33
    503851.35,4443830.18,13.84
    503854.73,4443812.83, 9.05
     503858.1,4443795.48, 8.27
    503858.34,4443840.55,14.28
    503861.47,4443778.12, 5.27
    503861.72,4443823.19, 9.12
    503865.09,4443805.84, 8.22
    503865.33,4443850.91,12.56
    503868.46,4443788.49, 5.32
    """)
    df = pd.read_csv(data)
    

    然后您可以可视化您的数据以供检查:

    from matplotlib import cm
    %matplotlib auto
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.scatter(df['X'], df['Y'], df['Parameter'],cmap=cm.coolwarm)
    

    这将创建一个 3D 图,显示 P(参数)的值实际上在 X、Y 中大致呈线性

    接下来我们计算线性回归

    from sklearn.linear_model import Ridge
    %matplotlib inline
    lin_regr = Ridge(alpha=1e-4)
    lin_res = lin_regr.fit(df[['X','Y']], df['Parameter'])
    lin_fit = lin_regr.predict(df[['X','Y']])
    plt.plot(lin_fit, df['Parameter'], 'o')
    plt.plot(lin_fit, lin_fit, '-')
    print(lin_res.coef_)
    

    该图显示了 P 的值与我们的线性拟合(蓝点)的对比,以直观地确认我们没有偏离:

    线性回归的系数为

    [-0.15636806  0.11802771]
    

    因此,P 在 X 方向上的变化率约为 c1=-0.156,在 Y 方向上约为 c2 = 0.118。对于任何其他方向,以角度 alpha 表示(以弧度表示),它由下式给出

    rate of change in direction alpha = c1*cos(alpha) + c2*sin(alpha)
    

    【讨论】:

      猜你喜欢
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多