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

Big Data Pyqp Answers

The document contains R programming examples for various tasks including checking if a number is positive or negative, sorting a vector, printing a multiplication table, checking for Armstrong numbers, and converting temperatures. It also explains Naive Bayes classification with an email example, discusses data visualization with a bar plot example, and provides functions to calculate the volume of a cuboid and check for leap years. Each section includes user input prompts and relevant output displays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views6 pages

Big Data Pyqp Answers

The document contains R programming examples for various tasks including checking if a number is positive or negative, sorting a vector, printing a multiplication table, checking for Armstrong numbers, and converting temperatures. It also explains Naive Bayes classification with an email example, discusses data visualization with a bar plot example, and provides functions to calculate the volume of a cuboid and check for leap years. Each section includes user input prompts and relevant output displays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SET NUMBER:1 [Big Data]

Q4]

2] Write an R program to find out number is positive or negative

# Function to check if a number is positive, negative, or zero

check_number <- function(num) {

if (num > 0) {

return("The number is positive.")

} else if (num < 0) {

return("The number is negative.")

} else {

return("The number is zero.")

# User input

number <- as.numeric(readline(prompt = "Enter a number: "))

result <- check_number(number)

cat(result, "\n")

3] Write an R program to sort a Vector in ascending and descending order

# Function to sort a vector

sort_vector <- function(vec) {

ascending <- sort(vec)

descending <- sort(vec, decreasing = TRUE)

list(ascending = ascending, descending = descending)

# User input

user_input <- as.numeric(unlist(strsplit(readline(prompt = "Enter numbers separated by spaces: "), "


")))
sorted_results <- sort_vector(user_input)

# Display results

cat("Ascending Order: ", sorted_results$ascending, "\n")

cat("Descending Order: ", sorted_results$descending, "\n")

4] Write an R Program to print Multiplication Table of 2.

# Function to print multiplication table of 2

print_multiplication_table <- function(num, range) {

for (i in 1:range) {

result <- num * i

cat(num, "x", i, "=", result, "\n")

# Set the number and range

number <- 2

range <- 10

# Print the multiplication table of 2

print_multiplication_table(number, range)

5] Write an R program to check number is Armstrong or not.

# Function to check if a number is an Armstrong number

is_armstrong <- function(num) {

# Convert the number to a string to split it into digits

digits <- as.numeric(unlist(strsplit(as.character(num), "")))

# Calculate the number of digits

n <- length(digits)
# Calculate the sum of each digit raised to the power of n

sum_of_powers <- sum(digits ^ n)

# Check if the sum equals the original number

return(sum_of_powers == num)

# User input

number <- as.numeric(readline(prompt = "Enter a number: "))

if (is_armstrong(number)) {

cat(number, "is an Armstrong number.\n")

} else {

cat(number, "is not an Armstrong number.\n")

SET NUMBER:2 [Big Data]

Q4]

a) Explain Naive Bayes with the help of an example.

Naive Bayes is a classification algorithm based on Bayes' Theorem, with the assumption of
independence between predictors. It’s particularly useful for text classification tasks.

Example: Suppose you want to classify emails as "Spam" or "Not Spam" based on the words they
contain.

1. Training Data:

o Emails labeled as Spam: "Win money", "Free offer"

o Emails labeled as Not Spam: "Meeting tomorrow", "Project update"

2. Calculate Prior Probabilities:

o P(Spam) = Number of Spam emails / Total emails

o P(Not Spam) = Number of Not Spam emails / Total emails

3. Calculate Likelihoods: For each word, calculate the probability of it appearing in Spam and
Not Spam emails. For example:
o P(Win | Spam) = Count(Win in Spam) / Count(Spam)

o P(Win | Not Spam) = Count(Win in Not Spam) / Count(Not Spam)

4. Prediction: Given a new email "Win a free phone", calculate:

o P(Spam | "Win a free phone") using Bayes' Theorem.

o P(Not Spam | "Win a free phone") similarly.

Choose the class with the higher probability.

b) What is data visualization? Explain with example in R.

Data Visualization is the graphical representation of information and data. It helps to understand
trends, patterns, and insights from data.

Example in R: Let’s create a simple bar plot using R to visualize the frequency of categories in a
dataset.

# Sample data

categories <- c("A", "B", "C", "D")

values <- c(10, 15, 5, 20)

# Creating a bar plot

barplot(values, names.arg=categories, col="blue", main="Category Frequency", xlab="Categories",


ylab="Values")

c) Write an R program to accept temperatures in Fahrenheit (F) and print it in Celsius (C).

# Function to convert Fahrenheit to Celsius

fahrenheit_to_celsius <- function(f) {

c <- (f - 32) * 5/9

return(c)

# Accept user input

f <- as.numeric(readline(prompt="Enter temperature in Fahrenheit: "))

c <- fahrenheit_to_celsius(f)

cat("Temperature in Celsius:", c, "\n")


d) Accept three dimensions length (l), breadth (b) and height (h) of a cuboid and print its volume.

# Function to calculate the volume of a cuboid

cuboid_volume <- function(l, b, h) {

volume <- l * b * h

return(volume)

# Accept user input

length <- as.numeric(readline(prompt="Enter length of the cuboid: "))

breadth <- as.numeric(readline(prompt="Enter breadth of the cuboid: "))

height <- as.numeric(readline(prompt="Enter height of the cuboid: "))

volume <- cuboid_volume(length, breadth, height)

cat("Volume of the cuboid:", volume, "\n")

e) Write an R program to accept any year as input and check whether the year is a leap year or not.

# Function to check if a year is a leap year

is_leap_year <- function(year) {

if ((year %% 4 == 0 && year %% 100 != 0) || (year %% 400 == 0)) {

return(TRUE)

} else {

return(FALSE)

# Accept user input

year <- as.numeric(readline(prompt="Enter a year: "))

if (is_leap_year(year)) {

cat(year, "is a leap year.\n")

} else {

cat(year, "is not a leap year.\n")

You might also like