Showing posts with label KNN. Show all posts
Showing posts with label KNN. 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.

Machine Learning MCQs-3 (Logistic Regression, KNN, SVM, Decision Tree)

 Machine Learning MCQs-3

 (Logistic Regression, KNN, SVM, Decision Tree)

---------------------------------------------------------------------

1.A Support Vector Machine can be used for

  1. Performing linear or nonlinear classification
  2. Performing regression
  3. For outlier detection
  4. All of the above
Ans: 4

2.  The decision boundaries in a Support Vector machine is fully determined (or “supported”) by the instances located on the edge of the street? 

  1. True
  2. False
Ans: 1 

3.  Support Vector Machines are not sensitive to feature scaling

  1. True
  2. False

Ans: 2 

4. If we strictly impose that all instances be off the street and on the right side, this is called

  1. Soft margin classification
  2. Hard margin classification
  3. Strict margin classification
  4. Loose margin classification

Ans: 2

5.The main issues with hard margin classification are

  1. It only works if the data is linearly separable
  2. It is quite sensitive to outliers
  3. It is impossible to find a margin if the data is not linearly separable
  4. All of the above

Ans: 4


6. The objectives of Soft Margin Classification are to find a good balance between

  1. Keeping the street as large as possible
  2. Limiting the margin violations
  3. Both of the above

Ans: 3

7. The balance between keeping the street as large as possible and limiting margin violations is controlled by this hyperparameter

  1. tol
  2. loss
  3. penalty
  4. C

Ans: 4

8. A smaller C value leads to a wider street but more margin violations.

  1. True
  2. False

Ans: 1

9. If your SVM model is overfitting, you can try regularizing it by reducing the value of

  1. tol
  2. C hyperparameter
  3. intercept_scaling
  4. None of the above

Ans: 2

10. A similarity function like Gaussian Radial Basis Function is used to

  1. Measure how many features are related to each other
  2. Find the most important features
  3. Find the relationship between different features
  4. Measure how much each instance resembles a particular landmark

Ans: 4

11. When using SVMs we can apply an almost miraculous mathematical technique for adding polynomial features and similarity features called the

  1. Kernel trick
  2. Shell trick
  3. Mapping and Reducing
  4. None of the Above

Ans: 1

12. Which is right for the gamma parameter of SVC which acts as a regularization hyperparameter

  1. If model is overfitting, increase it, if it is underfitting, reduce it
  2. If model is overfitting, reduce it, if it is underfitting, increase it
  3. If model is overfitting, keep it same
  4. If it is underfitting, keep it same

Ans: 2

13. LinearSVC is much faster than SVC(kernel="linear"))

  1. True
  2. False

Ans: 1

14. In SVM regression the model tries to

  1. Fit the largest possible street between two classes while limiting margin violations
  2. Fit as many instances as possible on the street while limiting margin violations

Ans: 2

15. Decision Trees can be used for

  1. Classification Tasks
  2. Regression Tasks
  3. Multi-output tasks
  4. All of the above

Ans: 4

16. The iris dataset has

  1. 5 features and 3 classes
  2. 4 features and 3 classes
  3. 2 features and 3 classes
  4. 4 features and 2 classes

Ans: 2

17.    A node’s gini attribute measures

  1. The number of training instances in the node
  2. The ratio of training instances in the node
  3. Its impurity

Ans: 3

18. If all the training instances of a node belong to the same class then the value of the node's Gini attribute will be

  1. 1
  2. 0
  3. Any integer between 0 and 1
  4. A negative value

Ans: 2

19. A Gini coefficient of 1 expresses maximal inequality among the training samples

  1. True
  2. False

Ans: 1

20. Gini index for a node is found by subtracting the sum of the square of ratio of each classes in a node from 1

  1. True
  2. False

Ans: 1

21. A decision tree estimates the probability that an instance belongs to a particular class k by finding the corresponding leaf node for the instance and then returning the ratio of training instances of class k

  1. True
  2. False

Ans: 1

22. The Decision Tree classifier predicts the class which has the highest probability

  1. True
  2. False

Ans: 1

23. The CART algorithm splits the training set in two subsets

  1. Using all the features and a threshold tk
  2. Using a single feature k and a threshold tk
  3. Using half of the features and a threshold k

Ans: 2

