Perform Prediction Using Regression Algorithm: Ex No: 1 Date
Perform Prediction Using Regression Algorithm: Ex No: 1 Date
Ex no: 1
Perform Prediction using Regression Algorithm
Date:
Aim
To write a python programming using linear regression algorithm for prediction Application.
Algorithm
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
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
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 category_encoders as ce
from sklearn.tree import DecisionTreeClassifier
import graphviz
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.shape, X_test.shape
X_train = encoder.fit_transform(X_train)
X_test = encoder.transform(X_test)
giniclf.fit(X_train, y_train)
ypred = giniclf.predict(X_test)
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))))
plt.figure(figsize = (12,8))
tree.plot_tree(giniclf.fit(X_train, y_train))
tree.plot_tree(giniclf.fit(X_train, y_train))
graph = graphviz.Source(newtree)
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)
plt.figure(figsize = (12,8))
tree.plot_tree(enclf.fit(X_train,y_train))
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 3: Bayesian learning classifier determines the probability of hypothesis with prior knowledge.
CODE
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 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.
CODE
import numpy as np
import pandas as pd
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)
12 717821f132-Mohamed Rajeek
DEPARTMENT OF INFORMATION TECHNOLOGY 21ID14 MACHINE LEARNING TECHNIQUES
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