Introduction to Machine Learning?
A subset of artificial intelligence known as machine learning focuses primarily on the creation of
algorithms that enable a computer to independently learn from data and previous experiences. Arthur
Samuel first used the term "machine learning" in 1959. Machine learning algorithms create a
mathematical model that, without being explicitly programmed, aids in making predictions or
decisions with the assistance of sample historical data, or training data.
Working of Machine Learning
A machine learning system builds prediction models, learns from previous data, and predicts the
output of new data whenever it receives it. The amount of data helps to build a better model that
accurately predicts the output, which in turn affects the accuracy of the predicted output.
Features of Machine Learning
➢ Machine learning uses data to detect various patterns in a given dataset.
➢ It can learn from past data and improve automatically.
➢ It is a data-driven technology.
➢ Machine learning is similar to data mining as it also deals with the huge amount of the data.
Need for Machine Learning
➢ Rapid increment in the production of data
➢ Solving complex problems, which are difficult for a human
➢ Decision making in various sector including finance
➢ Finding hidden patterns and extracting useful information from data.
Classification of Machine Learning
1 Supervised Learning: In supervised learning, sample labeled data are provided to the machine
learning system for training, and the system then predicts the output based on the training data.
Supervised learning can be grouped further in two categories of algorithms:
➢ Classification
➢ Regression
2 Unsupervised Learning: Unsupervised learning is a learning method in which a machine learns
without any supervision. The training is provided to the machine with the set of data that has not been
labeled, classified, or categorized, and the algorithm needs to act on that data without any
supervision.
➢ Clustering
➢ Association
3 Reinforcement Learning: Reinforcement learning is a feedback-based learning method, in which a
learning agent gets a reward for each right action and gets a penalty for each wrong action. The agent
learns automatically with these feedbacks and improves its performance.
EXPERIMENT – 1
Aim: Write a program to show the implementation of Simple Linear Regression.
Source Code
import numpy as np
import pandas as pd
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# Sample data
data = {'Advertising Spend': [1000, 2000, 3000, 4000, 5000], 'Sales': [5000, 7000, 10000, 11000,
12000]}
# Create a DataFrame
df = [Link](data)
# Features and target variable
X = df[['Advertising Spend']] # Features (independent variable)
y = df['Sales'] # Target (dependent variable)
# 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)
# Create a linear regression model
model = LinearRegression()
# Fit the model
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Print the coefficients
print(f"Slope (m): {model.coef_[0]}")
print(f"Intercept (b): {model.intercept_}")
# Plotting
[Link](X, y, color='blue', label='Actual Sales')
[Link](X_test, y_pred, color='red', linewidth=2, label='Predicted Sales')
[Link]('Linear Regression: Advertising Spend vs Sales')
[Link]('Advertising Spend ($)')
[Link]('Sales ($)')
[Link]()
[Link]()
Output:
Slope (m): 1.771428571428571
Intercept (b): 3742.857142857145
EXPERIMENT – 2
Aim: Write a program to implement the naïve Bayesian classifier for a sample training data set stored
as a .CSV file. Compute the accuracy of the classifier, considering few test data sets.
Source Code
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score
# Sample dataset creation (you can replace this with loading from a CSV file)
data = {'Feature1': [1.0, 2.0, 1.5, 3.0, 2.5, 1.0, 1.2, 3.5, 2.2, 1.3], 'Feature2': [0.5, 1.5, 1.0, 2.5, 2.0, 0.8,
1.1, 2.8, 1.6, 0.9], 'Label': ['A', 'B', 'A', 'B', 'B', 'A', 'A', 'B', 'B', 'A']}
# Load the dataset into a DataFrame
df = [Link](data)
# Separate features and target variable
X = df [['Feature1', 'Feature2']]
y = df['Label']
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Initialize the Gaussian Naive Bayes classifier
model = GaussianNB()
# Fit the model on the training data
[Link](X_train, y_train)
# Make predictions on the test data
y_pred = [Link](X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print (f'Accuracy: {accuracy * 100:.2f} %')
# Example of predicting a new data point
new_data = [Link]({'Feature1': [2.0], 'Feature2': [1.5]})
new_prediction = [Link](new_data)
print (f'Prediction for new data point {new_data.values}: {new_prediction[0]}')
Output: Accuracy: 100.00 %
Prediction for new data point [[2.0 1.5]]: B