返回峰值为lis

2024-09-28 21:54:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些代码将局部最大值标识为峰值。我想以列表的形式返回峰值[ 3, 6, 8, 10],但是当它打印出来时,返回为[ 3 6 8 10],没有逗号。我该如何让峰值作为列表返回?或者有没有一种方法可以从这个函数返回一个列表

import numpy as np
import matplotlib.pyplot as plt
from random import uniform
from scipy.signal import find_peaks, argrelextrema
import pandas as pd


player_name = 'player'
game_log = [22.7, 16.7, 18.5, 38.2, 1.5, 8.6,
            12.6, 7.4, 34.5, 23.2, 34.5, 20.5, 24.0, 35.1]


rounded_game_log = [round(x) for x in game_log]
formatted_game_log = np.array(rounded_game_log)

y = formatted_game_log
y = np.array(y)
x = (range(0, (len(formatted_game_log))))
x = np.array(x)
colors = (0, 0, 0)
area = np.pi*3

dips = np.where((y[1:-1] < y[0:-2]) * (y[1:-1] < y[2:]))[0] + 1

plt.xticks(np.arange(min(x), max(x)+5, 1.0))
plt.yticks(np.arange(min(y), max(y)+5, 5.0))
plt.axhline(y.mean(), color='k',
            linestyle='dashed', linewidth=1)
plt.plot(x, y, c=colors, alpha=0.5)
peaks, _ = find_peaks(y)
plt.plot(peaks, y[peaks], "x")
plt.plot(dips, y[dips], 'o')
plt.title(player_name)
plt.xlabel('Games')
plt.ylabel('Fantasy Points')
plt.show()

Tags: fromimportloggame列表plotasnp