Skip to content

Commit

Permalink
one day data, scatter and predictive
Browse files Browse the repository at this point in the history
  • Loading branch information
hannhegg committed Apr 8, 2025
1 parent eed8157 commit 100dae4
Showing 1 changed file with 144 additions and 12 deletions.
156 changes: 144 additions & 12 deletions notebooks/notebook_one_day_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
"### Viser temperaturen\n",
"Regner ut gjennomsnittst-temperatur ved hjelp av innebygde funksjoner. Finner også høyeste og laveste målte temperatur.\n",
"\n",
"Plotter temperaturen ved hjelp av matplotlib."
"Plotter gjennomsnittstemperaturen og temperaturen per time for dagen valgt, ved hjelp av matplotlib."
]
},
{
Expand All @@ -220,36 +220,43 @@
"print(\"Highest temperature:\", max_temp)\n",
"print(\"Lowest temperature:\", min_temp)\n",
"\n",
"# Set the x_axis to the index, which means the time\n",
"\n",
"# Set the x_axis to the index, which represents the time\n",
"x_axis = df.index\n",
"\n",
"# Choose the width and height of the plot\n",
"plt.figure(figsize=(12, 6))\n",
"\n",
"# Plotting temperatur\n",
"plt.plot(x_axis, temp, color='tab:red', label='Temperatur')\n",
"# Scatter plot for each temperature reading\n",
"plt.scatter(x_axis, temp, color='tab:green', label='Temperaturmålinger', alpha=0.6)\n",
"\n",
"# Get the current axsis, and store it as ax\n",
"# Add a horizontal line for the mean temperature\n",
"plt.axhline(y=temp_mean, color='green', linestyle='--', label=f'Gj.snitt {temp_mean}°C')\n",
"\n",
"# Get the current axis and store it as ax\n",
"ax = plt.gca()\n",
"\n",
"# Customize the x-axis to show ticks for each hour\n",
"ax.xaxis.set_major_locator(mdates.HourLocator(interval=1)) # Tick marks for every hour\n",
"ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) # Format as \"Day Month Hour:Minute\"\n",
"ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) # Format as \"Hour:Minute\"\n",
"\n",
"# Rotate x-axis labels for better readability\n",
"plt.xticks(rotation=45)\n",
"\n",
"# Adjust layout\n",
"plt.tight_layout()\n",
"\n",
"# Add title for the plot, with city_name and start to end date\n",
"# Add title for the plot\n",
"plt.title(f'Temperatur {city_name}, ({date})')\n",
"\n",
"# Shows a grid\n",
"# Show grid\n",
"plt.grid()\n",
"\n",
"# Show the label-description\n",
"plt.legend(loc = 'upper right')\n",
"# Show legend\n",
"plt.legend(loc='upper right')\n",
"\n",
"# Show the plot\n",
"plt.show()"
"plt.show()\n"
]
},
{
Expand Down Expand Up @@ -563,6 +570,131 @@
"# Show the plot\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lineær regresjons og prediktiv analyse\n",
"\n",
"lager en lineær regresjon og bruker den til å finne temperaturer vi kan forvente å få samme tid, neste år.\n",
"Bruker scikit-learn biblioteket, sklearn.linear_model, LinearRegression for å få til dette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.linear_model import LinearRegression\n",
"\n",
"#Hours of the day (0 to 23) and temperatures\n",
"data = {\n",
" 'Hour': np.arange(24), # 24 hours of the day\n",
" 'Temperature': temp # get the temperatures\n",
"}\n",
"\n",
"# Create a DataFrame\n",
"df = pd.DataFrame(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### klargjøre data til regresjonsmodellen"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Reshape the data (features should be 2D for sklearn)\n",
"X = df['Hour'].values.reshape(-1, 1) # Hours of the day (x-axis)\n",
"y = df['Temperature'].values # Temperatures (y-axis)\n",
"\n",
"# Create and train the regression model\n",
"model = LinearRegression()\n",
"model.fit(X, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### trene modellen og predikerte fremtidig data\n",
"\n",
"trene modellen og lager en graf som viser den originale dataen fra årets dag brukeren har skrevet inn og regresjons modellen\n",
"\n",
"bruker den trente modellen til å lage predikasjoner for temperaturen samme dag, neste år"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib.dates import DateFormatter\n",
"\n",
"y = np.array(df['Temperature'].values)\n",
"\n",
"x_axis = df.index\n",
"\n",
"plt.figure(figsize=(12, 6))\n",
"\n",
"# Plot the original data\n",
"plt.scatter(x_axis, y, color='green', label='Original data', alpha=0.6)\n",
"\n",
"predicted_temperatures = model.predict(X)\n",
"\n",
"# Plot the regression line\n",
"plt.plot(x_axis, predicted_temperatures, color='red', label='Prediktiv temperatur')\n",
"\n",
"#title for the axis\n",
"plt.xlabel('Datetime')\n",
"plt.ylabel('Temperature (°C)')\n",
"\n",
"#ax = plt.gca()\n",
"\n",
"# Customize the x-axis to show ticks for each hour\n",
"plt.xticks(rotation=45)\n",
"plt.gca().xaxis.set_major_formatter(DateFormatter('%H:%M'))\n",
"\n",
"plt.tight_layout()\n",
"\n",
"#Title for the plot\n",
"plt.title(f'Temperature {city_name}')\n",
"\n",
"plt.grid(axis = 'x')\n",
"\n",
"plt.legend(loc='upper right')\n",
"\n",
"plt.show()\n",
"\n",
"# Display the predicted temperatures\n",
"print(f'predicted temperatures: {predicted_temperatures}')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -581,7 +713,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 100dae4

Please sign in to comment.