# First Hands-On ML Model

In this article, I explain my first machine learning project, which focused on predicting student performance based on various factors. The dataset used for this project was the [**Student Performance Factors**](https://www.kaggle.com/datasets/lainguyn123/student-performance-factors) dataset, available on Kaggle. This dataset contains 19 features, and as a beginner, I started with a simple approach.

Initially, I focused on predicting student scores using just one feature: **hours of study**(Hours\_Studied). This was my first step in the project. Afterward, I expanded the analysis to include all 19 features to understand which ones had an impact on student scores and which did not. I also identified which feature had the most significant effect on the students' grades.

## Getting Started with the Data

As a beginner, I started with the basics by importing my data into **Jupyter Notebook**. The first thing I did was to analyze the dataset's structure, checking the shape, the appearance of the data, and identifying any missing values.

As a popular quote says, *“Half of the problem is solved by understanding the data.”* This is why I focused on gaining a deep understanding of the dataset.

## Data Manipulation and Analysis

For data manipulation and analysis, I used the **Pandas** library in Python. I applied various functions to clean and analyze the data effectively. This step helped prepare the dataset for further modelling.

The [`data.info`](http://data.info)`()` function provides information about the dataset, including the data types of each feature and the count of non-null values.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1753346864200/229dd29a-fa45-47f7-a9d0-769487791d1f.png align="center")

The `data.isna().sum()` The function provides the total number of missing values in each column (feature).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1753346882381/d36960b8-2db3-4564-a893-4a9365957660.png align="center")

The `data.describe()` function provides statistical summaries of the numerical data, including metrics like:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1753346844499/c431fe5c-ff38-49ea-8c94-49bb5e01292b.png align="center")

## Data Visualization

Next, I visualized the data using **Matplotlib**. This tool enabled me to create various plots, such as box plots and scatter plots, which helped in better understanding the relationships between the features and student performance.

Here is the scatter plot showing the relationship between the input variable (**Hours\_Studied**) and the output (**Exam\_Score**).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1753347702715/b67616ef-f817-447d-80e8-be83aee4ad61.png align="center")

## Applying the Machine Learning Model: Linear Regression

After manipulating and visualizing the data, it was time to apply my first machine learning model: **Linear Regression**. Before using the model, I needed to split the dataset into two parts: the **training set** and the **validation set**. The reason for this split is to evaluate the model's performance on unseen data. I used a 75-25 ratio, where 75% of the data was allocated to the training set and the remaining 25% was used for validation.

## Training the Model

With the data split, I proceeded to train the linear regression model on the training dataset. Once the model was trained, I evaluated its accuracy using the validation dataset.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1753348413682/48a546b2-4eb1-4428-8b50-8ebd9269eebb.png align="center")

## Model Evaluation

The final step in the process was the **evaluation** phase, where I assessed how well the model performed on the validation data. The evaluation metrics helped determine the accuracy and effectiveness of the model in predicting student performance.

Here are the evaluation metrics, including **MAE**, **MSE**, and **RMSE** results:

```plaintext
MAE: 2.5057653562271898
```

```plaintext
MSE: 12.130452140014187
```

```plaintext
RMSE: 3.4828801111037695
```

The results suggest that my model performs well with the **Hours\_Studied** feature. Now, I will apply **Polynomial Regression** to see how this model performs.

## Why Do We Need Polynomial Regression?

The fundamental idea behind **linear regression** is that there is a linear relationship between the dependent variable (y) and the independent variable (X). Linear regression fits a straight line to the data, and while this approach gave decent results, the model still didn’t perform as well as expected.

To improve performance, I decided to try **polynomial regression**, which models the relationship between the independent variable (X) and the dependent variable (y) as an **nth-degree polynomial**. This allows the model to capture more complex, non-linear relationships in the data.

Polynomial regression is similar to linear regression, with the main difference being that we add **polynomial terms** (i.e., powers of the independent variable) to the feature space. This enables the model to better fit non-linear patterns in the data.

Let’s apply this model to our dataset and see how it performs.

```python
poly = PolynomialFeatures(degree=3) 

x_poly = poly.fit_transform(train_X)

poly.fit(x_poly,train_y) 

lin = LinearRegression()

 lin.fit(x_poly,train_y)
```

## Model Evaluation

```plaintext
MAE: 2.497633745495845
```

```plaintext
MSE: 11.83724466538172
```

```plaintext
RMSE: 3.440529707091877
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1753665648311/44c3c2bb-0af3-407d-9377-840182b1f29f.png align="center")

As we can see, there is not much difference between the curve of **linear regression** and **polynomial regression**. This suggests that the relationship between the independent variable (X) and the dependent variable (y) is linear in our case.

You can find the code and further details in my GitHub repository: [https://github.com/Mehar-Zaman-Ali/student-performance-factors](https://github.com/Mehar-Zaman-Ali/student-performance-factors)
