Theory of Intelligence
12TH•Computer Science•Data And Analysis•Building a Machine Learning Model

6.6 Building a Machine Learning Model

Features, train-test split, linear regression, and a grade-prediction example.

6.6 Building a Machine Learning Model

Building a machine learning model involves several clear steps (as shown in Figure 6.6). A simple example of machine learning is student grade prediction. In this example, data about students is first collected, such as attendance, quiz marks, assignment scores, study hours, and previous exam results. The data is then cleaned and prepared so that it can be used for learning. Important values, also called features, are selected because they help the model understand the factors that affect student performance. After this, the machine learning model is trained using the prepared data. Once trained, the model can predict a student's possible grade based on new input data. This helps teachers identify students who may need extra support before the final examination.

(Figure 6.6: Machine Learning Model Building Process)

TIDBIT

lightbulb

Building a model involves selecting data and features carefully. Training data teaches the model patterns. Testing data checks performance. Feature selection improves results. A well-built model gives reliable predictions.

Example:

Students' exam scores are often affected by the number of hours they study. The purpose of the following program is to build a simple linear regression model that predicts exam scores based on study hours. The dataset contains examples of hours studied and corresponding exam scores. The data is divided into training and testing sets, the model is trained using the training data, and its performance is evaluated using Mean Squared Error (MSE). Finally, the actual data points and the predicted regression line are plotted to show the relationship between study hours and exam scores.

PYTHON
# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Example data (Hours studied vs. Exam scores)
X = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])  # Hours studied
y = np.array([1, 2, 2, 3, 3, 4, 5, 5, 6, 7])  # Exam scores

# Step 1: Split the 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)

# Step 2: Create and train the model
model = LinearRegression()
model.fit(X_train, y_train)

# Step 3: Make predictions
y_pred = model.predict(X_test)

# Step 4: Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error: {mse}")

# Step 5: Plotting the results
plt.scatter(X, y, color='blue')  # Actual data points
plt.plot(X, model.predict(X), color='red')  # Predicted line
plt.xlabel('Hours Studied')
plt.ylabel('Exam Scores')
plt.title('Linear Regression: Hours vs. Scores')
plt.show()
  • Data Preparation: The X values represent hours studied, and y values represent the corresponding exam scores (as shown in Figure 6.7).
  • Model Training: A linear regression model is created and trained on the training data (X_train, y_train).
  • Testing: The model is tested on the test data (X_test) and the mean squared error (MSE) is calculated to evaluate its performance. A lower MSE indicates better model performance because it means the predicted values are closer to the actual values.
  • Improvement: As more data is gathered or adjusted, the model's accuracy improves.

Output:

Mean Squared Error: 0.14840.1484 Model equation: Exam Score =0.6552×= 0.6552 \times Hours Studied +0.1466+ 0.1466

(Figure 6.7: Linear Regression: Hours Studied vs Exam Scores)

Discuss theoretical concepts, mathematical formulas, and architectural foundations.

Supports inline code with `backticks`
No comments yet. Be the first to start the discussion!