Showing posts with label Linear Regression. Show all posts
Showing posts with label Linear Regression. Show all posts

Machine Learning Programs

 Machine Learning Programs

πŸ‘‰Data Preprocessing in Machine Learning

πŸ‘‰Data Preprocessing in Machine learning (Handling Missing values )

πŸ‘‰Linear Regression - ML Program - Weight Prediction

πŸ‘‰NaΓ―ve Bayes Classifier - ML Program

πŸ‘‰LOGISTIC REGRESSION - PROGRAM

πŸ‘‰KNN Machine Learning Program

πŸ‘‰Support Vector Machine (SVM) - ML Program

πŸ‘‰Decision Tree Classifier on Iris Dataset

πŸ‘‰Classification of Iris flowers using Random Forest

πŸ‘‰DBSCAN

πŸ‘‰ Implement and demonstrate the FIND-S algorithm for finding the most specific hypothesis based on a given set of training data samples. Read the training data from a .CSV file

πŸ‘‰For a given set of training data examples stored in a .CSV file, implement and demonstrate the Candidate-Elimination algorithm to output a description of the set of all hypotheses consistent with the training examples.

πŸ‘‰Write a program to demonstrate the working of the decision tree based ID3 algorithm. Use an appropriate data set for building the decision tree and apply this knowledge to classify a new sample.

πŸ‘‰Build an Artificial Neural Network by implementing the Backpropagation algorithm and test the same using appropriate data sets.

πŸ‘‰Write a program to construct a Bayesian network considering medical data. Use this model to demonstrate the diagnosis of heart patients using a standard Heart Disease Data Set.

πŸ‘‰Write a program to implement k-Nearest Neighbors algorithm to classify the iris data set. Print both correct and wrong predictions.

πŸ‘‰Implement the non-parametric Locally Weighted Regression algorithm in order to fit data points. Select appropriate data set for your experiment and draw graphs.

πŸ‘‰Write a program to implement SVM algorithm to classify the iris data set. Print both correct and wrong predictions.

πŸ‘‰Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data set for clustering using k-Means algorithm. Compare the results of these two algorithms and comment on the quality of clustering.

πŸ‘‰ Write a program using scikit-learn to implement K-means Clustering

πŸ‘‰Program to calculate the entropy and the information gain

πŸ‘‰Program to implement perceptron.

TensorFlow and Keras -1

 Q1. Binary classification

In order to perform binary classification on a dataset (class 0 and 1) using a neural network, which of the options is correct regarding the outcomes of code snippets a and b? Here the labels of observation are in the form : [0, 0, 1...].

Common model:

import tensorflow
from keras.models import Sequential
from keras.layers import Dense
from tensorflow.keras.optimizers import SGD
model = Sequential()
model.add(Dense(50, input_dim=2, activation='relu', kernel_initializer='he_uniform'))
opt = SGD(learning_rate=0.01)

Code snippet a:

model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])

Code snippet b:

mode.add(Dense(1, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])

The term "Required results" in the options means that the accuracy of the model should be above 60%.

Note: 40% of the dataset is from class 0.

Choose the correct answer from below:

A.     Both a and b will give required results.

B.     Only b will give the required results.

C.     Only a will give the required results.

D.     Both a and b will fail to give required results.

Ans: C

Correct option: only a will give the required results.

Explanation :

  • The task requires that the output layer is configured with a single node and a ‘sigmoid‘ activation function in order to predict the probability for the required class. For applying the softmax function for binary classification, the output layer should have 2 neurons for predicting the probability of the two classes individually.
  • In order to get the required results using the softmax function we need to have 2 neurons in the output layer and also the labels should be in one-hot encoded format.

 

Q2. Sequential classification model

import numpy as np
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.optimizers import SGD

model = Sequential()
model.add(Dense(64, activation = 'y', input_dim=50))
model.add(Dense(64, activation = 'y'))
model.add(Dense(x, activation = 'z'))

model.compile(loss ='categorical_crossentropy',
 optimizer = SGD(lr = 0.01),
 metrics = ['accuracy'])

model.fit(X_train, y_train,
 epochs=20)

Ram wants to create a model for the classification of types of malware in 10 different categories. He asked for help from Shyam, and he helped him with the incomplete code as shown above in the snippet. Help Ram in completing the code for classification if the data used has 50 input features. Choose the best-suited option for filling out xy, and z.

Choose the correct answer from below:

