CSIT366-Lab File
CSIT366-Lab File
Line Graph
A line chart represents data points connected by straight lines. It is useful for showing trends or
changes over time or continuous data. The x-axis typically represents the independent variable,
while the y-axis represents the dependent variable.
x = [1,2,3,4,5]
y = [2,4,6,8,10]
plt.plot(x,y)
plt.xlabel('X-AXIS')
plt.ylabel('Y-AXIS')
plt.title('Line Chart')
plt.show()
Bar Chart
A bar chart displays categorical data using rectangular bars of varying lengths. Each bar
represents a category, and the length of the bar corresponds to the value or frequency of that
category. Bar charts are suitable for comparing values between different categories.
x = ['Maths','English','Hindi','Science','GK']
y = [80, 90, 81, 45, 67, 76]
plt.bar(x,y)
plt.xlabel('Subjects')
plt.ylabel('Marks')
plt.title('Bar Chart')
plt.show()
Histogram
A histogram visualizes the distribution of numerical data. It consists of adjacent rectangular bins
representing intervals or ranges of values, while the height of each bin represents the frequency
or count of values falling within that range. Histograms help analyze the shape, spread, and
central tendency of the data.
data = [1,1,2,3,4,4,4,5,5,6,7,7,7,8,9]
plt.hist(data, bins=10)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()
EXPERIMENT 2
AIM: Create a n*k matrix to represent a linear function that maps k-dimensional vectors to n-
dimensional vectors.
SOFTWARE USED: Python, matplotlib library
CODE:
import random
import numpy as np
def create_matrix(n,k):
matrix=[]
for i in range(n):
row=[]
for j in range(k):
row.append(random.random())
matrix.append(row)
return matrix
if __name__ == "__main__":
matrix = create_matrix(3,2)
print(matrix)
EXPERIMENT 3
AIM: Import a dataset from Kaggle and perform different tasks on it.
SOFTWARE USED: Python, Kaggle
CODE:
import numpy as np
import pandas as pd
df = pd.read_csv("./exp_3/data2.csv")
print(df["User ID"])
print(df.head())
print(df.shape)
print(df.info())
EXPERIMENT 4
AIM: Write a program to perform different text analysis operations using NLTK
SOFTWARE USED: Python, matplotlib, nltk library
CODE:
import nltk
from nltk.tokenize import sent_tokenize
from nltk.tokenize import word_tokenize
from nltk.probability import FreqDist
import matplotlib.pyplot as plt
from nltk.corpus import stopwords
nltk.download("punkt")
text = """Hello Dear Students, how are you doing today? Today we will study natural language concepts,
and implement the same on Python platform. Everyone has to write the program and make practile file
too"""
tokenized_sent = sent_tokenize(text)
print(tokenized_sent)
tokenized_word = word_tokenize(text)
print(tokenized_word)
fdist = FreqDist(tokenized_word)
print(fdist.most_common(2))
nltk.download("stopwords")
stop_words = set(stopwords.words("english"))
print(stop_words)
filtered_tokens = []
for w in tokenized_word:
if w not in stop_words:
filtered_tokens.append(w)
1) HTTP Request
import random
import string
def generate_random_word(length):
letters = string.ascii_lowercase
return "".join(random.choice(letters for _ in range(length)))
def main():
try:
word_length = int(input("Enter the desired word length: "))
num_words = int(input("Enter the number of words you need: "))
except ValueError:
print(
"Please enter valid positive integers for word length and number of words."
)
if __name__ == "__main__":
main()
2) A text file
import random
def get_list_of_words(path):
with open(path, "r", encoding="utf-8") as f:
return f.read().splitlines()
words = get_list_of_words("/content/wordGenerate.txt")
print(words)
random_word = random.choice(words)
print(random_word)
EXPERIMENT 6
AIM: To explore how to use morphing to transform one image into another.
SOFTWARE USED: Python
CODE:
EXPERIMENT 7
AIM: To generate n-grams from a given text, where an n-gram is a sequence of 'n' words.
SOFTWARE USED: Python
CODE:
def generate_ngrams(text, WordsToCombine):
words = text.split()
output = []
for i in range(len(words) - WordsToCombine + 1):
output.append(words[i:i + WordsToCombine])
return output
# Example usage:
text = 'this is a very good class to teach and interact'
WordsToCombine = 3
print(ngrams)
EXPERIMENT 8
AIM: To understand and implement Part-of-Speech (POS) tagging using a Hidden Markov
Model and to implement Viterbi decoding for POS tagging.
SOFTWARE USED: Python, Jupyter
CODE:
# Import libraries
import nltk
from nltk.tag import hmm
from nltk.corpus import treebank
nltk.download('treebank')
# POS tagging
test_sentence = "This is a sample sentence for POS tagging."
tokens = nltk.word_tokenize(test_sentence)
pos_tags = tagger.tag(tokens)
# Import libraries
import nltk
from nltk.tag import hmm
from nltk.corpus import treebank
# Import libraries
import transformers
from transformers import pipeline
# Display sentiment
print(sentiment)