-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_science.py
More file actions
58 lines (52 loc) · 1.5 KB
/
data_science.py
File metadata and controls
58 lines (52 loc) · 1.5 KB
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
from __future__ import annotations
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
from matplotlib.axes import Axes
from matplotlib.figure import Figure
# 1) Load data
df: pd.DataFrame = pd.read_csv("data/data.csv", parse_dates=["date"])
# 2) Clean / prep
df["date"] = pd.to_datetime(df["date"])
df["weekday"] = df["date"].dt.day_name()
df["is_weekend"] = df["date"].dt.weekday >= 5
df = df.dropna() # or fillna
# 3) Basic stats
stats: pd.DataFrame = df[["temp_c", "humidity"]].describe()
corr_value: float = float(df["temp_c"].corr(df["humidity"]))
print(stats)
print("Correlation:", corr_value)
# 4) Line plot over time
sns.set_theme()
fig: Figure
ax1: Axes
fig, ax1 = plt.subplots(figsize=(10, 4))
sns.lineplot(data=df, x="date", y="temp_c", ax=ax1, label="Temp (C)", color="tomato")
ax2: Axes = ax1.twinx()
sns.lineplot(
data=df, x="date", y="humidity", ax=ax2, label="Humidity", color="royalblue"
)
ax1.set_ylabel("Temp (C)")
ax2.set_ylabel("Humidity (%)")
plt.title("Daily Temp vs Humidity")
fig.tight_layout()
plt.show()
# 5) Scatter with hue by weekend (and trendline)
scatter_fig: Figure
scatter_ax: Axes
scatter_fig, scatter_ax = plt.subplots(figsize=(6, 5))
sns.scatterplot(
data=df,
x="temp_c",
y="humidity",
hue="is_weekend",
palette=["gray", "orange"],
ax=scatter_ax,
)
sns.regplot(
data=df, x="temp_c", y="humidity", scatter=False, color="black", ax=scatter_ax
)
plt.title("Temp vs Humidity")
plt.show()
# 6) Brief notes
print("Observations: ...")