Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CS22412 – ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING

LABORATORY

EX.NO:9
DATE:
DECISION TREE

AIM:
To implement decision tree based ID3 algorithm using the data set for building the decision tree and
apply this knowledge to classify a new sample.

ALGORITHM:
1. Start
2. Import Libraries: The program begins by importing necessary libraries such as NumPy, Pandas,
scikit-learn's tree module, and other modules for data handling and visualization.
3. Load Data: It loads the Iris dataset using load_iris() function from scikit-learn. The dataset
contains samples of iris flowers with their features (sepal length, sepal width, petal length, petal
width) and corresponding target labels (species).
4. Split Data: The dataset is split into training and testing sets using train_test_split() function from
scikit-learn. This step helps in evaluating the model's performance on unseen data.
5. Define and Train the Classifier: A Decision Tree Classifier is instantiated with a maximum depth
of 3 (tree.DecisionTreeClassifier(3)) and trained on the training data using fit() method.
6. Predictions: The trained classifier is then used to make predictions on the test data using the
predict() method.
7. Evaluate Model: The accuracy of the model is calculated using metrics.accuracy_score() from
scikit-learn by comparing the predicted labels with the actual labels.
8. Visualize Decision Tree (Optional): The program optionally visualizes the decision tree using
Graphviz and the export_graphviz() function. This part of the code isn't fully shown, but it typically
involves generating a DOT file representing the decision tree structure and then visualizing it using
Graphviz.
9. Stop.

ROLL NO: 2127220501187 PAGE NO: 38


PROGRAM:
import numpy as np
import pandas as pd
from sklearn import tree
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.tree import export_graphviz
from six import StringIO
from IPython.display import Image
import pydotplus
data=load_iris()
data.data.shape
print("features to:",data.feature_names)

x=data.data
y=data.target
display(x.shape,y.shape)
X_train,X_test,y_train,y_test=train_test_split(x,y,random_state=50,test_size=0.35)

clf=tree.DecisionTreeClassifier(3)
clf.fit(X_train,y_train)
y_predict=clf.predict(X_test)
print(y_predict)

print("accuracy:",metrics.accuracy_score(y_test,y_predict))

dot_data=StringIO()

SAMPLE INPUT/OUTPUT:

RESULT:
The program to implement decision tree has been successfully executed and the results are recorded.

ROLL NO: 2127220501187 PAGE NO: 39


CS22412 – ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING
LABORATORY

EX.NO:10
DATE:
MULTILAYER PERCEPTRON

AIM:
To implement Multilayer Perceptron model to Classify a set of documents and measure the accuracy,
precision and recall.

ALGORITHM:
1. Start
2. Data Preprocessing: Convert text documents into numerical representations (e.g., using
techniques like TF-IDF or word embeddings).
3. Model Architecture: Design the MLP architecture, including the number of layers, neurons per
layer, and activation functions.
4. Training: Train the MLP model on the training data.
5. Evaluation: Evaluate the model's performance using metrics such as accuracy, precision, and
recall.
6. Stop.

PROGRAM:
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score,classification_report,confusion_matrix
cancer_data=load_breast_cancer()
X,y=cancer_data.data,cancer_data.target
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)
scaler=StandardScaler()
X_train=scaler.fit_transform(X_train)
X_test=scaler.transform(X_test)
mlp=MLPClassifier(hidden_layer_sizes=(64,32),max_iter=1000,random_state=42)
mlp.fit(X_train,y_train)
y_pred=mlp.predict(X_test)
accuracy=accuracy_score(y_test,y_pred)
print(f"Accuracy:{accuracy:.2f}")
class_report=classification_report(y_test,y_pred)
print("Classification Report:\n",class_report)

ROLL NO: 2127220501187 PAGE NO: 40


SAMPLE INPUT/OUTPUT:

RESULT:
The program to implement Multilayer Perceptron has been successfully executed and the results are
recorded.

ROLL NO: 2127220501187 PAGE NO: 41


CS22412 – ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING
LABORATORY

EX.NO:11
DATE:
XGBOOST

AIM:
To implement XG Boost for sales spice prediction.
ALGORITHM:
1. Start
2. Read the Tesla stock data from a CSV file into a DataFrame (df).
3. Plot the close price of Tesla stock over time using matplotlib.
4. Display histograms for each feature ('Open', 'High', 'Low', 'Close', 'Volume') using seaborn.
5. Split the 'Date' column into year, month, and day components and add them as new columns in
the DataFrame.
6. Make a copy of the DataFrame (data).
7. Define features (X) and target variable (y).
8. Split the data into training and testing sets using train_test_split.
9. Initialize an XGBoost regressor (xgb).
10. Train the XGBoost model on the training data.
11. Make predictions on the testing data.
12. Calculate the root mean squared error (RMSE) between the actual and predicted values.
13. Plot the actual vs predicted stock prices using matplotlib.
14. Stop

ROLL NO: 2127220501187 PAGE NO: 42


PROGRAM:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_error

df = pd.read_csv('/content/TSLA (1).csv')
plt.plot(df['Close'])
plt.title('Tesla close price')
plt.ylabel('Price in USD')
plt.show()
df.head()

features = ['Open','High','Low','Close','Volume']

for i,col in enumerate(features):


sb.distplot(df[col])
plt.show()
splitted = df['Date'].str.split('-',expand = True)
df['year'] = splitted[0].astype('int')
df['month'] = splitted[1].astype('int')
df['day'] = splitted[2].astype('int')
df.head()

data = df.copy()

X = data[['Open', 'High', 'Low', 'Close', 'Volume']]


y = data['Close']

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

xgb = XGBRegressor(objective='reg:squarederror', random_state=42)

xgb.fit(X_train, y_train)

y_pred = xgb.predict(X_test)

rmse = np.sqrt(mean_squared_error(y_test, y_pred))


print("Root Mean Squared Error:", rmse)
plt.figure(figsize=(10, 6))
plt.plot(y_test.values, label='Actual')
plt.plot(y_pred, label='Predicted')
plt.title('Tesla Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.show()

ROLL NO: 2127220501187 PAGE NO: 43


SAMPLE INPUT/OUTPUT:

RESULT:
Hence program to predict tesla stock price using XGBoost gradient boosting has been
executed successfully and predictions has been visualized.

ROLL NO: 2127220501187 PAGE NO: 44

You might also like