You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

皮肤温度时序数据的二次多项式拟合及曲率计算技术问询

Calculating Curvature from Quadratic Fits for Each ID

Hey there! I get that fitting curves and computing derivatives can get tricky, especially when dealing with grouped data. Let's break this down step by step to get those curvature values sorted out for each of your IDs.

First, a quick reminder: since we're fitting a quadratic polynomial (y = ax² + bx + c) to each ID's SkinTemp vs Time data, the derivatives simplify nicely:

  • First derivative: y' = 2ax + b (linear in Time)
  • Second derivative: y'' = 2a (a constant for each ID, since it's the second derivative of a quadratic)

That simplifies the curvature formula k = y''/(1 + y'^2)^(3/2) a lot—we just need to extract the coefficients from each fit, compute the derivatives, and plug them in.

Step-by-Step Implementation

1. Import Required Libraries

First, make sure you have pandas and numpy installed:

import pandas as pd
import numpy as np

2. Define a Function to Process Each ID Group

This function will handle fitting the quadratic polynomial, calculating derivatives, and computing curvature for every time point in the group:

def compute_curvature_for_group(group):
    # Fit quadratic polynomial: returns coefficients [a, b, c] (ax² + bx + c)
    coeffs = np.polyfit(group['Time'], group['SkinTemp'], deg=2)
    a, b, c = coeffs
    
    # Calculate first derivative (y')
    y_prime = 2 * a * group['Time'] + b
    
    # Calculate second derivative (y'' is constant for quadratic)
    y_double_prime = 2 * a
    
    # Compute curvature using the given formula
    curvature = y_double_prime / np.power(1 + np.square(y_prime), 3/2)
    
    # Add results back to the group DataFrame
    group['a'] = a
    group['b'] = b
    group['c'] = c
    group['curvature'] = curvature
    
    return group

3. Apply the Function to Your DataFrame

Assuming your DataFrame is named df with columns id, Time, and SkinTemp, we'll group by id and apply our function. We'll also filter out any IDs with fewer than 3 data points (since you need at least 3 points to fit a quadratic):

# Filter IDs with enough data points (≥3)
filtered_df = df.groupby('id').filter(lambda x: len(x) >= 3)

# Apply curvature calculation to each group
result_df = filtered_df.groupby('id').apply(compute_curvature_for_group).reset_index(drop=True)

4. Verify the Results

Your result_df will now have all the original columns plus:

  • a, b, c: The coefficients of the quadratic fit for each ID
  • curvature: The curvature value at each Time point for the ID

Key Notes to Keep in Mind

  • If a = 0, the quadratic fit reduces to a linear line. In this case, y'' = 0, so curvature will be 0 (which makes sense—straight lines have zero curvature).
  • The curvature will vary with Time for each ID because y' changes linearly with Time. If you need a single curvature value per ID (e.g., average curvature over time), you can add group['avg_curvature'] = curvature.mean() inside the function.
  • If you run into errors with np.polyfit, double-check that your Time and SkinTemp columns are numeric (no missing or non-null values).

Give this a shot with your dataset—hopefully this clears up the derivative and curvature calculation hurdles you were facing! 😊

内容的提问来源于stack exchange,提问作者HCAI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:34:30