绘制风速的 Weibull 分布

数据挖掘 统计数据 绘图 matplotlib 分配
2022-02-08 12:51:31

我有一组 720 小时的风速和风向数据,我想在上面拟合 Weibull 分布。经过一段时间的搜索,我用 Python 编写了以下代码来获取我的分发,我也将分享我的图像以供澄清。windspeed weibull<code>在此输入代码</code>

ws.hist(bins=np.arange(0, ws.max()), alpha=0.5, normed=True)
weibull_params = (sc.stats.exponweib.fit(ws,floc=0,f0=1))
x= ws
def weib(x,lamb,k):
   return (k / lamb) * (x / lamb)**(k-1) * np.exp(-(x/lamb)**k)
k_shape, lamb_scale = weibull_params[1], weibull_params[3]

plt.plot(x, weib(x, lamb_scale, k_shape), label='self-defined Weibull')

这看起来好吗?我如何理解这一点?我还在这里找到了一个包,但我不明白如何在我的代码中实现它。https://github.com/cqcn1991/Wind-Speed-Analysis

谢谢你。

1个回答

在 OP 评论中尝试了一些不起作用的东西。但是,您可以将Wolfram 语言应用到您的项目中。有一个免费的Wolfram 引擎供开发人员使用,您可以通过 Python 的Wolfram 客户端库在 Python中使用这些函数。

import datetime

from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr

启动 Wolfram 会话

wolfSession = WolframLanguageSession()

我将在 2016 年 10 月从离百慕大最近的气象站收集自己WindSpeedDataWeatherData气象站。"Country" Entity

weatherStation = wolfSession.evaluate(
    wl.WeatherData(wl.Entity('Country','Bermuda'))
);
print(weatherStation)
Entity['WeatherStation', 'TXKF']
windData = wolfSession.evaluate(
    wl.WindSpeedData(
        weatherStation,
        [
            datetime.datetime(2016,10,1),
            datetime.datetime(2016,10,31)
        ]
    )
)

windData是一个TimeSeries观察计数与您的问题相当的对象。

print(wolfSession.evaluate(windData('PathLength')))
1058

结果可以DateListPlot通过支持的Raster Image FormatsVector Graphics FormatsExport之一进行可视化

wolfSession.evaluate(
    wl.Export(
        '<path with image filename>', 
        wl.DateListPlot(windData, 
            PlotTheme='Detailed', 
            PlotRange=wl.All, 
            FrameLabel=wl.Automatic
        )
    )
)

数学图形

那个月的飓风是造成风速飙升的原因。

现在有了HistogramPDF规模给出的数据。

hist=wolfSession.evaluate(
    wl.Histogram(windData, wl.Automatic, 'PDF',
        PlotTheme='Detailed', 
        ChartStyle=wl.ColorData('Crayola','Silver'),
        PlotRange=wl.All
    )
);

wolfSession.evaluate(
    wl.Export(
        '<path with image filename>', 
        hist
    )
)

数学图形

用于. EstimatedDistribution_WeibullDistribution

windDistrbution=wolfSession.evaluate(
    wl.EstimatedDistribution(
        wl.QuantityMagnitude(windData('Values')),
        wl.WeibullDistribution(wl.Global.alpha, wl.Global.beta, wl.Global.mu)
    )
);
print(windDistrbution) 
WeibullDistribution[1.883495945177254, 28.34295076324276, -0.7675654467340361]

或者使用FindDistribution受限于连续分布函数来自动查找和拟合分布。

autoDistribution=wolfSession.evaluate(
    wl.FindDistribution(
        wl.QuantityMagnitude(windData('Values')),
        TargetFunctions='Continuous'
    )
);

print(autoDistribution)
ExtremeValueDistribution[18.436141153779957, 10.204118744677338]

检查 with 的优劣DistributionFitTest

print(
    wolfSession.evaluate(
        wl.Map(
            wl.Function(wl.DistributionFitTest(windData,wl.Slot(1),'PValue')), 
            [windDistrbution,autoDistribution]
        )
    )
)
[0.0002862317707539308, 0.1802410209978217]

Weibull 拟合不是很好,极值也不是那么强。在任何情况下PlotPDF功能。

pdfs=wolfSession.evaluate(
    wl.Map(
        wl.Function(wl.PDF(wl.Slot(1), wl.Global.x)), 
        [windDistrbution,autoDistribution]
    )
);


pdfPlot=wolfSession.evaluate(
    wl.Plot(
        pdfs,
        [wl.Global.x, 0, wl.QuantityMagnitude(wl.Max(windData))],
        PlotTheme='Detailed',
        PlotLegends=wl.Placed(
            wl.LineLegend(wl.Automatic, [windDistrbution,autoDistribution]), 
            wl.Below)
    )
)

wolfSession.evaluate(
    wl.Export(
        '<path with image filename>', 
        pdfPlot
    )
)

数学图形

将图与 结合起来Show

wolfSession.evaluate(
    wl.Export(
        '<path with image filename>', 
        wl.Show(hist, pdfPlot)
    )
)

数学图形

终止会话

wolfSession.terminate()

希望这可以帮助。