A.     x = len(np.unique(y_train)), y = softmax, z = softmax

B.     x = 2 * len(np.unique(y_train)), y = relu, z = relu

C.     x = len(np.unique(y_train)), y = relu, z = softmax

D.     x = 0.5 * len(np.unique(y_train)), y = relu, z = relu

Ans: C

Correct option :

  • x = len(np.unique(y_train))
  • y = relu
  • z = softmax

Explanation :

  • z : For multiclass classification, softmax activation is used.
  • x : For the softmax activation, the output layer has the same number of neurons as the number of different classes.
  • y : ReLu activation function can definitely be used in the intermediate layers. ReLU is not used in the output layer of classification. Because of it's unbounded range, it's difficult to determine thresholds. Though ReLu can be used in regression tasks where negative values don't make sense like predicting prices.

Q3. Multi target output

For a multi-output regression model:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

def get_model(n_inputs):
  model = keras.Sequential()
  model.add(Dense(20, input_dim = n_inputs, kernel_initializer='he_uniform', activation='relu'))
  model.add(______)
  model.compile(loss = 'mae', optimizer = 'adam')
  return model

We want to build a neural network for a multi-output regression problem. For each observation, we have 2 outputs. Complete the code snippet to get the desired output.

Choose the correct answer from below:

A.     Dense(2)

B.     Dense(3)

C.     activation('sigmoid')

D.     activation('relu')

Ans:  A

Correct option: Dense(2).

Explanation:
As we have 2 outputs therefore our output layer of model should have 2 neurons.

 

Q4. Number of parameters

Consider the following neural network model :

model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

The number of parameters in this model is:

Choose the correct answer from below:

A.     120

B.     96

C.     108

D.     121

Ans: D

Correct option : 121

Explanation :

Number of nodes in the input layer(i) = 8
Number of nodes in the hidden layer(h) = 12
Number of nodes in the output layer(o) = 1
So,
Number of parameters = (8×12+12×1)+12+1 = 121

 

Q5. Model summary

Complete the following code snippet in order to get a model with the attached model summary.

import tensorflow as tf
model = tf.keras.models.Sequential()

# Create model
model.add(tf.keras.layers.Input(shape=(_a_, )))
model.add(tf.keras.layers._b_( 512 , activation='relu'))
model.add(tf.keras.layers.Dense( _c_, activation='softmax'))

model.summary()




Choose the correct answer from below:

A.     a - 32, b - Dense, c - 10

B.     a - 12, b - Dense, c - 10

C.     a - 10, b - Dense, c - 5

D.     a - Dense(33), b - Dense, c – 50

Ans: Correct Option:
a - 32, b - Dense, c - 10

Explanation:

  • The key for getting a is that in the first layer we will have the number of parameters equal to (no. of features in input * neurons in the first layer) + neurons in the first layer, i.e. 32 x 512 + 512 = 16896
  • As from the first layer, we got the info from summary as dense. Similarly, for the second layer (i.e. c), we can get the number of neurons from output shape from dense_1.

 

Q6. Logistic regression model

Which of these neural networks would be most appropriately representing a logistic regression model structure for binary classification?

a.