24. How does the CART algorithm chooses the feature k and the threshold tk for splitting ?

  1. It randomly chooses a feature k
  2. It chooses the mean of the values of the feature k as threshold
  3. It chooses the feature k and threshold tk which produces the purest subsets
  4. It chooses the feature k and threshold tk such that the gini index value of the subsets is 0

Ans: 3

25. The cost function for finding the value of feature k and threshold tk takes into consideration

  1. The Gini index values of the subsets
  2. The number of instances in the subsets
  3. The total number of instances in the node that is being split
  4. All of these

Ans: 4

26. Once the CART algorithm has successfully split the training set in two

  1. It stops splitting further
  2. It splits the subsets using the same logic, then the sub- subsets and so on, recursively
  3. It splits only the right subset
  4. It splits only the left subset

Ans: 2

27.The CART algorithm stops recursion once it reaches the maximum depth (defined by the max_depth hyperparameter), or if it cannot find a split that will reduce impurity

  1. True
  2. False

Ans: 1

28. Which of the following are correct for the CART algorithm

  1. It is a greedy algorithm
  2. It greedily searches for an optimum split at each level
  3. It does not check whether or not the split will lead to the lowest possible impurity several levels down
  4. All of the above are correct

Ans: 4

29. While making a prediction in Decision Tree, each node only requires checking the value of one feature

  1. True
  2. False

Ans: 1

30. Gini impurity is slightly faster to compute in comparison to entropy

  1. True
  2. False

Ans: 1

31. Models like Decision Tree models are often called nonparametric model because

  1. They do not have any parameters
  2. The number of parameters is not determined prior to training
  3. They have lesser parameters as compared to other models
  4. They are easy to interpret and understand

Ans: 2


KNN Machine Learning Program

KNN Classifier for IRIS Data Set

Steps:

  1. Import the library files
  2. Read the dataset (Iris Dataset) and analyze the data
  3. Preprocessing the data
  4. Divide the data into Training and Testing
  5. Build the model - KNN Classifier
  6. Model Evaluation

  1. Import the library files



2. Read the dataset (Iris Dataset) and analyze the data







3. Preprocessing the data





 







4. Divide the data into Training and Testing




5. Build the model - KNN Classifier

KNN Classifier

class sklearn.neighbors.KNeighborsClassifier(n_neighbors=5*weights='uniform'algorithm='auto'leaf_size=30p=2metric='minkowski'metric_params=Nonen_jobs=None)

Parameters:

n_neighborsint, default=5

Number of neighbors to use by default for kneighbors queries.

weights{‘uniform’, ‘distance’} or callable, default=’uniform’

Weight function used in prediction. Possible values:

  • ‘uniform’ : uniform weights. All points in each neighborhood are weighted equally.

  • ‘distance’ : weight points by the inverse of their distance. in this case, closer neighbors of a query point will have a greater influence than neighbors which are further away.

  • [callable] : a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.

algorithm{‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, default=’auto’

Algorithm used to compute the nearest neighbors:

  • ‘ball_tree’ will use BallTree

  • ‘kd_tree’ will use KDTree

  • ‘brute’ will use a brute-force search.

  • ‘auto’ will attempt to decide the most appropriate algorithm based on the values passed to fit method.

Note: fitting on sparse input will override the setting of this parameter, using brute force.

leaf_sizeint, default=30

Leaf size passed to BallTree or KDTree. This can affect the speed of the construction and query, as well as the memory required to store the tree. The optimal value depends on the nature of the problem.

pint, default=2

Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

metricstr or callable, default=’minkowski’

Metric to use for distance computation. Default is “minkowski”, which results in the standard Euclidean distance when p = 2. See the documentation of scipy.spatial.distance and the metrics listed in distance_metrics for valid metric values.

If metric is “precomputed”, X is assumed to be a distance matrix and must be square during fit. X may be a sparse graph, in which case only “nonzero” elements may be considered neighbors.

If metric is a callable function, it takes two arrays representing 1D vectors as inputs and must return one value indicating the distance between those vectors. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.

metric_paramsdict, default=None

Additional keyword arguments for the metric function.

n_jobsint, default=None

The number of parallel jobs to run for neighbors search. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details. Doesn’t affect fit method.




6. Model Evaluation



About Machine Learning

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