0% found this document useful (0 votes)
7 views6 pages

Decision Tree

The document contains Python code examples demonstrating the use of different machine learning algorithms including Decision Tree, Random Forest, Linear Regression, and K-Nearest Neighbors (KNN) using the iris dataset. Each section includes loading the dataset, splitting it into training and testing sets, training the model, making predictions, and evaluating accuracy. The document also suggests possible modifications such as hyperparameter tuning and cross-validation.

Uploaded by

sugikrish
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)
7 views6 pages

Decision Tree

The document contains Python code examples demonstrating the use of different machine learning algorithms including Decision Tree, Random Forest, Linear Regression, and K-Nearest Neighbors (KNN) using the iris dataset. Each section includes loading the dataset, splitting it into training and testing sets, training the model, making predictions, and evaluating accuracy. The document also suggests possible modifications such as hyperparameter tuning and cross-validation.

Uploaded by

sugikrish
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
You are on page 1/ 6

from sklearn import datasets

from sklearn.tree import DecisionTreeClassifier

from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score

# Loading iris dataset

iris = datasets.load_iris()

X = iris.data

y = iris.target

# Splitting dataset into train and test sets

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

# Creating Decision Tree Classifier

dtree = DecisionTreeClassifier()

# Training the model

dtree.fit(X_train, y_train)

# Making predictions on test set

y_pred = dtree.predict(X_test)

# Evaluating the model

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

This code loads the iris dataset, splits it into train and test sets, creates a decision tree classifier,
trains the model, makes predictions on the test set, and evaluates the model by computing accuracy.
You can modify this code as per your requirement, for example, you can tune hyperparameters to
optimize the decision tree, or you can use cross-validation to evaluate the model.
# Import necessary libraries

from sklearn.datasets import load_iris

from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score

# Load iris dataset

iris = load_iris()

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)

# Create a random forest classifier with 100 trees

rfc = RandomForestClassifier(n_estimators=100)

# Train the model using the training sets

rfc.fit(X_train, y_train)

# Make predictions on the testing set

y_pred = rfc.predict(X_test)

# Calculate the accuracy of the model

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)
Linear Regression

# Import necessary libraries

import numpy as np

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression

# Define the independent variable (x) and dependent variable (y)

x = np.array([1, 2, 3, 4, 5])

y = np.array([3, 5, 7, 9, 11])

# Reshape the arrays

x = x.reshape((-1, 1))

y = y.reshape((-1, 1))

# Create the linear regression model and fit the data

model = LinearRegression().fit(x, y)

# Print the coefficients and intercept

print('Coefficients:', model.coef_)

print('Intercept:', model.intercept_)

# Predict a value

y_pred = model.predict([[6]])

print('Predicted value for x=6:', y_pred)


plt.scatter(x, y)

plt.plot(x, model.predict(x), color='red')

plt.show()
KNN

# Importing required libraries

import pandas as pd

import numpy as np

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.neighbors import KNeighborsClassifier

from sklearn.metrics import accuracy_score

# Loading the iris dataset

iris_data = load_iris()

X = pd.DataFrame(iris_data.data, columns=iris_data.feature_names)

y = pd.Series(iris_data.target)

# Splitting the dataset into train and test sets

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

# Creating a kNN classifier

knn = KNeighborsClassifier(n_neighbors=3)

# Training the classifier using the training set


knn.fit(X_train, y_train)

# Making predictions on the test set

y_pred = knn.predict(X_test)

# Evaluating the accuracy of the classifier

accuracy = accuracy_score(y_test, y_pred)

print('Accuracy:', accuracy)

You might also like