使用 Python 進行綜合天氣資料分析:溫度、降雨趨勢和視覺化

Mary-Kate Olsen
發布: 2024-10-16 18:13:03
原創
722 人瀏覽過
  • ケニアのさまざまな都市の気象データの分析と予測
    • はじめに
    • データセットの概要
    • 探索的データ分析
    • 主要な気象特徴の視覚化
    • 気象状況分析
    • 都市別降雨量
    • 月平均気温
    • 月間平均降水量
    • 気象変数間の相関関係
    • ケーススタディ: 都市特有の傾向
    • 結論

ケニアのさまざまな都市の気象データの分析と予測


導入

この記事では、Python を使用して天気パターンを分析する方法を説明します。気温傾向の特定から降水量の視覚化まで、このステップバイステップのガイドは、気象分析にデータ サイエンス技術を使用することに興味がある人に最適です。実用的な洞察を得るために、コード、データ操作、視覚化について調査します。

ケニアでは、天気は多くの分野、特に農業、観光、野外活動において重要な役割を果たしています。農家、企業、イベント プランナーは、意思決定を行うために正確な気象情報を必要としています。ただし、気象パターンは地域ごとに大きく異なる可能性があり、現在の予測システムでは常に局所的な洞察が得られるとは限りません。

このプロジェクトの目的は、ケニア全土のさまざまな地域の OpenWeatherMap API と Weather API からリアルタイムの気象データを収集することです。このデータはデータベースに保存され、Python を使用して分析され、次のような洞察が明らかになります。-

  • 気温の傾向
  • 降雨パターン - 湿度と風の状態

このプロジェクトでは、ケニアのさまざまな都市の気象情報を含むデータセットを分析します。データセットには、温度、湿度、気圧、風速、視程、降雨量などの要素を含む 3,000 行を超える気象観測結果が含まれています。これらの洞察を使用して、農業、観光、さらには管理などの天候に敏感なセクターにおける意思決定を支援できる、正確な地域固有の天気予報を提供することを目指しています。

データセットの概要

データセットはいくつかの列を使用して構造化されました:

  • Datetime - 天気がいつ記録されたかを示すタイムスタンプ。
  • 都市と国 - 気象観測の場所。
  • 緯度と経度 - 場所の地理座標。
  • 温度 (摂氏) - 記録された温度。
  • 湿度 (%) - 空気中の湿度の割合。
  • 圧力 (hPa) - ヘクトパスカル単位の大気圧。
  • 風速 (m/s) - その時の風速。
  • 雨 (mm) - ミリメートル単位で測定された降雨量。
  • 雲 (%) - 雲の範囲の割合。
  • 気象条件と天気の説明 - 天気の一般的および詳細な説明 (例: 「雲」、「散在雲」)。

これは、データベース内でデータがどのように構造化されているかです。
Comprehensive Weather Data Analysis Using Python: Temperature, Rainfall Trends, and Visualizations


探索的データ分析

分析の最初のステップには、データの基本的な調査が含まれていました。
_ データ ディメンション - データセットには 3,000 行と 14 列が含まれています。
_ Null 値 - データの欠落が最小限に抑えられ、さらなる分析に対してデータセットの信頼性が保証されます。

print(df1[['temperature_celsius', 'humidity_pct', 'pressure_hpa', 'wind_speed_ms', 'rain', 'clouds']].describe())
登入後複製

上記のコードを使用して、温度、湿度、圧力、降雨量、雲の範囲、平均、広がりについての洞察を提供する数値列の要約統計量を計算しました。

主要な気象特徴の視覚化

気象の特徴をより明確に理解するために、さまざまな分布をプロットしました。

温度分布

sns.displot(df1['temperature_celsius'], bins=50, kde=True)
plt.title('Temperature Distribution')
plt.xlabel('Temperature (Celsius)')
登入後複製

この分布は、都市全体の気温の一般的な広がりを明らかにします。 KDE ライン プロットは、温度の確率分布を滑らかに推定します。

降水量分布

sns.displot(df1['rain'], bins=50, kde=True)
plt.title('Rainfall Distribution')
plt.xlabel('Rainfall (mm/h)')
登入後複製

