2173112-Assignment 02

You might also like

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

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks:

Obtained Marks:

Natural Language processing(NLP)

Assignment # 02
Last date of Submission: 5 june, 2023

Submitted to: Dr. Tehreem Qasim


______________________________________________________________________________

Submitted by: M Kashif Shah


______________________________________________________________________________

Reg Number: 2173112


_____________________________________________________________________________

Instructions: Copied or shown assignments will be marked zero. Late submissions are not
entertained in any case. Submit on Google classroom.

DIP/ACV MSCS/MSDS SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Q#1:
For the BBC text classification dataset, available at the following link.
https://lazyprogrammer.me/course_files/nlp/bbc_text_cls.csv

Report the performance of Random Forest classifier available in the ScikitLearn library in Python .

Solution:
import pandas as pd

data_url = 'https://lazyprogrammer.me/course_files/nlp/bbc_text_cls.csv'
df = pd.read_csv(data_url)
# Display the first few rows of the dataset
print(df.head())

X = df['text']
y = df['category']

from sklearn.model_selection import train_test_split

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

from sklearn.feature_extraction.text import TfidfVectorizer

vectorizer = TfidfVectorizer()
X_train_tfidf = vectorizer.fit_transform(X_train)
X_test_tfidf = vectorizer.transform(X_test)

from sklearn.ensemble import RandomForestClassifier

rf_classifier = RandomForestClassifier()
rf_classifier.fit(X_train_tfidf, y_train)

from sklearn.metrics import accuracy_score

y_pred = rf_classifier.predict(X_test_tfidf)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

DIP/ACV MSCS/MSDS SZABIST-ISB

You might also like