0% found this document useful (0 votes)
6 views3 pages

Machine Learning: Decision Trees & Regression

The document outlines machine learning programs using Decision Trees and Linear Regression on various datasets, including the Iris, Breast Cancer, Housing, and Marks datasets. It demonstrates the implementation of classification and regression models, evaluates their performance using metrics like accuracy, mean squared error, and R2 score, and visualizes the decision trees. Additionally, it includes code snippets for data preparation, model training, prediction, and result visualization.

Uploaded by

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

Machine Learning: Decision Trees & Regression

The document outlines machine learning programs using Decision Trees and Linear Regression on various datasets, including the Iris, Breast Cancer, Housing, and Marks datasets. It demonstrates the implementation of classification and regression models, evaluates their performance using metrics like accuracy, mean squared error, and R2 score, and visualizes the decision trees. Additionally, it includes code snippets for data preparation, model training, prediction, and result visualization.

Uploaded by

gurugowda733
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Machine Learning Programs

Decision Tree on Iris Dataset


from [Link] import load_iris
from [Link] import DecisionTreeClassifier, plot_tree
from sklearn.model_selection import train_test_split
from [Link] import classification_report,
accuracy_score
import [Link] as plt

iris = load_iris()
X, y = [Link], [Link]

X_train, X_test, y_train, y_test = train_test_split(X,


test_size=0.3, random_state=42)

clf = DecisionTreeClassifier(criterion='entropy', max_depth=3,


random_state=42)
[Link](X_train, y_train)

y_pred = [Link](X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))


print("\nClassification Report:\n",
classification_report(y_test, y_pred))

[Link](figsize=(12,6))
plot_tree(clf, filled=True, feature_names=iris.feature_names,
class_names=iris.target_names, rounded=True)
[Link]()

Decision Tree on Breast Cancer Dataset


import pandas as pd
from [Link] import load_breast_cancer
from [Link] import DecisionTreeClassifier, plot_tree
from sklearn.model_selection import train_test_split
from [Link] import classification_report,
accuracy_score
import [Link] as plt

data = load_breast_cancer()
X, y = [Link], [Link]

X_train, X_test, y_train, y_test = train_test_split(Xx,


test_size=0.3, random_state=42)

clf = DecisionTreeClassifier(criterion='gini', max_depth=4,


random_state=42)
[Link](X_train, y_train)

y_pred = [Link](X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))


print("\nClassification Report:\n",
classification_report(y_test, y_pred))

[Link](figsize=(16,8))
plot_tree(clf, filled=True, feature_names=data.feature_names,
class_names=data.target_names, rounded=True)
[Link]()

Linear Regression on Housing Dataset


import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import r2_score, mean_absolute_error,
mean_squared_error

df = pd.read_csv("[Link]")
df = pd.get_dummies()

e =
[Link](['mainroad_no','guestroom_no','basement_yes','hotwater
heating_yes','airconditioning_yes'], axis=1)

x = [Link][:,1:]
y = [Link][:,0]

x_train, x_test, y_train, y_test =


train_test_split(x,test_size=0.2)

lm = LinearRegression()
[Link](x_train, y_train)

y_pred = [Link](x_test)

print("MSE:", mean_squared_error(y_test, y_pred))


print("RMSE:", [Link](mean_squared_error(y_test, y_pred)))
print("MAE:", mean_absolute_error(y_test, y_pred))
print("R2 Score:", r2_score(y_test, y_pred))

Linear Regression on Marks Dataset


import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error,
mean_absolute_error, r2_score
import [Link] as plt

df = pd.read_csv("marks_datasets.csv")
print([Link]())

x = df['CIE'].[Link](-1,1)
y = df['SEE'].[Link](-1,1)

x_train, x_test, y_train, y_test = train_test_split(x,


random_state=0)

ln = LinearRegression()
[Link](x_train, y_train)

y_pred = [Link](x_test)

print("MAE:", mean_absolute_error(y_test, y_pred))


print("MSE:", mean_squared_error(y_test, y_pred))
print("R2 Score:", r2_score(y_test, y_pred))
print("RMSE:", [Link](mean_squared_error(y_test, y_pred)))

[Link](x_train, y_train, color='g')


[Link](x_test, y_pred, color='f')
[Link]()

You might also like