Talk:Pyronema
This article is rated Stub-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | |||||||||||
|
q7
[edit]import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from sklearn.linear_model import LinearRegression import numpy as np
- Filter data for Oregon
oregon_data = df[df["state"] == "Oregon"]
- Visualize the relationship between mean_avg and bad_mental_health_days
plt.figure(figsize=(10, 6)) sns.scatterplot(x="mean_avg", y="bad_mental_health_days", data=oregon_data, color="blue", s=100) sns.regplot(x="mean_avg", y="bad_mental_health_days", data=oregon_data, scatter=False, color="red") plt.title("Relationship Between PM2.5 (mean_avg) and Mental Health in Oregon", fontsize=16) plt.xlabel("PM2.5 Levels (mean_avg)", fontsize=14) plt.ylabel("Bad Mental Health Days", fontsize=14) plt.grid(True) plt.show()
- Fit a linear regression model
X = oregon_data["mean_avg"].values.reshape(-1, 1) # Independent variable y = oregon_data["bad_mental_health_days"].values # Dependent variable model = LinearRegression() model.fit(X, y)
- Display the regression results
print(f"Model Coefficient (Slope): {model.coef_[0]:.3f}") print(f"Model Intercept: {model.intercept_:.3f}") print(f"R-squared: {model.score(X, y):.3f}")
- Hypothesis testing: Permutation test
observed_slope = model.coef_[0] permuted_slopes = []
- Shuffle mean_avg values and compute slopes
n_permutations = 1000 for _ in range(n_permutations):
shuffled_mean_avg = np.random.permutation(oregon_data["mean_avg"]) perm_model = LinearRegression().fit(shuffled_mean_avg.reshape(-1, 1), y) permuted_slopes.append(perm_model.coef_[0])
- Compute p-value
p_value = np.mean(np.abs(permuted_slopes) >= np.abs(observed_slope)) print(f"P-value from permutation test: {p_value:.3f}")
- Plot the permutation distribution
plt.figure(figsize=(10, 6)) plt.hist(permuted_slopes, bins=30, color="skyblue", edgecolor="black", alpha=0.7) plt.axvline(observed_slope, color="red", linestyle="--", linewidth=2, label="Observed Slope") plt.title("Permutation Test for PM2.5 Effect on Mental Health", fontsize=16) plt.xlabel("Slope from Permutation", fontsize=14) plt.ylabel("Frequency", fontsize=14) plt.legend() plt.grid(True) plt.show() 2601:646:A101:E40:EDFB:7607:3A97:6201 (talk) 01:34, 7 December 2024 (UTC)
q8
[edit]import numpy as np
def optimize_child_health(play_start_hour, play_duration, air_quality, lung_health, outdoor_benefit):
""" Objective function to optimize children's health based on outdoor activity and air quality.
Parameters: - play_start_hour (int): Hour of the day when outdoor play begins (7 <= hour <= 19). - play_duration (float): Duration of outdoor play in hours (0 <= duration <= 2). - air_quality (function): A function that takes an hour and returns air quality index (AQI). - lung_health (function): A function that computes the impact of air quality on lung health. Signature: lung_health(exposure_duration, air_quality). - outdoor_benefit (function): A function that computes the health benefit of outdoor play. Signature: outdoor_benefit(duration).
Returns: - Total health score (float): A value where higher is better for the child’s health. """ # Constants MIN_PLAY_HOUR = 7 # 7 AM MAX_PLAY_HOUR = 19 # 7 PM MAX_PLAY_DURATION = 2 # Maximum outdoor play time in hours
# Check constraints if not (MIN_PLAY_HOUR <= play_start_hour <= MAX_PLAY_HOUR): raise ValueError("Play start hour must be between 7 AM and 7 PM.") if not (0 <= play_duration <= MAX_PLAY_DURATION): raise ValueError("Play duration must be between 0 and 2 hours.")
# Calculate the average air quality during playtime play_hours = np.arange(play_start_hour, play_start_hour + play_duration, 0.25) play_hours = play_hours[play_hours <= MAX_PLAY_HOUR] # Limit to allowable hours avg_air_quality = np.mean([air_quality(hour) for hour in play_hours])
# Calculate lung health impact based on air quality lung_health_impact = lung_health(play_duration, avg_air_quality)
# Calculate outdoor activity benefits activity_benefit = outdoor_benefit(play_duration)
# Total health score: maximize benefits while minimizing harm total_health_score = activity_benefit - lung_health_impact
return total_health_score
(!!You don't need to include the code below)
- Example utility functions
def air_quality(hour):
"""Example function returning AQI for a given hour.""" return 50 + 10 * np.sin((hour - 12) / 4) # Simulates better air in morning/evening
def lung_health(exposure_duration, avg_air_quality):
"""Example function modeling the impact of air quality on lung health.""" return exposure_duration * avg_air_quality / 100 # Higher AQI worsens lung health
def outdoor_benefit(duration):
"""Example function modeling the benefit of outdoor play.""" return duration * (2 - 0.5 * duration) # Benefits diminish with overplay
- Example optimization scenario
play_start_hour = 10 play_duration = 1.5
health_score = optimize_child_health(play_start_hour, play_duration, air_quality, lung_health, outdoor_benefit) print(f"Health Score for play start at {play_start_hour} and duration {play_duration} hours: {health_score:.2f}") 2601:646:A101:E40:EDFB:7607:3A97:6201 (talk) 01:39, 7 December 2024 (UTC)