簡體   English   中英

Python 找到分布的峰值

[英]Python find peaks of distribution

在這樣的數據集中:(y 是角度,x 是數據點)

在此處輸入圖像描述

如何在忽略潛在隨機點的情況下找到每個“波段”的加權平均值(在這種情況下為 0.1 和 -90 左右)。

我在考慮 FFT 變換,但這可能不是正確的方法。

也許將其轉換為類似正態分布的圖形並找到峰值?

使用KMeans求解

步驟 1. 生成數據

from random import randint, choice
from numpy import random
import numpy as np
from matplotlib import pyplot as plt

def gen_pts(mean_, std_, n):
  """Generate gaussian distributed random data
     mean: mean_
    standard deviation: std_
    number points: n
  """
  return np.random.normal(loc=mean_, scale = std_, size = n)

# Number of groups of horizontal blobs
n_groups = 20

# Genereate random count for each group
counts = [randint(100, 200) for _ in range(n_groups)]

# Generate random mean for each group (i.e. 0 or -90)
means = [random.choice([0, -90]) for _ in range(n_groups)]

# All the groups
data = [gen_pts(mean_, 5, n) for mean_, n in zip(means, counts)]

# Concatenate groups into 1D array
X = np.concatenate(data, axis=0)

# Show Data
plt.plot(X)
plt.show()

在此處輸入圖像描述

第 2 步 - 查找集群中心

# Reshape 1D data so it's suitable for kmeans model
X = X.reshape(-1,1)

# Get model for two clusters
kmeans = KMeans(n_clusters=2, init='k-means++', max_iter=300, n_init=10, random_state=0)

# Fit Data to model
pred_y = kmeans.fit_predict(X)

# Cluster Centers
centers = kmeans.cluster_centers_
print(*centers)
# Output: [-89.79165334] [-0.07875314]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM