K-means is an unsupervised machine learning algorithm that clusters data into k distinct groups based on similarity.
Climate analysis helps identify patterns, classify climate zones, and conduct temporal studies using environmental data.
The process of performing climate analysis using K-means includes:
Importing the necessary libraries
Loading the data
Selecting relevant features
Creating and fitting the K-means model
Visualizing the clustered data to uncover relationships between attributes such as temperature and humidity
K-means is an unsupervised machine-learning algorithm used for clustering data into K-distinct groups or clusters based on similarity. The primary goal of K-means is to partition a dataset into K clusters, where each cluster represents a group of data points that are more similar to each other compared to points in other clusters.
K-means clustering is employed in climate analysis for pattern recognition, data compression, classification of climate zones, anomaly detection, and temporal analysis. It aids in identifying similar regions, reducing data complexity, and exploring hidden structures in large climate datasets, contributing to understanding spatial and temporal climate variations.
We will be using K-means to analyze the relationship between temperature and humidity in the code below.
Dataset: The dataset.csv
file is a CSV file that contains weather-related data. The columns include formatted date, weather summary, precipitation type, temperature, apparent temperature, humidity, wind speed, wind bearing, visibility, cloud cover, atmospheric pressure, and daily summary.
Each row represents hourly weather data and includes details such as the date, weather conditions, temperature metrics, and atmospheric parameters. This dataset enables analysis of weather patterns and conditions over time.
Formatted Date,Summary,Precip Type,Temperature (C),Apparent Temperature (C),Humidity,Wind Speed (km/h),Wind Bearing (degrees),Visibility (km),Loud Cover,Pressure (millibars),Daily Summary2006-04-01 00:00:00.000 +0200,Partly Cloudy,rain,9.472222222222221,7.3888888888888875,0.89,14.1197,251.0,15.826300000000002,0.0,1015.13,Partly cloudy throughout the day.2006-04-01 01:00:00.000 +0200,Partly Cloudy,rain,9.355555555555558,7.227777777777776,0.86,14.2646,259.0,15.826300000000002,0.0,1015.63,Partly cloudy throughout the day.2006-04-01 02:00:00.000 +0200,Mostly Cloudy,rain,9.377777777777778,9.377777777777778,0.89,3.9284000000000003,204.0,14.9569,0.0,1015.94,Partly cloudy throughout the day.2006-04-01 03:00:00.000 +0200,Partly Cloudy,rain,8.28888888888889,5.944444444444446,0.83,14.1036,269.0,15.826300000000002,0.0,1016.41,Partly cloudy throughout the day.2006-04-01 04:00:00.000 +0200,Mostly Cloudy,rain,8.755555555555553,6.977777777777779,0.83,11.0446,259.0,15.826300000000002,0.0,1016.51,Partly cloudy throughout the day.2006-04-01 05:00:00.000 +0200,Partly Cloudy,rain,9.222222222222221,7.11111111111111,0.85,13.9587,258.0,14.9569,0.0,1016.66,Partly cloudy throughout the day.2006-04-01 06:00:00.000 +0200,Partly Cloudy,rain,7.733333333333334,5.522222222222221,0.95,12.3648,259.0,9.982000000000001,0.0,1016.72,Partly cloudy throughout the day.2006-04-01 07:00:00.000 +0200,Partly Cloudy,rain,8.77222222222222,6.527777777777778,0.89,14.1519,260.0,9.982000000000001,0.0,1016.84,Partly cloudy throughout the day.2006-04-01 08:00:00.000 +0200,Partly Cloudy,rain,10.82222222222222,10.82222222222222,0.82,11.3183,259.0,9.982000000000001,0.0,1017.37,Partly cloudy throughout the day.2006-04-01 09:00:00.000 +0200,Partly Cloudy,rain,13.77222222222222,13.77222222222222,0.72,12.525800000000002,279.0,9.982000000000001,0.0,1017.22,Partly cloudy throughout the day.2006-04-01 10:00:00.000 +0200,Partly Cloudy,rain,16.016666666666666,16.016666666666666,0.67,17.5651,290.0,11.2056,0.0,1017.42,Partly cloudy throughout the day.2006-04-01 11:00:00.000 +0200,Partly Cloudy,rain,17.144444444444446,17.144444444444446,0.54,19.7869,316.0,11.4471,0.0,1017.74,Partly cloudy throughout the day.2006-04-01 12:00:00.000 +0200,Partly Cloudy,rain,17.800000000000004,17.800000000000004,0.55,21.944300000000002,281.0,11.270000000000001,0.0,1017.59,Partly cloudy throughout the day.2006-04-01 13:00:00.000 +0200,Partly Cloudy,rain,17.333333333333332,17.333333333333332,0.51,20.6885,289.0,11.270000000000001,0.0,1017.48,Partly cloudy throughout the day.2006-04-01 14:00:00.000 +0200,Partly Cloudy,rain,18.87777777777778,18.87777777777778,0.47,15.375500000000002,262.0,11.4471,0.0,1017.17,Partly cloudy throughout the day.2006-04-01 15:00:00.000 +0200,Partly Cloudy,rain,18.911111111111115,18.911111111111115,0.46,10.4006,288.0,11.270000000000001,0.0,1016.47,Partly cloudy throughout the day.2006-04-01 16:00:00.000 +0200,Partly Cloudy,rain,15.38888888888889,15.38888888888889,0.6,14.4095,251.0,11.270000000000001,0.0,1016.15,Partly cloudy throughout the day.2006-04-01 17:00:00.000 +0200,Mostly Cloudy,rain,15.550000000000002,15.550000000000002,0.63,11.157300000000001,230.0,11.4471,0.0,1016.17,Partly cloudy throughout the day.2006-04-01 18:00:00.000 +0200,Mostly Cloudy,rain,14.255555555555553,14.255555555555553,0.69,8.5169,163.0,11.2056,0.0,1015.82,Partly cloudy throughout the day.2006-04-01 19:00:00.000 +0200,Mostly Cloudy,rain,13.144444444444442,13.144444444444442,0.7,7.631400000000001,139.0,11.2056,0.0,1015.83,Partly cloudy throughout the day.2006-04-01 20:00:00.000 +0200,Mostly Cloudy,rain,11.549999999999999,11.549999999999999,0.77,7.3899,147.0,11.0285,0.0,1015.85,Partly cloudy throughout the day.2006-04-01 21:00:00.000 +0200,Mostly Cloudy,rain,11.183333333333334,11.183333333333334,0.76,4.9266000000000005,160.0,9.982000000000001,0.0,1015.77,Partly cloudy throughout the day.2006-04-01 22:00:00.000 +0200,Partly Cloudy,rain,10.116666666666667,10.116666666666667,0.79,6.6493,163.0,15.826300000000002,0.0,1015.4,Partly cloudy throughout the day.2006-04-01 23:00:00.000 +0200,Mostly Cloudy,rain,10.2,10.2,0.77,3.9284000000000003,152.0,14.9569,0.0,1015.51,Partly cloudy throughout the day.2006-04-10 00:00:00.000 +0200,Partly Cloudy,rain,10.42222222222222,10.42222222222222,0.62,16.985500000000002,150.0,15.826300000000002,0.0,1014.4,Mostly cloudy throughout the day.2006-04-10 01:00:00.000 +0200,Partly Cloudy,rain,9.911111111111113,7.566666666666666,0.66,17.2109,149.0,15.826300000000002,0.0,1014.2,Mostly cloudy throughout the day.2006-04-10 02:00:00.000 +0200,Mostly Cloudy,rain,11.183333333333334,11.183333333333334,0.8,10.8192,163.0,14.9569,0.0,1008.71,Mostly cloudy throughout the day.2006-04-10 03:00:00.000 +0200,Partly Cloudy,rain,7.155555555555556,5.044444444444443,0.79,11.0768,180.0,15.826300000000002,0.0,1014.47,Mostly cloudy throughout the day.2006-04-10 04:00:00.000 +0200,Partly Cloudy,rain,6.111111111111111,4.816666666666667,0.82,6.6493,161.0,15.826300000000002,0.0,1014.45,Mostly cloudy throughout the day.2006-04-10 05:00:00.000 +0200,Partly Cloudy,rain,6.788888888888888,4.272222222222221,0.83,13.0088,135.0,14.9569,0.0,1014.49,Mostly cloudy throughout the day.2006-04-10 06:00:00.000 +0200,Mostly Cloudy,rain,7.26111111111111,5.155555555555556,0.85,11.1734,141.0,6.1985,0.0,1014.52,Mostly cloudy throughout the day.2006-04-10 07:00:00.000 +0200,Mostly Cloudy,rain,7.799999999999999,5.5277777777777795,0.83,12.8156,150.0,8.05,0.0,1014.16,Mostly cloudy throughout the day.2006-04-10 08:00:00.000 +0200,Mostly Cloudy,rain,9.872222222222225,7.933333333333334,0.78,13.7494,160.0,9.982000000000001,0.0,1014.24,Mostly cloudy throughout the day.2006-04-10 09:00:00.000 +0200,Mostly Cloudy,rain,12.222222222222221,12.222222222222221,0.72,15.633100000000002,150.0,9.982000000000001,0.0,1014.25,Mostly cloudy throughout the day.2006-04-10 10:00:00.000 +0200,Mostly Cloudy,rain,15.094444444444447,15.094444444444447,0.61,17.549000000000003,151.0,9.982000000000001,0.0,1013.96,Mostly cloudy throughout the day.2006-04-10 11:00:00.000 +0200,Mostly Cloudy,rain,17.355555555555558,17.355555555555558,0.52,22.7815,169.0,9.982000000000001,0.0,1013.85,Mostly cloudy throughout the day.2006-04-10 12:00:00.000 +0200,Mostly Cloudy,rain,19.00555555555555,19.00555555555555,0.46,23.892400000000002,169.0,9.982000000000001,0.0,1013.04,Mostly cloudy throughout the day.2006-04-10 13:00:00.000 +0200,Mostly Cloudy,rain,20.04444444444444,20.04444444444444,0.4,28.3682,170.0,9.982000000000001,0.0,1012.22,Mostly cloudy throughout the day.2006-04-10 14:00:00.000 +0200,Mostly Cloudy,rain,21.049999999999997,21.049999999999997,0.4,26.903100000000002,187.0,10.3523,0.0,1011.44,Mostly cloudy throughout the day.2006-04-10 15:00:00.000 +0200,Mostly Cloudy,rain,21.18333333333333,21.18333333333333,0.37,25.695600000000002,179.0,9.982000000000001,0.0,1010.52,Mostly cloudy throughout the day.2006-04-10 16:00:00.000 +0200,Mostly Cloudy,rain,20.11666666666666,20.11666666666666,0.4,25.309200000000004,162.0,9.982000000000001,0.0,1009.83,Mostly cloudy throughout the day.2006-04-10 17:00:00.000 +0200,Mostly Cloudy,rain,20.216666666666665,20.216666666666665,0.36,18.1125,161.0,10.3523,0.0,1009.26,Mostly cloudy throughout the day.2006-04-10 18:00:00.000 +0200,Mostly Cloudy,rain,20.0,20.0,0.43,23.425500000000003,160.0,9.982000000000001,0.0,1008.76,Mostly cloudy throughout the day.2006-04-10 19:00:00.000 +0200,Mostly Cloudy,rain,17.800000000000004,17.800000000000004,0.5,20.0445,150.0,11.2056,0.0,1008.36,Mostly cloudy throughout the day.2006-04-10 20:00:00.000 +0200,Mostly Cloudy,rain,16.06111111111111,16.06111111111111,0.53,21.3969,149.0,9.982000000000001,0.0,1008.11,Mostly cloudy throughout the day.2006-04-10 21:00:00.000 +0200,Mostly Cloudy,rain,15.02222222222222,15.02222222222222,0.55,21.3808,159.0,9.982000000000001,0.0,1008.15,Mostly cloudy throughout the day.2006-04-10 22:00:00.000 +0200,Overcast,rain,14.422222222222224,14.422222222222224,0.58,20.0123,159.0,15.826300000000002,0.0,1007.85,Mostly cloudy throughout the day.2006-04-10 23:00:00.000 +0200,Overcast,rain,14.255555555555553,14.255555555555553,0.59,16.5025,168.0,14.9569,0.0,1007.89,Mostly cloudy throughout the day.2006-04-11 00:00:00.000 +0200,Overcast,rain,13.77222222222222,13.77222222222222,0.6,17.0982,160.0,15.826300000000002,0.0,1007.36,Foggy in the evening.2006-04-11 01:00:00.000 +0200,Overcast,rain,13.283333333333331,13.283333333333331,0.63,14.3612,160.0,15.826300000000002,0.0,1007.26,Foggy in the evening.2006-04-11 02:00:00.000 +0200,Overcast,rain,8.633333333333333,5.466666666666669,0.87,22.0409,32.0,6.6976,0.0,1005.1,Foggy in the evening.2006-04-11 03:00:00.000 +0200,Overcast,rain,11.25,11.25,0.83,11.3344,152.0,15.826300000000002,0.0,1007.01,Foggy in the evening.2006-04-11 04:00:00.000 +0200,Overcast,rain,11.183333333333334,11.183333333333334,0.89,11.2056,180.0,9.9015,0.0,1006.73,Foggy in the evening.2006-04-11 05:00:00.000 +0200,Overcast,rain,10.694444444444445,10.694444444444445,0.95,10.4006,161.0,6.6976,0.0,1006.59,Foggy in the evening.2006-04-11 06:00:00.000 +0200,Mostly Cloudy,rain,11.11111111111111,11.11111111111111,0.93,12.0106,140.0,5.9731000000000005,0.0,1006.34,Foggy in the evening.2006-04-11 07:00:00.000 +0200,Mostly Cloudy,rain,11.11111111111111,11.11111111111111,0.93,9.209200000000001,103.0,10.8031,0.0,1006.09,Foggy in the evening.2006-04-11 08:00:00.000 +0200,Partly Cloudy,rain,12.166666666666666,12.166666666666666,0.82,9.9015,113.0,10.6743,0.0,1005.97,Foggy in the evening.2006-04-11 09:00:00.000 +0200,Partly Cloudy,rain,12.755555555555556,12.755555555555556,0.79,13.8299,129.0,10.8192,0.0,1005.63,Foggy in the evening.2006-04-11 10:00:00.000 +0200,Partly Cloudy,rain,13.838888888888887,13.838888888888887,0.84,9.0965,159.0,10.8192,0.0,1005.83,Foggy in the evening.2006-04-11 11:00:00.000 +0200,Partly Cloudy,rain,16.183333333333334,16.183333333333334,0.73,0.6440000000000001,207.0,10.7548,0.0,1005.97,Foggy in the evening.2006-04-11 12:00:00.000 +0200,Mostly Cloudy,rain,17.516666666666666,17.516666666666666,0.67,0.6762,153.0,10.8997,0.0,1005.64,Foggy in the evening.2006-04-11 13:00:00.000 +0200,Mostly Cloudy,rain,17.383333333333333,17.383333333333333,0.67,3.864,4.0,10.851400000000002,0.0,1005.2,Foggy in the evening.2006-04-11 14:00:00.000 +0200,Mostly Cloudy,rain,17.36111111111111,17.36111111111111,0.71,7.824600000000001,341.0,11.0768,0.0,1005.1,Foggy in the evening.2006-04-11 15:00:00.000 +0200,Mostly Cloudy,rain,17.205555555555556,17.205555555555556,0.74,15.584800000000001,15.0,10.851400000000002,0.0,1004.65,Foggy in the evening.2006-04-11 16:00:00.000 +0200,Mostly Cloudy,rain,15.633333333333333,15.633333333333333,0.81,23.6992,348.0,10.8836,0.0,1004.04,Foggy in the evening.2006-04-11 17:00:00.000 +0200,Overcast,rain,13.577777777777776,13.577777777777776,0.9,20.9622,321.0,7.615300000000001,0.0,1004.08,Foggy in the evening.2006-04-11 18:00:00.000 +0200,Foggy,rain,10.911111111111111,10.911111111111111,0.86,22.395100000000003,311.0,2.6565,0.0,1004.61,Foggy in the evening.2006-04-11 19:00:00.000 +0200,Foggy,rain,8.800000000000002,5.294444444444445,0.99,26.500600000000002,339.0,2.6565,0.0,1004.99,Foggy in the evening.2006-04-11 20:00:00.000 +0200,Overcast,rain,8.961111111111112,5.777777777777777,0.93,23.2162,340.0,3.8157000000000005,0.0,1004.85,Foggy in the evening.2006-04-11 21:00:00.000 +0200,Overcast,rain,8.199999999999998,4.605555555555555,0.96,25.4219,330.0,4.4919,0.0,1004.96,Foggy in the evening.2006-04-11 22:00:00.000 +0200,Overcast,rain,7.688888888888891,3.7222222222222237,0.96,28.1267,339.0,4.4436,0.0,1005.14,Foggy in the evening.2006-04-11 23:00:00.000 +0200,Overcast,rain,7.766666666666664,4.649999999999999,0.96,19.2878,339.0,5.6833,0.0,1005.05,Foggy in the evening.2006-04-12 00:00:00.000 +0200,Foggy,rain,8.199999999999998,5.072222222222224,0.96,20.447,341.0,3.1073,0.0,1004.8,Foggy overnight and breezy in the morning.2006-04-12 01:00:00.000 +0200,Foggy,rain,8.177777777777777,4.372222222222221,0.93,27.8691,19.0,3.2039,0.0,1004.89,Foggy overnight and breezy in the morning.2006-04-12 02:00:00.000 +0200,Mostly Cloudy,rain,7.311111111111109,6.1833333333333345,0.82,6.745900000000001,277.0,14.0553,0.0,1007.0,Foggy overnight and breezy in the morning.2006-04-12 03:00:00.000 +0200,Overcast,rain,7.644444444444442,5.038888888888889,0.93,14.9086,9.0,6.118,0.0,1004.3,Foggy overnight and breezy in the morning.2006-04-12 04:00:00.000 +0200,Overcast,rain,6.622222222222224,2.616666666666667,0.93,25.035500000000003,0.0,6.118,0.0,1003.68,Foggy overnight and breezy in the morning.2006-04-12 05:00:00.000 +0200,Overcast,rain,6.683333333333334,3.2222222222222205,0.93,19.8352,350.0,6.166300000000001,0.0,1003.86,Foggy overnight and breezy in the morning.2006-04-12 06:00:00.000 +0200,Overcast,rain,6.0888888888888895,1.6499999999999995,0.93,28.0945,349.0,7.969500000000001,0.0,1003.57,Foggy overnight and breezy in the morning.2006-04-12 07:00:00.000 +0200,Overcast,rain,6.066666666666667,1.633333333333332,0.93,27.9818,348.0,6.3434,0.0,1003.96,Foggy overnight and breezy in the morning.2006-04-12 08:00:00.000 +0200,Breezy and Mostly Cloudy,rain,6.144444444444446,1.4944444444444431,0.93,30.863700000000005,349.0,10.5455,0.0,1004.1,Foggy overnight and breezy in the morning.2006-04-12 09:00:00.000 +0200,Breezy and Mostly Cloudy,rain,7.1333333333333355,2.688888888888891,0.8,32.1678,349.0,11.2056,0.0,1004.26,Foggy overnight and breezy in the morning.2006-04-12 10:00:00.000 +0200,Overcast,rain,7.205555555555555,3.2333333333333334,0.86,26.5328,339.0,11.270000000000001,0.0,1004.18,Foggy overnight and breezy in the morning.2006-04-12 11:00:00.000 +0200,Mostly Cloudy,rain,7.566666666666666,3.544444444444446,0.84,28.336000000000006,339.0,10.5455,0.0,1004.23,Foggy overnight and breezy in the morning.2006-04-12 12:00:00.000 +0200,Mostly Cloudy,rain,8.900000000000002,5.1611111111111105,0.77,29.833300000000005,348.0,11.270000000000001,0.0,1004.47,Foggy overnight and breezy in the morning.2006-04-12 13:00:00.000 +0200,Mostly Cloudy,rain,9.961111111111112,6.644444444444445,0.71,28.480900000000005,339.0,11.270000000000001,0.0,1004.52,Foggy overnight and breezy in the morning.2006-04-12 14:00:00.000 +0200,Mostly Cloudy,rain,9.888888888888888,6.55,0.71,28.5131,338.0,11.0768,0.0,1004.29,Foggy overnight and breezy in the morning.2006-04-12 15:00:00.000 +0200,Mostly Cloudy,rain,11.066666666666668,11.066666666666668,0.71,25.695600000000002,320.0,11.270000000000001,0.0,1003.93,Foggy overnight and breezy in the morning.2006-04-12 16:00:00.000 +0200,Overcast,rain,10.116666666666667,10.116666666666667,0.76,25.3414,311.0,11.270000000000001,0.0,1004.0,Foggy overnight and breezy in the morning.2006-04-12 17:00:00.000 +0200,Overcast,rain,11.038888888888888,11.038888888888888,0.66,21.413000000000004,311.0,9.982000000000001,0.0,1004.3,Foggy overnight and breezy in the morning.2006-04-12 18:00:00.000 +0200,Overcast,rain,10.65,10.65,0.7,17.8549,310.0,14.9086,0.0,1004.0,Foggy overnight and breezy in the morning.2006-04-12 19:00:00.000 +0200,Overcast,rain,10.050000000000002,10.050000000000002,0.7,15.1984,328.0,9.982000000000001,0.0,1004.39,Foggy overnight and breezy in the morning.2006-04-12 20:00:00.000 +0200,Mostly Cloudy,rain,9.899999999999999,7.716666666666667,0.66,15.729700000000001,348.0,11.0285,0.0,1005.48,Foggy overnight and breezy in the morning.2006-04-12 21:00:00.000 +0200,Overcast,rain,8.794444444444444,6.816666666666668,0.71,12.3648,20.0,9.982000000000001,0.0,1005.89,Foggy overnight and breezy in the morning.2006-04-12 22:00:00.000 +0200,Mostly Cloudy,rain,7.827777777777779,5.405555555555554,0.72,13.894300000000003,28.0,15.826300000000002,0.0,1006.37,Foggy overnight and breezy in the morning.2006-04-12 23:00:00.000 +0200,Overcast,rain,7.855555555555556,6.122222222222224,0.72,9.8049,11.0,15.005200000000002,0.0,1006.56,Foggy overnight and breezy in the morning.2006-04-13 00:00:00.000 +0200,Mostly Cloudy,rain,7.316666666666667,6.211111111111111,0.75,6.6654,326.0,15.874600000000001,0.0,1007.07,Overcast throughout the day.2006-04-13 01:00:00.000 +0200,Overcast,rain,7.244444444444444,6.005555555555556,0.75,7.1162,309.0,15.874600000000001,0.0,1007.37,Overcast throughout the day.2006-04-13 02:00:00.000 +0200,Partly Cloudy,rain,5.438888888888888,5.438888888888888,0.88,3.7191000000000005,193.0,9.982000000000001,0.0,1012.23,Overcast throughout the day.
KMeans
class from the scikit-learn
library, which is used for clustering.pyplot
module as plt
to create visualizations.pd
for data manipulation and analysis.MinMaxScaler
from Scikit-Learn
for feature scaling.X
that contains only the columns Temperature ( C )
and Humidity
. This data is used for clustering.KMeans
object with the following parameters:
KMeans
model to the data in X
to perform clustering and determine cluster centroids.X
using the trained KMeans
model.In the output, the plot visually displays how the KMeans
algorithm groups the data into five clusters based on humidity and temperature, with each cluster represented by a different color.
Free Resources