SoFunction
Updated on 2024-11-16

Python Using Machine Learning Models for Temperature Prediction Explained

With Python you can use machine learning models for temperature prediction. Commonly used models are regression analysis, random forest, etc. Before using them, you need to prepare enough historical data and perform feature engineering, build the model and train it, and finally use the prediction results.

Temperature prediction Regression analysis

The following code uses a linear regression algorithm to predict temperature data:

import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

# Read temperature data
data = pd.read_csv('temperature_data.csv')

# Separate features and labels
X = data[['day_of_year', 'year']]
y = data['temperature']

# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Training models
reg = LinearRegression().fit(X_train, y_train)

# Projected results
y_pred = (X_test)

# Assessment models
score = (X_test, y_test)
print('R2 score: ', score)

Import the necessary libraries:

  • import pandas as pd: Used to read CSV files and process data.
  • import numpy as np: Used to perform numerical operations.
  • from sklearn.linear_model import LinearRegression: Import a linear regression model from the scikit-learn library.
  • from sklearn.model_selection import train_test_split: Import the data segmentation function from the scikit-learn library.

Read the temperature data:

data = pd.read_csv('temperature_data.csv'): Use pandas to read a CSV file and save it to the data variable.

Separate features and labels:

  • X = data[['day_of_year', 'year']]: Store the day_of_year and year columns of the temperature data as features in the X variable.
  • y = data['temperature']: Stores the temperature column of the temperature data as a label in the y variable.

Split the dataset:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2): Use the train_test_split() function to split the data into a training set and a test set, where the test set size is 20%.

Training models:

reg = LinearRegression().fit(X_train, y_train): Train a linear regression model using the training set data and save it to the reg variable.

Predicted results:

y_pred = (X_test): Predict the results using the test set data and save them to the y_pred variable.

Assessment:

print('R-squared:', (X_test, y_test)): The predictive accuracy of the model is assessed using the R-squared value, where the closer the value is to 1, the higher the predictive accuracy of the model.

The temperature_data.csv file is a CSV file of temperature data and may contain the following fields:

day_of_year,year,temperature
1,2021,20.5
2,2021,21.6
3,2021,22.7
365,2021,19.4
1,2022,18.5
2,2022,19.6

The day_of_year column indicates the day of the year, the year column indicates the year of the day, and the temperature column indicates the temperature of the day.

Running the code yields the following screenshot.

Temperature Prediction Random Forest Python Writing

Here is the Python code for the random forest regression:

import pandas as pd
from  import RandomForestRegressor
from sklearn.model_selection import train_test_split

# Read temperature data
data = pd.read_csv('temperature_data.csv')

# Separate features and labels
X = data[['day_of_year', 'year']]
y = data['temperature']

# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Training models
reg = RandomForestRegressor(n_estimators=100).fit(X_train, y_train)

# Projected results
y_pred = (X_test)

# Assessment models
print('R-squared:', (X_test, y_test))

The code description is as follows:

  • import pandas as pd : Import the pandas library.
  • from import RandomForestRegressor : Introducing the Random Forest regression algorithm.
  • from sklearn.model_selection import train_test_split : Import the dataset segmentation tool.
  • data = pd.read_csv('temperature_data.csv') : Reads temperature data.
  • X = data[['day_of_year', 'year']] : Extract feature data (features: day and year of the year).
  • y = data['temperature'] : Extracts labeled data (label: temperature).
  • X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) : The dataset is divided into a training set and a test set, where the test set has 20% of the data.
  • reg = RandomForestRegressor(n_estimators=100).fit(X_train, y_train) : Train the model using the random forest regression algorithm.
  • y_pred = (X_test) : Predictions are made on test set data using the trained model.
  • print('R-squared:', (X_test, y_test)): The predictive accuracy of the model is assessed using the R-squared value, where the closer the value is to 1, the higher the predictive accuracy of the model.

Code run results:

To this point this article on Python using machine learning models to achieve temperature prediction detailed article is introduced to this, more related Python temperature prediction content, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!