0% found this document useful (0 votes)
15 views13 pages

Perform Prediction Using Regression Algorithm: Ex No: 1 Date

MLT record

Uploaded by

ezhilventhanmb30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

Perform Prediction Using Regression Algorithm: Ex No: 1 Date

MLT record

Uploaded by

ezhilventhanmb30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

Ex no: 1
Perform Prediction using Regression Algorithm
Date:

Aim
To write a python programming using linear regression algorithm for prediction Application.

Algorithm

Step 1: Load the dataset

Step 2: Split dataset int training set and test set.

Step 3: Fit simple linear regression model

Step 4: Finding there is any correlation between 2 variables

Step 5: Finding the best fit line for dataset.

Step 6: Dependent variable is changing into independent variable.

Step 7: Predict the test set.

Step 8: Visualizing the test set.

Step 9: Make new predictions

Code
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dataset=pd.read_csv('Salary_Data.csv')
dataset.head()

print(dataset)
dataset.tail()

1 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

dataset.shape

dataset.info()

dataset.describe()

dataset.size
dataset.isnull().sum()

plt.scatter(dataset['YearsExperience'],dataset['Salary'],color='blue')
plt.scatter(dataset['YearsExperience'],dataset['Salary'],color='blue')
plt.title('Comparsion chart')
plt.xlabel('Experience of year')
plt.ylabel('Salary')
plt.show()
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error,r2_score

2 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

x=dataset[['YearsExperience']]
y=dataset['Salary']

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42)
model=LinearRegression()
model.fit(x_train,y_train)

predictions = model.predict(x_test)
import seaborn as sns
sns.distplot(predictions-y_test)

3 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

plt.scatter(x_train, y_train, color='red')


plt.plot(x_train, model.predict(x_train))

Result

Thus, the implementation of python programming using linear regression algorithm for prediction
application has been completed successfully

4 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

Ex no: 2
Data Classification using Decision Trees
Date:

AIM
To write a python programming using data classification using tree for car safety application.

Algorithm

Step 1: Begin the tree with the root node, which containsthe complete dataset.

Step 2: Find the best attribute in the dataset using Attribute Selection Measure (ASM).

Step 3: Divide the data set into subsets that contains possible values for the best attributes.which is

determined using information gain entrophy & gain of the attribute

Step 4: Generate the decision tree node, which contains the best attribute

Step 5: Recursively make new decision trees using the subsets of the dataset created in step 3. Continue this

process until a stage is reached where you cannot further classify the nodes and called the final node as a

leaf node.

CODE

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

from sklearn.model_selection import train_test_split

!pip install category_encoders

import category_encoders as ce
from sklearn.tree import DecisionTreeClassifier

from sklearn.metrics import accuracy_score

from sklearn import tree

import graphviz

from sklearn.metrics import confusion_matrix

dataset=pd.read_csv('car_evaluation.csv')

dataset.head()

5 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

dataset.info()

dataset.tail()

dataset.isnull().sum()

#renaming columns
col_names = ['buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety', 'class']

dataset.columns = col_names

dataset.describe()

X = dataset.drop("class", axis = 1)
y = dataset["class"]

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

X_train.shape, X_test.shape

encoder = ce.OrdinalEncoder(cols = ['buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety'])

X_train = encoder.fit_transform(X_train)

6 717821f132 – Mohamed Rajeek


DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

X_test = encoder.transform(X_test)

giniclf = DecisionTreeClassifier(criterion = "gini", max_depth = 3, random_state = 0)

giniclf.fit(X_train, y_train)

ypred = giniclf.predict(X_test)

ypredtrain = giniclf.predict(X_train) #accuracy

print('Model accuracy score for test data with criterion gini index: {0:0.4f}'.
format(accuracy_score(y_test, ypred)))

print(print('Model accuracy score for training data with criterion gini index: {0:0.4f}'.
format(accuracy_score(y_train, ypredtrain))))

print('Training set score: {:.4f}'.format(giniclf.score(X_train, y_train)))

print('Test set score: {:.4f}'.format(giniclf.score(X_test, y_test)))

plt.figure(figsize = (12,8))
tree.plot_tree(giniclf.fit(X_train, y_train))

