Uploaded by Devendra Chaliya

Practical file(machine learning)

advertisement
Practical File
of
Machine learning
BACHELOR OF COMPUTER APPLICATION
(Session 2021-2024)
Submitted To:
Submitted By:
Faculty Name :
Punita mam
Name : Raj
Class – BCA 2021 B
Roll no - 2113670
PUNJAB COLLEGE OF TECHNICAL EDUCATION
BADDOWAL (LUDHIANA)
Index
 Read the numeric data from .CSV file and use some basic operation on it
 Write a program to demonstrate the working of the decision tree algorithm. Use an
appropriate data set for building the decision tree and apply this knowledge to
classify a new sample
 Implementation of regression in machine learning both types of regression using
dependent and independent variable.
1. Read the numeric data from .CSV file and use some basic operation on it

How to read a csv file :
import pandas as pd
df = pd.read_csv("people.csv")
print(df.head())

How to create a csv file :
import csv
data = [
['Name', 'Age', 'City'],
['John', 30, 'New York'],
['Alice', 25, 'Los Angeles'],
['Bob', 35, 'Chicago'] ]
file_path = 'example.csv'
with open(file_path, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
print(f"CSV file '{file_path}' has been created successfully.")

How to write in already exiting file csv file :
import csv
additional_data = [
['Eve', 28, 'San Francisco'],
['Michael', 40, 'Seattle']
]
file_path = 'example.csv'
with open(file_path, 'a', newline='') as file:
writer = csv.writer(file)
writer.writerows(additional_data)
print("Additional data has been added to the CSV file.")
2. Write a program to demonstrate the working of the decision tree algorithm.
Use an appropriate data set for building the decision tree and apply this
knowledge to classify a new sample.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
new_sample = [[5.1, 3.5, 1.4, 0.2]] # Example new sample
predicted_class = clf.predict(new_sample)
print("Predicted class for new sample:", iris.target_names[predicted_class[0]])
3. Implementation of regression in machine learning both types of regression
using dependent and independent variable.
 Using Dependent variable
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
boston = load_boston()
X = boston.data
y = boston.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
lr_model = LinearRegression()
lr_model.fit(X_train, y_train)
y_pred = lr_model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error:", mse)
 Using independent variable
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
degree = 2
poly_model = make_pipeline(PolynomialFeatures(degree), LinearRegression())
poly_model.fit(X_train, y_train)
y_pred_poly = poly_model.predict(X_test)
mse_poly = mean_squared_error(y_test, y_pred_poly)
print("Mean Squared Error (Polynomial Regression):", mse_poly)
Download