【问题标题】:Is there a way to cluster a curve in r using K-means using slope rather than value?有没有办法使用 K-means 使用斜率而不是值来聚类 r 中的曲线?
【发布时间】:2020-12-15 16:38:21
【问题描述】:

我正在尝试使用 K-means 方法将 r 中的以下曲线分为 2 组。当我运行 K-means 时,我将正值聚集在一起,将负值聚集在一起。我试图根据它们的斜率对它们进行聚类,其中第一组在斜率为正时,第二组在斜率为负时。 我很确定答案是将曲线转换为其斜率值,但不确定如何获得曲线的移动斜率。 我会很感激你的帮助。谢谢!

【问题讨论】:

  • 你的例子是sin,很容易计算导数。您知道函数及其导数吗?还是只有数据?
  • 它看起来像一个正弦曲线,但它不是一个精确的正弦曲线。此外,我没有可以计算导数的方程式,只有数据。

标签: r statistics regression k-means


【解决方案1】:

这可能是你想要的。我创建了一些垃圾测试数据。

##  Some sample data - not sin, but pretty close.
set.seed(1234)
x = sort(runif(200, -pi, pi))
y = sin(x) * (1 + x/20)


## Get derivative
DERIV = diff(y)/diff(x)

## Set up blank plot
plot(x,y, type='n')

## Add curve
segments(x[-length(x)], y[-length(y)], x[-1], y[-1], 
    col=as.numeric(DERIV>0)+1)

【讨论】:

  • 非常感谢!我相信您的回答让我朝着正确的方向前进!
猜你喜欢
  • 2020-12-16
  • 2012-01-01
  • 2022-01-27
  • 2011-04-11
  • 2020-02-28
  • 2016-07-28
  • 1970-01-01
  • 2020-08-03
  • 2020-06-03
相关资源
最近更新 更多