-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather data analysis project (1).py
569 lines (320 loc) · 12.2 KB
/
weather data analysis project (1).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#!/usr/bin/env python
# coding: utf-8
# In[13]:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
# In[14]:
df_weather=pd.read_csv(r"C:\Users\VEDANSHI\Downloads\weatherHistory.csv.zip")
# In[15]:
df_weather_row_count, df_weather_column_count=df_weather.shape
print('Total number of rows:', df_weather_row_count)
print('Total number of columns:', df_weather_column_count)
# In[16]:
df_weather.info()
# In[17]:
Summary_Weather=df_weather["Summary"].value_counts().reset_index()
Summary_Weather.columns=["Weather Type","Count"]
Summary_Weather
# In[18]:
wt_missing =df_weather.isna().sum()
wt_missing
# In[19]:
import matplotlib.pyplot as plt
import seaborn as sns
# Assuming df_weather is your DataFrame
# Calculate missing values
wt_missing = df_weather.isna().sum()
# Set up Seaborn for better aesthetics
sns.set(style="whitegrid")
# Create a colorful box plot
plt.figure(figsize=(10, 6))
plt.boxplot(wt_missing, vert=False, widths=0.7, patch_artist=True, boxprops=dict(facecolor='lightblue'))
plt.yticks([1], ['Missing Values'])
plt.xlabel('Count of Missing Values', fontsize=12)
plt.title('Box Plot of Missing Values in df_weather DataFrame', fontsize=14)
plt.grid(axis='x', linestyle='--', alpha=0.7)
# Display the plot
plt.show()
# In[22]:
weatherdata=['formatted date','temperature','apparent temperature','windspeed','visibility','pressure','daily summary']
data=[96429,7574,8984,2484,949,4979,214]
plt.pie(data,labels=weatherdata)
plt.show()
# In[8]:
t_cells = np.product(df_weather.shape)
t_missing = wt_missing.sum()
percent_missing = (t_missing/t_cells) * 100
print(percent_missing)
# In[9]:
df_weather['Precip Type'].fillna(df_weather['Precip Type'].value_counts().index[0],inplace=True)
df_weather.isna().sum()
# In[10]:
df_weather.head().iloc[:5]
# In[11]:
#number of times the weather was Clear
#filtering
df_weather.head(2)
# In[12]:
#value count
df_weather.Summary.value_counts()
# In[13]:
summary_data=['Partly Cloudy','Mostly Cloudy','Overcast','Clear','Foggy']
data=[31733,28094,16597,10890,7148]
plt.pie(data,labels=summary_data)
plt.show()
# In[18]:
#data when weather was clear using filtering method
df_weather[df_weather.Summary=='Clear']
# In[14]:
#TREND IN DATA FOR TEMP,HUMIDITY ,VISIBILITY ,WIND SPEED,APPARENT TEMP
import pandas as pd
df_w1=pd.DataFrame(df_weather)
# In[17]:
import matplotlib.pyplot as plt
import seaborn as sns
# Assuming df_w1 is your DataFrame
# Increase the size of the heatmap and change color coordination
plt.figure(figsize=(10, 8))
sns.heatmap(df_w1[df_w1.columns[:8]].corr(), cmap='coolwarm', annot=True, fmt=".2f", linewidths=.5)
# Set the title
plt.title('Correlation Heatmap of the First 8 Columns in df_w1', fontsize=14)
# Display the plot
plt.show()
# In[42]:
import datetime as dt
from datetime import timedelta
# In[43]:
df_weather["Formatted Date"]=pd.to_datetime(df_weather["Formatted Date"])
# In[44]:
plt.figure(figsize=(12,7))
plt.xticks(rotation=90)
sns.barplot(data=df_weather, x="Summary", y="Temperature (C)",hue="Precip Type")
# In[29]:
#bar plot for humidty vs summary
plt.figure(figsize=(12,6))
plt.xticks(rotation=90)
sns.barplot(data=df_weather, x="Summary", y="Humidity")
# In[30]:
#rename the column name Daily Summary to Weather Condition
df_weather.head(3)
# In[46]:
df_weather.rename(columns={'Daily Summary':'Weather Condition'})
# In[47]:
#calculating the mean humidity
df_weather.head(2)
# In[48]:
import pandas as pd
# In[49]:
df_w=pd.DataFrame(df_weather)
# In[50]:
df_w.Humidity.mean()
# In[51]:
#standard Deviation of pressure
df_w.Pressure(millibars).std()
# In[31]:
#finding all the instances when the wind speed was more than 20 and visbility >15
df_weather.head(3)
# In[32]:
df_weather[(df_weather['Wind Speed (km/h)']>20)& (df_weather['Visibility (km)']>15)]
# In[9]:
#showing all records where the data is is clear and the humidity is greater than 20 or visibility greater than 15
data=pd.DataFrame(df_weather)
# In[10]:
data.head(2)
# In[11]:
data[(data['Summary']=='Clear' ) & (data['Humidity'] >20) | (data['Visibility (km)']> 15)]
# In[54]:
import pandas as pd
import matplotlib.pyplot as plt
filtered_data = data[(data['Summary'] == 'Clear') & ((data['Humidity'] > 20) | (data['Visibility (km)'] > 15))]
#you can create a scatter plot of 'Humidity' vs. 'Visibility (km)where the data points are those which meets the condn of filetered data'
plt.scatter(filtered_data['Humidity'], filtered_data['Visibility (km)'])
plt.xlabel('Humidity')
plt.ylabel('Visibility (km)')
plt.title('Scatter Plot of Humidity vs. Visibility')
plt.show()
# In[63]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Load the weather data from the CSV file
df_weather = pd.read_csv(r"C:\Users\VEDANSHI\Downloads\weatherHistory.csv.zip")
# Convert the 'Formatted Date' column to a datetime object
df_weather['Formatted Date'] = pd.to_datetime(df_weather['Formatted Date'])
# Sort the data by date
df_weather = df_weather.sort_values(by='Formatted Date')
# Smooth wind speed data (e.g., using a rolling mean) to reduce noise
df_weather['Smoothed Wind Speed'] = df_weather['Wind Speed (km/h)'].rolling(window=7, min_periods=1).mean()
# Detect anomalies in wind speed
threshold = 5.0 # Adjust this threshold as needed
df_weather['Wind Speed Anomaly'] = np.abs(df_weather['Wind Speed (km/h)'] - df_weather['Smoothed Wind Speed']) > threshold
# Plot wind speed and anomalies
plt.figure(figsize=(10, 6))
plt.plot(df_weather['Formatted Date'], df_weather['Wind Speed (km/h)'], label='Wind Speed (km/h)', color='blue')
plt.scatter(df_weather[df_weather['Wind Speed Anomaly']]['Formatted Date'],
df_weather[df_weather['Wind Speed Anomaly']]['Wind Speed (km/h)'],
color='red', marker='o', label='Anomalies')
plt.xlabel('Date')
plt.ylabel('Wind Speed (km/h)')
plt.title('Wind Speed and Anomalies')
plt.legend()
plt.grid(True)
plt.show()
# In[59]:
#To visualize the distribution of daily temperatures using histograms or kernel density plots with the provided dataset
#kernel density checks for the estimated probability density
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# on x we have value of temp as bins and on y we have freq count of temp within each bin
df_weather = pd.read_csv(r"C:\Users\VEDANSHI\Downloads\weatherHistory.csv.zip")
plt.figure(figsize=(8, 6))
sns.histplot(data=df_weather, x='Temperature (C)', bins=30, kde=True, color='red')
plt.xlabel('Temperature (C)')
plt.ylabel('Frequency')
plt.title('Distribution of Daily Temperatures')
plt.grid(True)
plt.show()
# In[21]:
import pandas as pd
from windrose import WindroseAxes
import matplotlib.pyplot as plt
wind_speed = df['Wind Speed']
wind_direction = df['Wind Direction']
# Create a wind rose chart
fig, ax = plt.subplots(subplot_kw=dict(projection="windrose"))
ax.bar(wind_direction, wind_speed, normed=True, opening=0.8, edgecolor='white')
# Customize the appearance of the wind rose chart
ax.set_legend(title='Wind Speed (m/s)')
ax.set_title('Wind Rose Chart')
# Display the plot
plt.show()
# In[22]:
pip install windrose
# In[35]:
import pandas as pd
from windrose import WindroseAxes
import matplotlib.pyplot as plt
import seaborn as sns
# Assuming df_weather is your DataFrame
wind_speed = df_weather['Wind Speed (km/h)']
wind_direction = df_weather['Wind Bearing (degrees)']
# Set a different color palette (e.g., "viridis")
sns.set_palette("plasma")
# Create a wind rose chart
fig, ax = plt.subplots(subplot_kw=dict(projection="windrose"))
ax.bar(wind_direction, wind_speed, normed=True, opening=0.8, edgecolor='white')
# Customize the appearance of the wind rose chart
ax.set_legend(title='Wind Speed (km/h)', loc='upper left', bbox_to_anchor=(1, 1))
ax.set_title('Wind Rose Chart', fontsize=16, fontweight='bold')
# Add a grid for better readability
ax.grid(linestyle='--', alpha=0.7)
# Display the plot
plt.show()
# In[7]:
import matplotlib.pyplot as plt
import seaborn as sns
# Set the color palette for the plot
sns.set_palette("viridis")
# Create a bar plot with improved aesthetics
plt.figure(figsize=(12, 8))
sns.barplot(data=df_weather, y="Summary", x="Humidity", ci=None)
# Add labels and title
plt.xlabel("Humidity")
plt.ylabel("Weather Summary")
plt.title("Humidity Distribution by Weather Summary")
# Rotate y-axis labels for better readability
plt.xticks(rotation=45)
# Display the plot
plt.show()
# In[12]:
import pandas as pd
import matplotlib.pyplot as plt
# Filter the data
filtered_data = data[(data['Summary'] == 'Clear') & ((data['Humidity'] > 20) | (data['Visibility (km)'] > 15))]
# Create a scatter plot with improved aesthetics
plt.figure(figsize=(10, 6))
plt.scatter(filtered_data['Humidity'], filtered_data['Visibility (km)'], c='blue', alpha=0.6, s=50, edgecolors='w')
# Add labels and title
plt.xlabel('Humidity')
plt.ylabel('Visibility (km)')
plt.title('Scatter Plot of Humidity vs. Visibility for Clear Weather')
# Adjust plot appearance
plt.grid(True, linestyle='--', alpha=0.7)
plt.legend(['Clear Weather'], loc='upper right')
plt.tight_layout()
# Display the plot
plt.show()
# In[1]:
pip install pandas wordcloud matplotlib
# In[3]:
#Create a word cloud or bar chart to display the most frequent terms or phrases in the "Daily Summary" column.
#This can provide a quick overview of common weather patterns described in the dataset.
import pandas as pd
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# Load the CSV file into a pandas DataFrame
# Replace with the actual file path
df = pd.read_csv(r"C:\Users\VEDANSHI\Downloads\weatherHistory.csv.zip")
# Specify the column for which you want to create the word cloud
column_name = 'Summary' # Replace with the actual column name
# Combine all the text in the specified column
text = ' '.join(df[column_name].astype(str))
# Create the word cloud
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)
# Display the word cloud using matplotlib
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
# In[7]:
import pandas as pd
import matplotlib.pyplot as plt
# Replace with the actual file path
df = pd.read_csv(r"C:\Users\VEDANSHI\Downloads\weatherHistory.csv.zip")
# Specify the columns for the hexbin plot
temperature_column = 'Temperature (C)' # Replace with the actual column name
apparent_temperature_column = 'Apparent Temperature (C)' # Replace with the actual column name
# Create a hexbin plot
plt.figure(figsize=(10, 8))
plt.hexbin(df[temperature_column], df[apparent_temperature_column], gridsize=50, cmap='viridis')
plt.xlabel('Temperature (C)')
plt.ylabel('Apparent Temperature (C)')
plt.title('Hexbin Plot: Temperature vs. Apparent Temperature')
plt.colorbar(label='Count')
plt.tight_layout()
# Show the plot
plt.show()
# In[ ]:
#. This type of plot provides a smoothed representation of the data density, allowing you to visualize areas of high and low correlation
#highelights the high correlation for the above code
# In[ ]:
#Visibility is an essential weather parameter. Analyzing its distribution helps meteorologists understand typical visibility patterns, detect anomalies, and predict potential weather-related events
# In[3]:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Load the CSV file into a pandas DataFrame
# Replace with the actual file path
df = pd.read_csv(r"C:\Users\VEDANSHI\Downloads\weatherHistory.csv.zip")
# Specify the column for visibility
visibility_column = 'Visibility (km)' # Replace with the actual column name
# Create a histogram for visibility distribution
plt.figure(figsize=(10, 6))
sns.histplot(df[visibility_column], bins=20, kde=True, color='skyblue')
plt.xlabel('Visibility (km)')
plt.ylabel('Frequency')
plt.title('Visibility Distribution')
plt.tight_layout()
# Find the bin with the maximum frequency
bin_edges, bin_heights, _ = plt.hist(df[visibility_column], bins=20, alpha=0) # Use alpha=0 to hide the plotted histogram
max_bin_index = bin_heights.argmax()
max_bin_value = bin_edges[max_bin_index], bin_edges[max_bin_index + 1]
print(f"The bin with the maximum frequency is between {max_bin_value[0]} and {max_bin_value[1]} km.")
# Show the plot
plt.show()
# In[ ]: