What is Machine Learning?
Machine Learning (ML) is a subset of Artificial Intelligence (AI) that focuses on building systems capable of learning from data and making predictions or decisions without being explicitly programmed. Instead of writing specific rules for a computer to follow, we provide it with data and let it identify patterns on its own.
For example, if you want a computer to recognize cats in photos, you don’t write rules like "a cat has pointy ears and whiskers." Instead, you show the computer thousands of pictures of cats and let it figure out the patterns.
Machine Learning is used in various applications, such as:
- Recommendation Systems: Netflix and YouTube use ML to recommend movies and videos.
- Image Recognition: Facebook uses ML to tag people in photos.
- Natural Language Processing (NLP): Virtual assistants like Siri and Alexa use ML to understand and respond to voice commands.
- Healthcare: ML is used to predict diseases and analyze medical images.
- Finance: Banks use ML to detect fraudulent transactions.
A Simple Machine Learning Example: Predicting Exam Scores
Let’s walk through a simple supervised learning example using Python. We’ll predict a student’s exam score based on the number of hours they studied.
Step 1: Install Required Libraries
First, ensure you have Python installed. Then, install the necessary libraries using the following command:
pip install numpy pandas scikit-learn matplotlib
Step 2: Prepare the Data
We’ll create a small dataset with two columns: Hours Studied
and Exam Score
.
import pandas as pd
# Create a dataset
data = {
'Hours Studied': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'Exam Score': [20, 40, 60, 70, 80, 85, 90, 95, 98, 100]
}
# Convert to a DataFrame
df = pd.DataFrame(data)
print(df)
Step 3: Train the Model
We’ll use Linear Regression, a simple algorithm that finds the best-fit line through the data.
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# Define features (X) and target (y)
X = df[['Hours Studied']]
y = df['Exam Score']
# Create and train the model
model = LinearRegression()
model.fit(X, y)
# Make predictions
predictions = model.predict(X)
# Plot the results
plt.scatter(X, y, color='blue', label='Actual Scores')
plt.plot(X, predictions, color='red', label='Predicted Scores')
plt.xlabel('Hours Studied')
plt.ylabel('Exam Score')
plt.legend()
plt.show()
Step 4: Evaluate the Model
To check how well the model performs, we can calculate the accuracy using metrics like Mean Squared Error (MSE).
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(y, predictions)
print(f"Mean Squared Error: {mse}")
Comments
Post a Comment