Son Yazılar

Developing An Income Prediction Application in Python Using Machine Learning Algorithms and PyQt5

 




I developed an application that utilizes a simple machine learning algorithm. The dataset I used was sourced from the UCI Machine Learning Repository. To make the application more user-friendly, I integrated PyQt5 to build a graphical user interface (GUI). With this project, users can both view the results of predictions and input new values to generate forecasts.


      





The primary goal of the project was to create an income prediction tool. The dataset, known as the Adult Dataset, includes features such as age, educational background, and working hours. I applied the Decision Tree Algorithm to predict whether an individual’s income is over $50K or not based on these features. During this phase, I performed several key steps to prepare the data and train the model:

1- Data preprocessing

2- Model training and testing

3- Performance evaluation


Step 1: The Machine Learning Model (main.py)
The machine learning model, data processing, and logic for prediction are placed in the main.py file. This separation keeps the user interface code clean and allows for better maintenance and flexibility. Here's the full code for main.py:


import sys
from PyQt5.QtWidgets import QApplication
from gui import MainWindow
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score


def load_and_process_data():
    df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data", header=None)
    df.columns = ["age", "workclass", "fnlwgt", "education", "education-num", "marital-status", "occupation",
                  "relationship", "race", "sex", "capital-gain", "capital-loss", "hours-per-week", "native-country", "income"]

    X = df.drop("income", axis=1)
    y = df["income"]

    
    X = pd.get_dummies(X)
    return X, y


def train_and_evaluate_model():
    X, y = load_and_process_data()

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

  
    clf = DecisionTreeClassifier(random_state=42)
    clf.fit(X_train, y_train)

    
    y_pred = clf.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)

    return accuracy


def predict_income(age, hours_per_week):
    
    df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data", header=None)
    df.columns = ["age", "workclass", "fnlwgt", "education", "education-num", "marital-status", "occupation",
                  "relationship", "race", "sex", "capital-gain", "capital-loss", "hours-per-week", "native-country", "income"]

    X = df[["age", "hours-per-week"]]
    y = df["income"]

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

    
    clf = DecisionTreeClassifier(random_state=42)
    clf.fit(X_train, y_train)

    
    prediction = clf.predict([[age, hours_per_week]])

    return prediction[0]

def main():
    app = QApplication(sys.argv)
    window = MainWindow(train_and_evaluate_model, predict_income)
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()


Step 2: The Graphical User Interface (gui.py)

In the second part of the project, I designed a user interface to allow users to interact with the model through buttons rather than relying solely on the console. The main menu contains two options: “Test the Model Accuracy” and “Predict the Income”. The entire user interface is defined in the gui.py file.

Here’s the code for gui.py:


from PyQt5.QtWidgets import QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QLineEdit, QStackedWidget

class MainWindow(QMainWindow):
    def __init__(self, accuracy_function, prediction_function):
        super().__init__()

        self.accuracy_function = accuracy_function
        self.prediction_function = prediction_function

        self.setWindowTitle('Income Prediction Application')
        self.setGeometry(100, 100, 400, 300)

        
        self.stacked_widget = QStackedWidget()
        self.setCentralWidget(self.stacked_widget)

  
        self.main_menu_widget = QWidget()
        self.layout = QVBoxLayout()

        
        self.accuracy_button = QPushButton('Test Model Accuracy')
        self.accuracy_button.clicked.connect(self.show_accuracy_screen)
        self.layout.addWidget(self.accuracy_button)

        
        self.predict_button = QPushButton('Predict Income')
        self.predict_button.clicked.connect(self.show_prediction_screen)
        self.layout.addWidget(self.predict_button)

        self.main_menu_widget.setLayout(self.layout)
        self.stacked_widget.addWidget(self.main_menu_widget)

    def show_accuracy_screen(self):
        
        self.accuracy_widget = AccuracyWidget(self, self.accuracy_function)
        self.stacked_widget.addWidget(self.accuracy_widget)
        self.stacked_widget.setCurrentWidget(self.accuracy_widget)

    def show_prediction_screen(self):
        
        self.prediction_widget = PredictionWidget(self, self.prediction_function)
        self.stacked_widget.addWidget(self.prediction_widget)
        self.stacked_widget.setCurrentWidget(self.prediction_widget)

    def show_main_menu(self):
        
        self.stacked_widget.setCurrentWidget(self.main_menu_widget)


class AccuracyWidget(QWidget):
    def __init__(self, parent, accuracy_function):
        super().__init__()

        self.parent = parent
        self.accuracy_function = accuracy_function
        layout = QVBoxLayout()

        
        self.result_label = QLabel('Calculating model accuracy...')
        layout.addWidget(self.result_label)

        
        self.back_button = QPushButton('Back')
        self.back_button.clicked.connect(self.back_to_main_menu)
        layout.addWidget(self.back_button)

        self.setLayout(layout)

        
        self.show_accuracy()

    def show_accuracy(self):
        accuracy = self.accuracy_function()
        self.result_label.setText(f'Model Accuracy: {accuracy*100:.2f}%')

    def back_to_main_menu(self):
        
        self.parent.show_main_menu()


class PredictionWidget(QWidget):
    def __init__(self, parent, prediction_function):
        super().__init__()

        self.parent = parent
        self.prediction_function = prediction_function
        layout = QVBoxLayout()

        
        self.input_age = QLineEdit(self)
        self.input_age.setPlaceholderText("Age")
        layout.addWidget(self.input_age)

        self.input_hours_per_week = QLineEdit(self)
        self.input_hours_per_week.setPlaceholderText("Hours Per Week")
        layout.addWidget(self.input_hours_per_week)

        
        self.predict_button = QPushButton('Predict Income')
        self.predict_button.clicked.connect(self.run_prediction)
        layout.addWidget(self.predict_button)

       
        self.result_label = QLabel('Prediction result will be displayed here')
        layout.addWidget(self.result_label)

        
        self.back_button = QPushButton('Back')
        self.back_button.clicked.connect(self.back_to_main_menu)
        layout.addWidget(self.back_button)

        self.setLayout(layout)

    def run_prediction(self):
        
        age = int(self.input_age.text())
        hours_per_week = int(self.input_hours_per_week.text())

        
        prediction = self.prediction_function(age, hours_per_week)
        self.result_label.setText(f'Predicted Income: {prediction}')

    def back_to_main_menu(self):
        
        self.parent.show_main_menu()


Step 3: Interacting with the Application

When the user clicks on “Test the Model Accuracy,” the application displays the accuracy of the trained model. This feature helps evaluate how well the model performs on the given dataset. Additionally, I implemented a functionality where users can input their own values to predict income. The model requires details such as age and working hours to make a prediction.

Conclusion

This project serves as a valuable resource for individuals who want to learn more about Machine Learning and PyQt5. By making further additions or modifications to the project, users can enhance their understanding and improve their skills in both fields.

Hiç yorum yok