tree.plot_tree(giniclf.fit(X_train, y_train))

newtree = tree.export_graphviz(giniclf,out_file = None, feature_names = X_train.columns,


class_names = y_train, filled = True, rounded = True, special_characters = True)

graph = graphviz.Source(newtree)

from sklearn.tree import DecisionTreeClassifier

enclf = DecisionTreeClassifier(criterion = "entropy", max_depth = 3, random_state = 0)

enclf.fit(X_train,y_train)

7 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

ypreden = enclf.predict(X_test)
ypredten = enclf.predict(X_train)

print('Model accuracy for training data: {0:0.4f}'.format(accuracy_score(y_train, ypredten)))


print('Model accuracy for test data: {0:0.4f}'.format(accuracy_score(y_test,ypreden)))

print('Training set score:{:.4f}'.format(enclf.score(X_train,y_train)))


print('Test set score:{:.4f}'.format(enclf.score(X_test,y_test)))

plt.figure(figsize = (12,8))
tree.plot_tree(enclf.fit(X_train,y_train))

newtreeen = tree.export_graphviz(enclf, out_file=None, feature_names=X_train.columns,


class_names=y_train, filled=True, rounded=True, special_characters=True)

graph = graphviz.Source(newtreeen)

cm = confusion_matrix(y_test,ypreden)

print('Confusion Matrix\n',cm)

RESULT
Thus, the implementation of python programming using data classification using tree has been
executed successfully.

8 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

Ex no: 3
Data Classification using Bayesian learning method for income
Date:
prediction

Aim:

To write a python program using data classification byBayesian learning method for income prediction

Algorithm:

Step 1: Importing all the necessary libraries.

Step 2: Load the dataset.

Step 3: Bayesian learning classifier determines the probability of hypothesis with prior knowledge.

Step 4: Convert the given dataset into frequency tables.

Step 5: Generate likelihood table by finding the probabilities of given features.

Step 6: ApplyBaye’stheorem to calculate the posterior probability for income predictions.

Step 7: Thus, income prediction isimplemented byBayesian learning.

CODE

from sklearn.model_selection import train_test_split


from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, classification_report
from sklearn.datasets import load_iris
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dataset = pd.read_csv('heart.csv')
dataset.head(10)

9 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

dataset.info()

X = dataset[['age']]
y = dataset['fbs']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
model = GaussianNB()
model.fit(X_train, y_train)

y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print("\nClassification Report:\n", report)

RESULT

Thus, the implementation of python program using Bayesian learning for income prediction has been
executed successfully

10 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

Ex no: 4
Data Classification using Support Vector Machine for Credit Card
Date:
Fraud Detection

Aim:
To wire a python program using data Classification using Support Vector Machine for Credit Card Fraud
Detection.

Algorithm:
Step 1: Importing all the necessary Libraries.

Step 2: Load the dataset from the csv file.

Step 3: The sum classifier classifies the dataset by linear separable method to find the best line or decision

boundary.

Step 4: Sum classifier finds the closet point of the lines from the different classes.

Step 5: Train the dataset using sum classifier.

Step 6: Test the dataset.

Step 7: Thus, credit card fraud detection is implemented by sum.

CODE

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

from sklearn.svm import SVC

from sklearn.metrics import accuracy_score, classification_report

dataset = pd.read_csv('heart.csv')

dataset.head()

11 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

dataset.info()

X = dataset[['cp']]
y = dataset['slope']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)

X_test = scaler.transform(X_test)
model = SVC(kernel='linear', C=1.0, random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print("\nClassification Report:\n", report)

def plot_decision_boundaries(X, y, model):


h = .02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),

12 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES

np.arange(y_min, y_max, h))


plt.scatter(X_train, y_train, color='red')
plt.plot(X_train, model.predict(X_train))
plt.title('Comparsion chart for heart between cp and slope(717821f127)')
plt.xlabel('cp')
plt.ylabel('Slope')
plt.show()

RESULT

Thus ,the implementation of python program using data Classification using Support Vector Machine
for Credit Card Fraud Detection has been executed successfully.

13 717821f132-Mohamed Rajeek

You might also like