List of Topics:
Location Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Build a Regression Model Using Artificial Neural Network (ANN) for Monthly Sales Revenue Prediction?

Regression Model Using ANN

Condition for Building a Regression Model Using Artificial Neural Network (ANN) for Monthly Sales Revenue Prediction.

  • Description:
    This code builds a regression model using an Artificial Neural Network (ANN) to predict monthly sales revenue. It preprocesses the data by encoding categorical variables, handling missing values, and scaling the features. The model is trained using the training dataset, and its performance is evaluated using metrics like Mean Squared Error (MSE), Root Mean Squared Error (RMSE), Mean Absolute Error (MAE), and R-squared (R2 Score).
Step-by-Step Process
  • Import Libraries:
    Import necessary libraries like pandas for data handling, sklearn for preprocessing, and Keras for building the ANN model.
  • Read Dataset:
    Load the sales data from the provided CSV file using pd.read_csv().
  • Handle Missing Values:
    Check and handle any missing or NaN values in the dataset.
  • Encode Categorical Data:
    Convert categorical features into numeric values using LabelEncoder().
  • Scale Features:
    Apply MinMaxScaler and StandardScaler for scaling the target and independent variables, respectively.
  • Split Dataset:
    Split the dataset into training and testing sets using train_test_split().
  • Build and Train Model:
    Train the ANN model using the training data and validate it on the test set.
  • Evaluate Model:
    Evaluate the model using regression metrics such as MSE, RMSE, MAE, and R-squared.
Sample Source Code
  • # Import Necessary Libraries
    import pandas as pd
    from sklearn.preprocessing import LabelEncoder, StandardScaler, MinMaxScaler
    from sklearn.model_selection import train_test_split
    from tensorflow.keras.layers import Input, Dense
    from tensorflow.keras.models import Model

    # Suppress Warnings
    import warnings
    warnings.filterwarnings("ignore")

    # Load Dataset
    df = pd.read_csv("/home/soft12/Downloads/sample_dataset/Website/Dataset/Store_CA.csv")

    # Print Column Names
    print("Columns")
    print(df.columns)

    # Check Missing Values
    print('\nChecking Missing Values\n')
    print(df.isnull().sum())

    # Check NaN Values
    print('\nChecking NaN Values\n')
    print(df.isna().sum())

    # Encode Categorical Features
    label = LabelEncoder()
    for i in df.columns:
    if df[i].dtypes == object:
    df[i] = label.fit_transform(df[i])

    # Split Dependent and Independent Variables
    x = df.drop('MonthlySalesRevenue', axis=1)
    y = df['MonthlySalesRevenue']

    # Scale Target Variable
    scaler_y = MinMaxScaler()
    y = scaler_y.fit_transform(y.values.reshape(-1, 1))

    # Scale Features
    scaler = StandardScaler()
    x = scaler.fit_transform(x)

    # Split Dataset
    X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)

    # Define ANN Model
    def ANN_model_regression(input_shape):
    inputs = Input(shape=(input_shape,))
    layer1 = Dense(64, activation='relu')(inputs)
    layer2 = Dense(32, activation='relu')(layer1)
    output_layer = Dense(1, activation='linear')(layer2)
    ann_model = Model(inputs=inputs, outputs=output_layer)
    ann_model.compile(optimizer='adam', loss='mse', metrics=['mean_squared_error'])
    return ann_model

    # Initialize and Train Model
    model = ANN_model_regression(X_train.shape[1])
    model.summary()
    model.fit(X_train, y_train, batch_size=2, epochs=10, validation_data=(X_test, y_test))

    # Predict and Evaluate
    y_pred = model.predict(X_test).ravel()

    from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
    print("___Performance Metrics___\n")
    print('Mean Squared Error (MSE): ', mean_squared_error(y_test, y_pred))
    print('Root Mean Squared Error (RMSE):', mean_squared_error(y_test, y_pred, squared=False))
    print('Mean Absolute Error (MAE): ', mean_absolute_error(y_test, y_pred))
    print('R-squared (R2 Score): ', r2_score(y_test, y_pred))
Screenshots
  • ANN Output Screenshot