Showing posts with label prediction. Show all posts
Showing posts with label prediction. Show all posts

Tuesday

Predictive Maintenance Using Machine Learning

Context: A manufacturing company wants to predict when equipment is likely to fail, so they can schedule maintenance and reduce downtime.

Dataset: The company collects data on equipment sensor readings, maintenance records, and failure events.

Libraries:

pandas for data manipulation

numpy for numerical computations

scikit-learn for machine learning

matplotlib and seaborn for visualization

Code:


# Import libraries

import pandas as pd

import numpy as np

from sklearn.model_selection import train_test_split

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score, classification_report

import matplotlib.pyplot as plt

import seaborn as sns


# Load dataset

df = pd.read_csv('equipment_data.csv')


# Preprocess data

df['failure'] = df['failure'].map({'yes': 1, 'no': 0})

X = df.drop(['failure'], axis=1)

y = df['failure']


# Split data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)


# Train random forest classifier

rfc = RandomForestClassifier(n_estimators=100, random_state=42)

rfc.fit(X_train, y_train)


# Make predictions

y_pred = rfc.predict(X_test)


# Evaluate model

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

print("Classification Report:")

print(classification_report(y_test, y_pred))


# Visualize feature importance

feature_importance = rfc.feature_importances_

plt.figure(figsize=(10, 6))

sns.barplot(x=X.columns, y=feature_importance)

plt.title("Feature Importance")

plt.show()


# Use the model for predictive maintenance

new_data = pd.DataFrame({'sensor1': [10], 'sensor2': [20], 'sensor3': [30]})

prediction = rfc.predict(new_data)

print("Prediction:", prediction)


Explanation:

Load the dataset and preprocess it by converting the 'failure' column to binary (0/1).

Split the data into training and testing sets.

Train a random forest classifier on the training data.

Make predictions on the testing data and evaluate the model's accuracy.

Visualize the feature importance to understand which sensors are most predictive of failure.

Use the trained model to make predictions on new, unseen data.

You can get the predictive maintenance dataset from Kaggle

If you want to learn real-life use cases of AI, ML, DL and GenAI then can contact me. 

Saturday

SARIMA vs ARIMA for Timeseries Analysis Model

 


For predicting one particular day's weather from a previous year's long weather data, a SARIMA model is generally better than an ARIMA model. This is because SARIMA models can account for seasonality in the data, while ARIMA models cannot.

Seasonality is a regular pattern in the data that repeats over a fixed period of time. For example, temperature data exhibits seasonality, with higher temperatures in the summer and lower temperatures in the winter.

SARIMA models can account for seasonality by including additional parameters that model the seasonal component of the data. This allows SARIMA models to make more accurate predictions for seasonal data, such as weather data.

ARIMA models, on the other hand, cannot account for seasonality. This means that they may not be as accurate for predicting seasonal data as SARIMA models.

However, it is important to note that both SARIMA and ARIMA models are statistical models, and they are both subject to error. The accuracy of any forecasting model will depend on the quality of the data and the complexity of the relationships in the data.

In some cases, an ARIMA model may be more accurate than a SARIMA model for predicting one particular day's weather. This is because ARIMA models are simpler and easier to fit into the data. Additionally, ARIMA models may be more accurate for predicting short-term weather patterns.

However, in general, SARIMA models are better suited for predicting seasonal data. If you are trying to predict one particular day's weather from a previous year's long weather data, a SARIMA model is likely to be the best choice.

Here are some additional things to consider when choosing between an ARIMA and SARIMA model:

  • Seasonality: If your data exhibits seasonality, then a SARIMA model is generally the better choice. ARIMA models cannot account for seasonality, so they may not be as accurate for predicting seasonal data.
  • Data quality: ARIMA and SARIMA models are both statistical models, and they are both subject to error. The accuracy of any forecasting model will depend on the quality of the data. If your data is noisy or incomplete, then a more complex model, such as a SARIMA model, may not be able to improve your predictions.
  • Model complexity: ARIMA models are simpler than SARIMA models. This can make them easier to fit into the data and can also make them more accurate for short-term predictions. However, ARIMA models may not be as accurate for predicting seasonal data or long-term trends.

Ultimately, the best way to choose between an ARIMA and SARIMA model is to experiment with both models and see which model produces the most accurate predictions for your data.

Examples where ARIMA is best:

  • Predicting short-term trends, such as the number of customers who will visit a store on a given day.
  • Predicting non-seasonal data, such as the price of a stock or the GDP of a country.
  • Forecasting for data that is noisy or incomplete.
  • Forecasting when the model complexity needs to be low.

Examples where SARIMA is best:

  • Predicting seasonal data, such as the temperature or the number of tourists who will visit a destination during a particular month.
  • Forecasting for data that is complete and of high quality.
  • Forecasting when the model complexity is not a major concern.

Here are some specific examples:

  • ARIMA: Predicting the number of daily active users on a social media platform. Forecasting the sales of a product that is not affected by seasonality, such as milk or bread. Predicting the price of a stock that has a relatively stable trend.
  • SARIMA: Predicting the temperature on a particular day during the year. Forecasting the number of tourists who will visit a beach destination during the summer months. Predicting the demand for electricity during the winter months.

It is important to note that these are just examples, and the best model for a particular forecasting task will depend on the specific data and the desired outcome. It is always a good idea to experiment with both ARIMA and SARIMA models to see which model produces the most accurate predictions for your data.

Photo by Moose