model = Sequential()
model.add(Dense(units=32 input_shape=(2,), activation = ‘relu’))
model.add(Dense(units=64, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

b.

model = Sequential()
model.add(Dense(units=1, input_shape=(2,), activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

c.

model = Sequential()
model.add(Dense(units=1, input_shape=(2,), activation='sigmoid'))
model.add(Dense(units=1, input_shape=(2,), activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

d.

model = Sequential()
model.add(Dense(units=16))
model.add(Dense(units=32, activation=’relu’))
model.add(Dense(units=64,activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

 

Choose the correct answer from below:

A.     a

B.     b

C.     c

D.     d

Ans: B

Correct Option: b

Explanation:

  • Option B would be most appropriate for representing a logistic regression model structure for binary classification. This is because it has a single input layer with only one neuron and a sigmoid activation function. The sigmoid function maps the output to a probability value between 0 and 1, which is ideal for binary classification problems.

  • Option A has two layers, with the second layer using the sigmoid activation function. While this could work for binary classification, the use of the ReLU activation function in the first layer is more commonly used in multi-class classification problems.

  • Option C has two sigmoid layers, which would be more appropriate for a deeper neural network structure for more complex problems.

  • Option D has a similar structure to Option A, with an additional hidden layer. While this could also work for binary classification, the use of ReLU activation in the second layer may make it more suitable for multi-class classification problems.

 

Q7. Model hyperparameters

Complete the following model to get the training output attached to the image.

model.compile(optimizer='sgd',
  loss='sparse_categorical_crossentropy',
  metrics=[‘_a_’])

# train model
model.fit(x=X_train,
          y=y_train,
          epochs = _b_ ,
          validation_data=(X_test, y_test))




Choose the correct answer from below:

A.     a - loss, b - 5

B.     a - accuracy, b - 100

C.     a - loss, b - 25

D.     a - val_acc, b - 100

Ans: B

Correct option:
a - accuracy, b - 100

Explanation:
As the image shows the accuracy, therefore metrics has to be accuracy.
Also in the image, the no. of epochs is showing 100.

 

Q8. Model prediction

We want to use our trained binary classification (trained with binary cross entropy and sigmoid activation function) model 'model', in order to get the label for the first observation in our test dataset of shape (m x n).

Mark the correct option which has the code to meet our requirements.

Notem represents the number of observations and n represents the number of independent variables.

Choose the correct answer from below:

A.     model.predict(test_data[0])

B.     1 if model.predict(test_data[0].reshape(1,-1)) < 0.5 else 0

C.     model.predict(test_data[0].reshape(1,-1))

D.     1 if model.predict(test_data[0].reshape(1,-1)) > 0.5 else 0

Ans: D

Correct Answer: 1 if model.predict(test_data[0].reshape(1,-1)) > 0.5 else 0

Explanation:

  • As the model is trained with sigmoid activation function it’ll give the output with probability between 0 and 1, therefore we need to use the ternary operator.
  • Also we need to reshape the test_data[0] otherwise the api will throw an error mentioning reshaping the data if it has single sample.

 

 

Machine Learning MCQs-2 (Performance Metrics, Linear Regression, NaΓ―ve Bayes Classifier )

                       Machine Learning MCQs- 2
Performance Metrics, Linear Regression, NaΓ―ve Bayes Classifier 


1.  The greater the value for ROC AUC, better the model:
  1. True
  2. False
Ans: 1
 
2.  A set of data are all close to each other, and they are close to the actual value.  This set of data can be described as...
  1. Accurate
  2. Precise
  3. both Precise and accurate
  4. None of the above
Ans: 3

3. The maximum value of the ROC AUC is:
  1. 0.8
  2. 0.9
  3. 1
  4. 0
Ans: 3

4. Recall can be increased by increasing the decision threshold. True or False?
  1. True
  2. False
Ans: 2

5. Which of these is a good measure to decide which threshold to use?
  1. Confusion matrix
  2. F1 score
  3. ROC curve
  4. Precision & Recall versus Threshold Curve
Ans: 4

6. Which of these may have to be performed before analyzing and training the dataset?
  1. Shuffling
  2. Cross-Validation
  3. F1 Score
  4. None
Ans: 1

7. For the below confusion matrix, what is the total number of training datasets?


 

Not 5

5

Not 5

53272

1307

5

1077

4344

  1. 50000
  2. 60000
  3. 70000
  4. 80000
Ans: 2

8. For the below confusion matrix, what is the accuracy?

 

Not 5

5

Not 5

53272

1307

5

1077

4344

  1. 95%
  2. 90%
  3. 96%
  4. 98%
Ans: 4

9.  For the below confusion matrix, what is the recall?

 

Not 5

5

Not 5

53272

1307

5

1077

4344

  1. 0.7
  2. 0.8
  3. 0.9
  4. 0.95
Ans: 2

10. For the below confusion matrix, what is the precision?

 

Not 5

5

Not 5

53272

1307

5

1077

4344

  1. 0.73
  2. 0.76
  3. 0.78
  4. 0.82
Ans: 2

11. F1 score is:
  1. absolute mean of precision and recall
  2. harmonic mean of precision and recall
  3. squared mean of precision and recall
  4. None
Ans: 2

12. For the below confusion matrix, what is the F1 score?

 

Not 5

5

Not 5

53272

1307

5

1077

4344

  1. 0.72
  2. 0.784
  3. 0.82
  4. 0.84
Ans: 2

13. For a model to detect videos that are unsafe for kids, we need (safe video = positive class)
  1. High precision, low recall
  2. High recall, low precision
  3. High Precision, High Recall
  4. Low Precision, Low Recall
Ans: 1

14. For a model to detect shoplifters in surveillance images, we need (shoplifter is positive class)
  1. High precision, low recall
  2. High recall, low precision
  3. High Precision, High Recall
  4. Low Precision, Low Recall
Ans: 2 

15. which of these provide out-of-core support for linear regression?
  1. Normal Equation
  2. SGD
  3. Batch Gradient Descent
  4. None
Ans: 2

16. NormalEquation class in scikit-learn solve linear regression using:
  1. Normal Equation
  2. SGD
  3. There is no NormalEquation class in scikit-learn
  4. Mini-Batch Gradient Descent
Ans: 3

17.Training Linear Regression model using Normal Equation is linear with both the number of training dataset that have to be made and the number of features:
  1. True
  2. False
Ans: 2

18.Prediction using Linear Regression model is linear with both the number of predictions that have to be made and the number of features:
  1. True
  2. False
Ans: 1

19.Which of these is more prone to overfitting?
  1. Linear Regression
  2. Polynomial Regression
Ans: 2

20. Naive Baye is?
  1. Conditional Independence
  2. Conditional Dependence
  3. Both 1 and 2
  4. None of the above
Ans:1


21. Naive Bayes requires?
  1. Categorical Values
  2. Numerical Values
  3. Either 1 or 2
  4. Both 1 and 2
Ans: 1


22. Probabilistic Model of data within each class is?
  1. Discriminative classification
  2. Generative classification
  3. Probabilistic classification
  4. Both 2 and 3
Ans: 4

23. A Classification rule is said?
  1. Discriminative classification
  2. Generative classification
  3. Probabilistic classification
  4. Both 1 and 3
Ans: 4

24. Spam Classification is an example for ?
  1. Naive Bayes
  2. Probabilistic condition
  3. Random Forest
  4. All the Above
Ans: 1.

25. NaΓ―ve Bayes Algorithm is a ________ learning algorithm.
  1. Supervised
  2. Reinforcement
  3. Unsupervised
  4. None of these
Ans: 1

26. Which of the following is correct about the Naive Bayes?
  1. Assumes that all the features in a dataset are independent
  2. Assumes that all the features in a dataset are equally important
  3. Both 1 and 2
  4. None of the above
Ans: 2

27. Types of NaΓ―ve Bayes Model:
  1. Gaussian
  2. Multinomial
  3. Bernoulli
  4. All of the above
Ans: 4

28. Disadvantages of NaΓ―ve Bayes Classifier:
  1. NaΓ―ve Bayes assumes that all features are independent or unrelated, so it cannot learn the relationship between features.
  2. It performs well in Multi-class predictions as compared to the other Algorithms.
  3. NaΓ―ve Bayes is one of the fast and easy ML algorithms to predict a class of datasets
  4. It is the most popular choice for text classification problems
Ans: 1

29. The benefit of NaΓ―ve Bayes:-
  1. NaΓ―ve Bayes is one of the fast and easy ML algorithms to predict a class of datasets.
  2. It is the most popular choice for text classification problems.
  3. It can be used for Binary as well as Multi-class Classifications.
  4. All of the above
Ans: 4

30. TPR =
  1. TP/(FN+TP)
  2. TN/(TN+FP)
  3. FP/(FP+TN)
  4. FN/(FN+TP)
Ans: 1

31. TNR=
  1. TP/(FN+TP)
  2. TN/(TN+FP)
  3. FP/(FP+TN)
  4. FN/(FN+TP)
Ans: 2

32. FPR=
  1. TP/(FN+TP)
  2. TN/(TN+FP)
  3. FP/(FP+TN)
  4. FN/(FN+TP)
Ans: 3

33. FNR=
  1. TP/(FN+TP)
  2. TN/(TN+FP)
  3. FP/(FP+TN)
  4. FN/(FN+TP)
Ans: 4

34. Precision =

  1. TP/(TP+FP)
  2. TP/(FN+TP)
  3. FP/(TP+FP)
  4. FP/(FN+TP)
Ans: 1

35. Recall=
  1. TP/(TP+FP)
  2. TP/(FN+TP)
  3. FP/(TP+FP)
  4. FP/(FN+TP)
Ans: 2


About Machine Learning

Welcome! Your Hub for AI, Machine Learning, and Emerging Technologies In today’s rapidly evolving tech landscape, staying updated with the ...