Download as PPTX, PDF, TXT or read online from Scribd
Download as pptx, pdf, or txt
You are on page 1of 16
Discuss all bout supervised and unsupervised ML algorithm
implementation with example
•Supervised and unsupervised machine learning are two broad
categories of machine learning algorithms.
•Supervised machine learning is a type of machine learning
where the algorithm is trained on a labeled dataset, where each data point is associated with a target variable or outcome.
•Here some SMLexample with IMPLEMENTATION
1. Linear regression is a type Spervised machine learning
algorithm used for predicting the value of a dependent variable based on the value of one or more independent variables. A dependent variable is the variable that we are trying to predict or explain. An independent variable, is also the variable that is used to predict or explain the dependent variable. The goal of linear regression is to find the best-fitting line that describes the relationship between the independent and dependent variables. Here is an example implementation of the linear regression algorithm: the steps 1. Data preparation: The first step is to prepare the data by cleaning and normalizing the data. 2. Feature selection: The next step is to select the independent variables that will be used to predict the dependent variable. 3 Model training: The next step is to train the model using the selected independent variables and the dependent variable. This is done by fitting the data to a linear equation of the form y = mx + b, where y is the dependent variable, x is the independent variable, m is the slope of the line, and b is the y- intercept. 4. Model evaluation: The next step is to evaluate the performance of the model by comparing the predicted values to the actual values 5. Model optimization: The final step is to optimize the model by adjusting the values of the coefficients in the linear equation to minimize the error. Here the implementation example of linear regression algorithm in Python using the scikit-learn library: ```from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error # Data preparation X = [[1], [2], [3], [4], [5]] y = [2, 3, 4, 5, 6] # Model training model = Linear Regression().fit(X, y) # Model evaluation y_pred = model.predict(X) mse = mean_squared_error(y, y_pred) print("Mean squared error:", mse) # Model optimization model.coef_ # This will print the coefficients of the linear equation 2. Logic regression is a type SM machine learning algorithm used for predicting the outcome of a binary classification problem. The goal of logic regression is to find the best combination of independent variables that can accurately predict the outcome of the dependent variable. Here is an example implementation of the logic regression algorithm 1. Data preparation: 2. Feature selection: The next step is to select the independent variables that will be used to predict the dependent variable. # Data preparation X = [[1], [2], [3], [4], [5]] y = [2, 3, 4, 5, 6] # Model training model = Linear Regression().fit(X, y) # Model evaluation y_pred = model.predict(X) mse = mean_squared_error(y, y_pred) print("Mean squared error:", mse) # Model optimization model.coef_ # This will print the coefficients of the linear equation This is a basic example of how the linear regression algorithm can be implemented. 3. evaluating the model performance 4. Model evaluation: 5. Model optimization: logical equation to minimize the error between the predicted and actual values. Here example of how the logic regression algorithm can be implemented in Python using the scikit-learn library: from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score, precision_score, recall_score # Data preparation X = [[0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1]] y = [0, 1, 0, 1] # Model training model = LogisticRegression().fit(X, y) # Cluster assignment y_pred = kmeans.predict(X) # Cluster optimization silhouette = silhouette_score(X, y_pred) print("Silhouette score:", silhouette 3. decision tree is a type SM machine learning algorithm used for both classification and regression problems. The goal of a decision tree is to create a tree-like model of decisions and their possible consequences, which can be used to make predictions about new data. Here is an example of how the decision tree algorithm can be implemented in Python using the scikit-learn library: from sklearn.tree import DecisionTreeClassifier y from sklearn.metrics import accuracy_score, precision_score, recall_score # Data preparation X = [[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 0, 0]] y = [0, 1, 0, 1] # Model training model = Logistic Regression().fit(X, y) # Model training model = DecisionTreeClassifier().fit(X, y) # Model evaluation y_pred = model.predict(X) accuracy = accuracy_score(y, y_pred) precision = precision_score(y, y_pred) recall = recall_score(y, y_pred) ("Accuracy:", accuracy) print("Precision:", precision) print("Recall:", recall) # Model optimization model.coef_ # This will print the coefficients of the logical equation Unsupervised machine learning, on the other hand, is a type of machine learning where the algorithm is trained . on an unlabeled dataset, where each data point is not associated with a target variable. The algorithm learns to identify patterns or structures in the data by clustering similar data points together or reducing the number of input variables while preserving the important information in the data. Unsupervised machine learning algorithms are used for tasks such as clustering and dimensionality reduction, where the goal is to group data points together based on their similarity or to reduce the number of input variables while preserving the important information in the data. Here these USML examples with implementation…. 1. Association rule mining is A UML technique used to identify relationships between variables in a dataset. The goal of association rule mining is to find rules that can be used to predict the occurrence of one variable based on the occurrence of another variable. Here is an example implementation of the Apriori algorithm, which is a popular algorithm for association rule mining: 1 Data transformation:. 2 Frequent itemset mining: The next step is to identify the frequent itemsets in the data4. Rule generation: The next step is to generate association rules from the frequent itemsets. 3 Rule evaluation: The next step is to evaluate the performance of the rules by calculating the support, confidence, and lift of each rule. Here example of how the Apriori algorithm can be implemented in Python using the mlxtend library: from mlxtend.frequent import apriori # Data preparation transactions = [['bread', 'ilk', 'eggs'], ['bread', 'cheese', 'eggs'], ['milk', 'cheese', 'eggs']] # Data transformation itemsets = apriori(transactions, min_support=2) # Rule generation rules = apriori(transactions, min_support=2, association_rules=True) # Rule evaluation support = [rule.support for rule in rules] confidence = [rule.confidence for rule in rules] lift = [rule.lift for rule in rules] print("Support:", support) 2. Clustering is a technique used to group similar data points together based on their characteristics. The goal of clustering is to identify patterns or structures in the data that can be used to segment the data into meaningful groups. Here is an example implementation of the K-means clustering algorithm: 1. Data preparation: 2. Model training: 3. Cluster assignment: The next step is to assign each data point to a cluster based on the distance between the data point and the centroid of the cluster. 4. Cluster update: The next step is to update the centroids of the clusters based on the data points assigned to each cluster. 5. Cluster optimization: 3. Principal Component Analysis (PCA) is a statistical technique used for dimensionality reduction while preserving as much variance as possible in the dataset. It transforms the original variables into a new set of uncorrelated variables called principal components, which are ordered by the amount of variance they capture from the data. PCA is widely used in exploratory data analysis, feature extraction, and data visualization. Implementation Example Let's implement PCA using Python's scikit-learn library with a synthetic dataset. Step 1: Import Libraries import numpy as np import pyplot as plt from sklearn.datasets import make_blobs from sklearn.decomposition import PCA Step 2: Create a Synthetic Dataset # Creating a synthetic dataset with 3 clusters X, y = make_blobs(n_samples=300, centers=3, cluster_std=1.0, random_state=42) Step 3: Visualize the Original Data # Visualizing the original dataset plt.scatter(X[:, 0], X[:, 1], s=30, c=y) plt.title('Original Dataset') plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.show() Step 4: Apply PCA
# Applying PCA to reduce dimensions to 2 components