このコードは、ケニアの都市全体の降雨分布を分析します。

湿度、気圧、風速

湿度 (%)気圧 (hPa)、および 風速 (m/s) の同様の分布プロットは、それぞれについて有益な洞察を提供します。データセット全体にわたるこれらのパラメーターのバリエーション

気象状況の分析

気象条件 (「雲」、「雨」など) がカウントされ、円グラフを使用して視覚化され、その比例分布が示されました。

condition_counts = df1['weather_condition'].value_counts()

plt.figure(figsize=(8,8))
plt.pie(condition_counts, labels=condition_counts.index, autopct='%1.1f%%', pctdistance=1.1, labeldistance=0.6, startangle=140)
plt.title('Distribution of Weather Conditions')
plt.axis('equal')
plt.show()
登入後複製

Comprehensive Weather Data Analysis Using Python: Temperature, Rainfall Trends, and Visualizations

City-wise Rainfall

One of the key analysis was the total rainfall by city:

rainfall_by_city = df1.groupby('city')['rain'].sum().sort_values()

plt.figure(figsize=(12,12))
rainfall_by_city.plot(kind='barh', color='skyblue')
plt.title('Total Rainfall by City')
plt.xlabel('Total Rainfall (mm)')
plt.ylabel('City')
plt.tight_layout()
plt.show()
登入後複製

This bar plot highlighted which cities received the most rain over the observed period, with a few outliers showing significant rainfall compared to others.

Comprehensive Weather Data Analysis Using Python: Temperature, Rainfall Trends, and Visualizations

Average Monthly Temperature

avg_temp_by_month.plot(kind='line')
plt.title('Average Monthly Temperature')
登入後複製

The line chart revealed temperature fluctuations across different months, showing seasonal changes.

Comprehensive Weather Data Analysis Using Python: Temperature, Rainfall Trends, and Visualizations

Average Monthly Rainfall

monthly_rain.plot(kind='line')
plt.title('Average Monthly Rainfall')
登入後複製

Similarly, rainfall was analyzed to observe how it varied month-to-month.

Comprehensive Weather Data Analysis Using Python: Temperature, Rainfall Trends, and Visualizations

We also visualized the data using heatmaps for a more intuitive understanding of monthly temperature and rainfall.
Here are the heatmaps for the average monthly temperature and rainfall

Comprehensive Weather Data Analysis Using Python: Temperature, Rainfall Trends, and Visualizations

Comprehensive Weather Data Analysis Using Python: Temperature, Rainfall Trends, and Visualizations

Correlation Between Weather Variables

Next, I calculated the correlation matrix between key weather variables:

correlation_matrix = df1[['temperature_celsius', 'humidity_pct', 'pressure_hpa', 'wind_speed_ms', 'rain', 'clouds']].corr()
correlation_matrix
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Between Weather Variables')
登入後複製

This heatmap allowed us to identify relationships between variables. For example, we observed a negative correlation between temperature and humidity, as expected.

Case Study: City Specific Trends

I have focused on individual cities such as Mombasa and Nyeri, to explore their unique weather patterns:

Mombasa Temperature Trends

plt.plot(monthly_avg_temp_msa)
plt.title('Temperature Trends in Mombasa Over Time')
登入後複製

This city showed significant variation in temperature across the year.

Nyeri Rainfall Trends

plt.plot(monthly_avg_rain_nyr)
plt.title('Rainfall Trends in Nyeri Over Time')
登入後複製

The rainfall data for Nyeri displayed a clear seasonal pattern, with rainfall peaking during certain months.

Conclusion

This analysis provides a comprehensive overview of the weather conditions in major cities, highlighting the temperature, rainfall, and other key weather variables. By using visualizations like histograms, line charts, pie charts, and heatmaps, we were able to extract meaningful insights into the data. Further analysis could involve comparing these trends with historical weather patterns or exploring predictive modeling to forecast future weather trends.

You can find the Jupyter Notebook with the full code for this analysis in my GitHub repository).


以上是使用 Python 進行綜合天氣資料分析:溫度、降雨趨勢和視覺化的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!