<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nikhil Babar on Medium]]></title>
        <description><![CDATA[Stories by Nikhil Babar on Medium]]></description>
        <link>https://medium.com/@nikkbabargccp?source=rss-8cc485f7e083------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*M2bz9h5ybXR3kwsT</url>
            <title>Stories by Nikhil Babar on Medium</title>
            <link>https://medium.com/@nikkbabargccp?source=rss-8cc485f7e083------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 07:46:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nikkbabargccp/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Docker — Basics]]></title>
            <link>https://medium.com/@nikkbabargccp/docker-basics-92ae192ec3b3?source=rss-8cc485f7e083------2</link>
            <guid isPermaLink="false">https://medium.com/p/92ae192ec3b3</guid>
            <category><![CDATA[docker-beginner]]></category>
            <category><![CDATA[docker-basic]]></category>
            <category><![CDATA[docker-image]]></category>
            <category><![CDATA[docker-swarm]]></category>
            <dc:creator><![CDATA[Nikhil Babar]]></dc:creator>
            <pubDate>Wed, 11 Dec 2024 17:37:39 GMT</pubDate>
            <atom:updated>2024-12-11T17:37:39.855Z</atom:updated>
            <content:encoded><![CDATA[<h3>Docker — Basics</h3><p>Key terms</p><h3><strong>What is Docker</strong></h3><p><strong>Docker</strong> is an open-source platform that enables developers to automate the deployment, scaling, and management of applications using <strong>containerization</strong>.</p><h3>Image</h3><p>A lightweight, standalone, and executable software package that includes everything needed to run an application (code, runtime, libraries, etc.).</p><ul><li>Think of it as a blueprint for a container.</li><li>Example: python:3.9, nginx, or custom-built images.</li></ul><h3><strong>Container</strong></h3><p>A runtime instance of a Docker image. It is a lightweight, isolated environment where your application runs.</p><ul><li>Containers share the host OS kernel but operate independently.</li><li>Commands: docker run, docker stop, docker rm.</li></ul><h3>Difference between Virtual Machine and Containers</h3><p><strong>Virtual Machines (VMs):</strong></p><ul><li><strong>Virtualization:</strong> Virtualizes the entire hardware layer, creating a virtual machine that emulates a physical machine.</li></ul><p><strong>Containers :</strong></p><ul><li><strong>Virtualization:</strong> Virtualizes the operating system-level, sharing the host OS kernel.</li></ul><h3>Example</h3><p>Think of a <strong>Docker image</strong> as a blueprint or a template for a house. It contains all the instructions and materials needed to build the house.</p><p>A <strong>Docker container</strong> is like the actual house built from that blueprint. It’s a running instance of the image, with its own environment and processes.</p><h3>Checking Running Containers</h3><ul><li><strong>docker ps</strong>: Lists the currently running containers.</li><li><strong>docker ps -a</strong>: Lists all containers, including those that are stopped, and provides logs about the runtime and stop time of containers.</li></ul><h3>Docker Run</h3><ul><li><strong>Creates a new container</strong>: Instantiates a new container from a specified image.</li><li><strong>Starts the container</strong>: Immediately starts the container’s primary process.</li><li><strong>docker run -d</strong>: Runs a Docker container in the background using the -d flag.</li></ul><h3>Docker Start</h3><ul><li><strong>Starts an existing container</strong>: Resumes a previously stopped container.</li></ul><h3>Stopping Containers</h3><ul><li><strong>docker stop &lt;container_name or container_id&gt;</strong>: Stops a running container.</li><li><strong>Stop all containers at once</strong>:</li><li>docker stop $(docker ps -q)</li></ul><h3>Copying Files</h3><ul><li><strong>docker cp &lt;source&gt; &lt;destination&gt;</strong>: Copies files between a container and the host machine.</li><li>Example:</li><li>docker cp 1bee2bba:nikk.txt new.txt</li><li>Here, 1bee2bba is the container ID, nikk.txt is the file inside the container, and new.txt is the destination file on the host machine.</li></ul><h3>Managing Images</h3><ul><li><strong>docker images</strong>: Lists all available images and their details.</li><li><strong>Committing changes</strong>:</li><li>docker commit &lt;container_id&gt; &lt;repository_name&gt;:&lt;tag_name&gt;</li><li>Example:</li><li>docker commit 1bee newRepo:10-12</li><li>This creates a new image with the repository name newRepo and the tag 10-12 from the container with ID 1bee.</li></ul><h3>Running Commands in a Container</h3><ul><li><strong>docker exec -it &lt;container_id or container_name&gt; &lt;command&gt;</strong>: Executes a command inside a running container.</li></ul><h3>Creating Custom Images</h3><ul><li><strong>Using a Dockerfile</strong>:</li><li>docker build -t &lt;image_name&gt; .</li><li>Example:</li><li>docker build -t python-hello-world .</li><li><strong>Sample Dockerfile</strong>:</li><li>FROM python:3.9-alpine RUN pip install --upgrade pip RUN pip install flask COPY app.py /app.py CMD [&quot;python&quot;, &quot;app.py&quot;]</li></ul><h3>Viewing Logs</h3><ul><li><strong>docker container logs &lt;container_id&gt;</strong>: Displays the logs for a specific container.</li><li>Example:</li><li>docker container logs 62f</li></ul><h3>Tagging Images</h3><ul><li><strong>docker tag &lt;source_image&gt; &lt;repository_name&gt;/&lt;image_name&gt;:&lt;tag&gt;</strong>:</li><li>Example:</li><li>docker tag python-hello-world nikkbabargccp/python-hello-world:latest</li></ul><h3>Docker Orchestration</h3><ul><li><strong>What is Docker Orchestration?</strong></li><li>Docker orchestration refers to managing and scheduling multiple containers across a cluster of machines. The primary tool for this in Docker is <strong>Docker Swarm</strong>.</li><li><strong>Key Commands for Docker Swarm</strong>:</li><li><strong>Initialize Swarm</strong>:</li><li>docker swarm init --advertise-addr eth0</li><li><strong>View Nodes in the Swarm</strong>:</li><li>docker node ls</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92ae192ec3b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hyper Parameter Tunning — GridSearch CV , RandomizedSearch CV]]></title>
            <link>https://medium.com/@nikkbabargccp/hyper-parameter-tunning-gridsearch-cv-randomizedsearch-cv-48f6c4013255?source=rss-8cc485f7e083------2</link>
            <guid isPermaLink="false">https://medium.com/p/48f6c4013255</guid>
            <category><![CDATA[randomizedsearchcv]]></category>
            <category><![CDATA[hyperparameter-tuning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[gridsearchcv]]></category>
            <category><![CDATA[parameter-tuning]]></category>
            <dc:creator><![CDATA[Nikhil Babar]]></dc:creator>
            <pubDate>Thu, 26 Sep 2024 13:32:03 GMT</pubDate>
            <atom:updated>2024-09-26T13:32:03.641Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hyper Parameter Tunning — GridSearch CV , RandomizedSearch CV</h3><p><strong>Hyper Parameter Tunning — Grid Search CV</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/0*pfKWh-YWvocShJSz.png" /></figure><p>Hyper Parameter Tunning :</p><p>Hyperparameter tuning is the process of optimizing the hyperparameters of a machine learning model to achieve the best performance.</p><p>Hyperparameters are parameters that are not learned from the data but are set prior to training, such as the learning rate in gradient descent or the depth of a decision tree. Proper tuning of hyperparameters can significantly improve a model’s accuracy and generalization ability.</p><p>The goal of hyperparameter tuning is to find the best combination of hyperparameters that maximizes the model’s predictive accuracy or minimizes its loss.</p><p>Hyperparameter tuning is an iterative process that often requires experimenting with different hyperparameter combinations to find the best ones for your specific problem and dataset. It can be computationally intensive, but it’s essential for achieving the best possible model performance.</p><p><strong>What is Hyperparameters :</strong></p><p>These are configuration settings used to control the training process of a model. Examples include learning rate, number of epochs, batch size, number of layers in a neural network, and regularization parameters.</p><p><strong>Common techniques and libraries used for hyperparameter tuning in Python include:</strong></p><p>Grid Search and Random Search using GridSearchCV and RandomizedSearchCV from scikit-learn.</p><p>Bayesian Optimization using libraries like hyperopt and scikit-optimize.</p><p>Gradient-based optimization algorithms (e.g., Adam, RMSprop) for neural networks.</p><p>Automated Hyperparameter Tuning Platforms like AutoML and cloud-based services like Google</p><p>Cloud AutoML and AWS Sage Maker.</p><p><strong>GridSearchCV -</strong></p><p>The GridSearchCV is the brute force method for finding optimal parameters of the model .</p><p>It invloves specifying a grid of hyperparameter values and training the model for every possible combination of these values.</p><p>for best prediction of your model you need to check in which parameters our model works efficiently or best . for that finding the parameters of that model we use Grid search CV .</p><p><strong>How to use GridSearchCV :</strong></p><p>Here is a step-by-step guide :</p><p>1) Define the model</p><p>2) Specify Hyperparameters and Their Ranges:(Create a parameter grid as a dictionary where keys are hyperparameter names and values are lists of possible values.)</p><p>3) Create a GridSearchCV object by providing the model, parameter grid, scoring metric (e.g., ‘accuracy’ for classification tasks or ‘mean_squared_error’ for regression tasks), and the number of cross-validation folds.</p><p>like this — grid_search = GridSearchCV(model, param_grid, scoring=’accuracy’, cv=5)</p><p>4) fit the model , after fitting, you can access the best hyperparameters and the best model using the best_params_ and best_estimator_ attributes of the GridSearchCV object.</p><p>best_params = grid_search.best_params_</p><p>best_model = grid_search.best_estimator_</p><p>5) and predict the which model is performing well</p><p>y_pred = best_model.predict(X_test)</p><p>accuracy = accuracy_score(y_test, y_pred)</p><p>GridSearchCV helps you systematically explore different combinations of hyperparameters and select the best ones based on cross-validated performance. It’s a powerful tool for improving the performance of your machine learning models.</p><p>Implementation :</p><pre>import numpy as np <br>import pandas as pd<br>from sklearn.datasets import load_iris<br>dataset = load_iris()<br>df = pd.DataFrame(dataset.data,columns=dataset.feature_names)<br>df[&quot;target&quot;]=dataset.target<br><br>X = df.drop([&quot;target&quot;],axis=1)<br>y= df[&quot;target&quot;]<br><br># Instead of train test split and manual parameter tunning we go for the Grid search cv which uses k fold cross validation<br><br>from sklearn.svm import SVC<br>from sklearn.model_selection import GridSearchCV<br><br>clf = GridSearchCV(SVC(gamma=&#39;auto&#39;), {<br>    &#39;C&#39;: [1,10,20],<br>    &#39;kernel&#39;: [&#39;rbf&#39;,&#39;linear&#39;]<br>}, cv=5, return_train_score=False)<br>clf.fit(X,y)<br>clf.cv_results_<br><br># by cv_results_ method you can print the result of model but it is difficult to view so that we can create the dataframe of this<br><br>df = pd.DataFrame(clf.cv_results_)<br>df[[&#39;param_C&#39;,&#39;param_kernel&#39;,&#39;mean_test_score&#39;]] ## taking only neccessary columns <br><br># this function tells the best parameter <br>clf.best_params_<br>Output : {&#39;C&#39;: 1, &#39;kernel&#39;: &#39;rbf&#39;}<br><br># this function tells the best score from model we getting<br>clf.best_score_ <br>Output : 0.9800000000000001<br><br># The Grid search cv works efficiently when we have the limited amount of data but when size of data is large or huge then we cant check all the possible combinations of parameters on the data this will take too much time to tunning the parameters<br><br> # Use RandomizedSearchCV to reduce number of iterations and with random combination of parameters.<br># This is useful when you have too many parameters to try and your training time is longer. It helps reduce the cost of computation<br><br>from sklearn.model_selection import RandomizedSearchCV<br>rs = RandomizedSearchCV(SVC(gamma=&#39;auto&#39;), {<br>        &#39;C&#39;: [1,10,20],<br>        &#39;kernel&#39;: [&#39;rbf&#39;,&#39;linear&#39;]<br>    }, <br>    cv=5, <br>    return_train_score=False, <br>    n_iter=2 ## number of combinations you want<br>)<br>rs.fit(X,y)<br>pd.DataFrame(rs.cv_results_)[[&#39;param_C&#39;,&#39;param_kernel&#39;,&#39;mean_test_score&#39;]]<br>Output :<br><br>param_C     param_kernel     mean_test_score<br>0 10         linear           0.973333<br>1 10         rbf              0.980000<br><br>## trying different models <br>from sklearn.ensemble import RandomForestClassifier<br>from sklearn.linear_model import LogisticRegression<br><br>## defining the parameter grid using the json object<br>model_params = {<br>    &#39;svm&#39;: {<br>        &#39;model&#39;: SVC(gamma=&#39;auto&#39;),<br>        &#39;params&#39; : {<br>            &#39;C&#39;: [1,10,20],<br>            &#39;kernel&#39;: [&#39;rbf&#39;,&#39;linear&#39;]<br>        }  <br>    },<br>    &#39;random_forest&#39;: {<br>        &#39;model&#39;: RandomForestClassifier(),<br>        &#39;params&#39; : {<br>            &#39;n_estimators&#39;: [1,5,10]<br>        }<br>    },<br>    &#39;logistic_regression&#39; : {<br>        &#39;model&#39;: LogisticRegression(solver=&#39;liblinear&#39;,multi_class=&#39;auto&#39;),<br>        &#39;params&#39;: {<br>            &#39;C&#39;: [1,5,10]<br>        }<br>    }<br>}<br><br>## creating a for loop for a storing all the results like best prameters of each model<br>scores = []<br><br>for model_name, mp in model_params.items():<br>    clf =  GridSearchCV(mp[&#39;model&#39;], mp[&#39;params&#39;], cv=5, return_train_score=False)<br>    clf.fit(X,y)<br>    scores.append({<br>        &#39;model&#39;: model_name,<br>        &#39;best_score&#39;: clf.best_score_,<br>        &#39;best_params&#39;: clf.best_params_<br>    })<br>    <br>df = pd.DataFrame(scores,columns=[&#39;model&#39;,&#39;best_score&#39;,&#39;best_params&#39;])<br>df<br>model                     best_score      best_params<br>0 svm                       0.980000       {&#39;C&#39;: 1, &#39;kernel&#39;: &#39;rbf&#39;}<br>1 random_forest             0.960000       {&#39;n_estimators&#39;: 10}<br>2 logistic_regression       0.966667       {&#39;C&#39;: 5}</pre><p>There are other techniques for hyperparameter tunning like</p><p><strong>Bayesian Optimization</strong>: Bayesian optimization uses probabilistic models to select the next set of hyperparameters to evaluate based on previous evaluations.</p><p><strong>Gradient-Based Optimization</strong>: Techniques such as gradient descent can be used for hyperparameter tuning, especially in deep learning where hyperparameters can be treated as differentiable parameters.</p><p>Thank You !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48f6c4013255" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[K -Mean Clustering -Unsupervised Machine Learning]]></title>
            <link>https://medium.com/@nikkbabargccp/k-mean-clustering-unsupervised-machine-learning-07d54d977ab5?source=rss-8cc485f7e083------2</link>
            <guid isPermaLink="false">https://medium.com/p/07d54d977ab5</guid>
            <category><![CDATA[k-means-algorithm]]></category>
            <category><![CDATA[clustering-algorithm]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[unsupervised-learning]]></category>
            <category><![CDATA[k-means-clustering]]></category>
            <dc:creator><![CDATA[Nikhil Babar]]></dc:creator>
            <pubDate>Thu, 26 Sep 2024 13:27:56 GMT</pubDate>
            <atom:updated>2024-09-26T13:27:56.359Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>K- Mean Clustering-Unsupervised Machine Learning</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/0*zDw2Dil1KbzZhPtF.png" /></figure><p>The K Mean Clustering is a unsupervised machine learning algorithm used for partitioning a dataset into distinct groups or clusters based on similarity.</p><p>The primary goal of K-Means is to group similar data points together and assign them to a cluster,</p><p>such that data points within the same cluster are more similar to each other than to those in other clusters.</p><p><strong>Steps for k means clustering</strong></p><p>1) Firstly we need to define the value of k . K is value of how many clusters or group you want to create.</p><p>2) Then we Randomly initialize K cluster centroids. These centroids represent the initial guesses for the cluster centers.</p><p>3) After that we calculate the Euclidian distance for each datapoint from the centroids.</p><p>whichever centroid distance is near from that datapoint will be added to that cluster</p><p>4) Calculate the mean (centroid) of all data points assigned to each cluster. This centroid becomes the new cluster center.</p><p>after that we repeat this steps until no data point is changed from one cluster to another</p><p>But do find the optimal value of k is challenging tasks because as we increase the value of k the chances of overfitting the data are increases.</p><p><strong>There are several methods used to calculate the value of k :</strong></p><p><strong>1) Elbow Method</strong> : also known as a the Inertia Method .</p><p>This is one of the most common methods for finding K. It involves running K-Means for a range of K values and plotting the inertia (sum of squared distances of data points to their assigned centroids) against K.</p><p>basically the elbow method use all the possible values of k starting from 2 . and we plot the graph for it the elbow point in the graph is the point in which the inertia(value of k) falling or slow down this is typically consider as optimal value of k .</p><p>Reference Image :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/323/0*BN5RX1SQHiUxDBee" /></figure><p><strong>2) Silhouette Score:</strong></p><p>The silhouette score measures how similar each data point in one cluster is to the other data points in the same cluster compared to the nearest neighboring cluster. The silhouette score ranges from -1 (a poor clustering) to +1 (a perfect clustering).</p><p>You can calculate the silhouette score for different K values and choose the K that maximizes the silhouette score.</p><p><strong>3) Hierarchical Clustering:</strong></p><p>Hierarchical clustering techniques (e.g., dendrogram analysis) can help you explore the structure of the data decide on the number of clusters by examining the dendrogram.</p><h3>Advantages and Limitations</h3><p><strong>Advantages</strong>:</p><ul><li>Simple and easy to implement.</li><li>Works well with large datasets.</li><li>Relatively efficient compared to hierarchical clustering.</li></ul><p><strong>Limitations</strong>:</p><ul><li>Requires the number of clusters K to be specified beforehand.</li><li>Sensitive to outliers and noise.</li><li>Can converge to local minima, depending on the initial centroid placement.</li><li>Not suitable for clusters of varying shapes and sizes.</li></ul><h3>Applications</h3><p>K-means clustering is used in various applications, such as:</p><ul><li>Market segmentation</li><li>Image compression</li><li>Anomaly detection</li><li>Document clustering</li><li>Customer segmentation</li></ul><p><strong>Implementation :</strong></p><pre>from sklearn.cluster import KMeans<br>from sklearn.preprocessing import MinMaxScaler<br>import pandas as pd <br>import numpy as np <br>%matplotlib inline <br>import matplotlib.pyplot as plt<br><br># We have dataset of income of persons the schema as follows ( Name , Age , Income($))<br>df = pd.read_csv(&quot;data/01_income.csv&quot;)<br><br># Lets visualize the plot to check how much clusters need to be  created <br>plt.scatter(df[&quot;Age&quot;],df[&quot;Income($)&quot;])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/0*73H8x4d8yOp-hwuq" /></figure><pre># As information from following image we can see that we have to make the 3 clusters <br>km = KMeans(n_clusters=3)<br><br># fit predict method used to fit the X , y into model and predict the results that the data points belongs to which cluster<br><br>y_predicted = km.fit_predict(df[[&quot;Age&quot;,&quot;Income($)&quot;]])<br>y_predicted<br><br>array([1, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2])<br>df[&quot;cluster&quot;]= y_predcited<br><br>## Creating separate df for each cluster<br> <br>df1 = df[df.cluster==0]<br>df2 = df[df.cluster==1]<br>df3 = df[df.cluster==2]<br>plt.scatter(df1.Age,df1[&#39;Income($)&#39;],color=&#39;red&#39;)<br>plt.scatter(df2.Age,df2[&#39;Income($)&#39;],color=&#39;blue&#39;)<br>plt.scatter(df3.Age,df3[&#39;Income($)&#39;],color=&#39;yellow&#39;)<br>plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color=&#39;purple&#39;,marker=&#39;*&#39;,label=&#39;centroid&#39;)<br>plt.xlabel(&#39;Age&#39;)<br>plt.ylabel(&#39;Income ($)&#39;)<br>plt.legend()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/0*_1bjncpT64EhIf9h" /></figure><p>As we can see the data is spread and not forming any group that is because our values have big ranges <br>we need to scale this datapoints values for that we use min max scaler this will scale the parameter between 0 to 1 so we can get perfect cluster .</p><pre>scaler = MinMaxScaler()<br>scaler.fit(df[[&quot;Age&quot;]])<br>df[&quot;Age&quot;] = scaler.transform(df[[&quot;Age&quot;]])<br><br>scaler.fit(df[[&#39;Income($)&#39;]])<br>df[&#39;Income($)&#39;] = scaler.transform(df[[&#39;Income($)&#39;]])<br><br>Now Again predict with scaled values <br><br>km = KMeans(n_clusters=3)<br>y_predicted_new = km.fit_predict(df[[&#39;Age&#39;,&#39;Income($)&#39;]])<br>y_predicted_new<br><br>array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2])<br>df[&quot;cluster&quot;]= y_predicted_new<br><br># from this method we can calculate the centroid of cluster<br>km.cluster_centers_<br><br>array([[0.1372549 , 0.11633428],<br>       [0.72268908, 0.8974359 ],<br>       [0.85294118, 0.2022792 ]])<br><br>df1 = df[df.cluster==0]<br>df2 = df[df.cluster==1]<br>df3 = df[df.cluster==2]<br>plt.scatter(df1.Age,df1[&#39;Income($)&#39;],color=&#39;green&#39;)<br>plt.scatter(df2.Age,df2[&#39;Income($)&#39;],color=&#39;red&#39;)<br>plt.scatter(df3.Age,df3[&#39;Income($)&#39;],color=&#39;black&#39;)<br>plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color=&#39;purple&#39;,marker=&#39;*&#39;,label=&#39;centroid&#39;)<br>plt.legend()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/0*PbJIGj1Dh-4XsM0S" /></figure><p>The Now we clearly see that the points and cluster centers which forms perfect clusters .</p><p><strong>Let’s Plot the Elbow plot</strong></p><pre>sse=[]<br>k_rng = range(2,10)<br>for k in k_rng:<br>    km = KMeans(n_clusters=k)<br>    km.fit(df[[&#39;Age&#39;,&#39;Income($)&#39;]])<br>    sse.append(km.inertia_) # Inertia (sum of squared distances of data points to their assigned centroids)<br><br>## elbow graph <br>plt.xlabel(&#39;K&#39;)<br>plt.ylabel(&#39;Sum of squared error&#39;)<br>plt.plot(k_rng,sse)<br><br>Using Silhouette Score method  : <br>from sklearn.metrics import silhouette_score<br>sil_score = []<br>k_rng = range(2, 10)<br>for k in k_rng:<br>    k_means = KMeans(n_clusters=k , random_state=42 )<br>    k_means.fit(df[[&#39;Age&#39; ,&#39;Income($)&#39;]])<br>    labels = k_means.labels_<br>    score = silhouette_score( df[[&#39;Age&#39; ,&#39;Income($)&#39;]] ,labels  )<br>    sil_score.append(score)<br><br># Plotting the silhouette scores<br>plt.plot(k_rng, sil_score, marker=&#39;o&#39;)<br>plt.xlabel(&#39;Number of clusters, k&#39;)<br>plt.ylabel(&#39;Silhouette Score&#39;)<br>plt.title(&#39;Silhouette Score vs. Number of Clusters&#39;)<br>plt.show()<br><br># Find the optimal number of clusters<br>optimal_k = k_rng[np.argmax(sil_score)]<br>print(f&#39;The optimal number of clusters is {optimal_k}&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/0*ySd2H_z7ZdtXT0iq" /></figure><p>From above graph we can see that from K =3 the Intertia starting to falling down or decrease.4</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/0*14MDwxU7TuySQzpg" /></figure><p>As we clearly see that silhouette score is highest when the k = 3 so the optimal value of k is 3.</p><p>Thank You !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=07d54d977ab5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Naive Bayes Classifier — Supervised Machine Learning Algorithm]]></title>
            <link>https://medium.com/@nikkbabargccp/the-naive-bayes-classifier-supervised-machine-learning-algorithm-3bd94a3c4f39?source=rss-8cc485f7e083------2</link>
            <guid isPermaLink="false">https://medium.com/p/3bd94a3c4f39</guid>
            <category><![CDATA[naive-bayes-in-python]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[email-classification]]></category>
            <category><![CDATA[naive-bayes-classifier]]></category>
            <category><![CDATA[naive-bayes-algorithm]]></category>
            <dc:creator><![CDATA[Nikhil Babar]]></dc:creator>
            <pubDate>Thu, 26 Sep 2024 13:22:35 GMT</pubDate>
            <atom:updated>2024-09-26T13:22:35.652Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Naive Bayes Classifier — Supervised Machine Learning Algorithm</h3><p><strong>The Naive Bayes Classifier — Supervised Machine Learning Algorithm</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/0*zikHTtFRYYmLp0xY.png" /></figure><p>The Naive Bayes Classifier is a popular machine learning algorithm used for the classification tasks particularly in text classification and spam email detection . Its based on Bayes theorem and is called “naive” because features used for classification are conditionally independent given the class label.</p><p>The Naive Bayes classifier uses probabilities to predict whether data belongs to a particular category, making it useful for tasks like spam detection.</p><p>The Gaussian naive bayes is performed well on the numerical data</p><p>Bayes Theorem:</p><p>The algorithm is based on Bayes theorem, which describes the probability of an event, based on prior knowledge of conditions ​ might be related to the event. Mathematically, it is represented as:</p><p>P(A∣B)=P(B∣A)⋅P(A)P(B)P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)}</p><p>Where:</p><ul><li>P(A|B)P(A∣B) is the probability of event A occurring when B is true.</li><li>P(B|A)P(B∣A) is the probability of event B occurring when A is true.</li><li>P(A) is the probability of event A.</li><li>P(B)P(B) is the probability of event B.</li></ul><p><strong>Types of Naive Bayes Classifiers:</strong></p><p>There are different variations of Naive Bayes classifiers, depending on the types of features and data distribution:</p><p>1) Multinomial Naive Bayes: Used for text classification, where features represent word counts or frequencies.</p><p>2) Gaussian Naive Bayes: Assumes that features follow a Gaussian (normal) distribution.</p><p>3) Bernoulli Naive Bayes: Suitable for binary data, where features are binary (0 or 1).</p><p>4) Categorical Naive Bayes: Used for categorical data with more than two categories.</p><h3>Steps to Implement a Naive Bayes Classifier</h3><ol><li><strong>Training Phase</strong>:</li></ol><ul><li>Calculate the prior probability for each class.</li><li>Calculate the likelihood of each feature given each class.</li><li>Calculate the marginal likelihood of each feature.</li></ul><p><strong>2. Prediction Phase</strong>:</p><ul><li>Apply Bayes’ Theorem to compute the posterior probability for each class given the observed features.</li><li>Choose the class with the highest posterior probability.</li></ul><p><strong>Mostly used in following topics -</strong></p><p>text-classification ,</p><p>handwritten digits recognition ,</p><p>email spam detection ,</p><p>whether prediction ,</p><p>face recognition and news article categorization</p><p><strong>Implementation using Scikit Learn -</strong></p><pre>from sklearn.feature_extraction.text import CountVectorizer <br><br>from sklearn.naive_bayes import MultinomialNB<br><br>we have spam email dataset which has category (spam or not spam) and email <br><br>import pandas as pd <br><br>df_email = pd.read_csv(&quot;data/10_spam.csv&quot;)<br><br># Preprocessing <br><br>df_dummies = pd.get_dummies(df_email[&#39;Category&#39;]).astype(int)<br><br>df_dummies.drop(&#39;ham&#39; , axis= 1 , inplace = True)<br><br>df_email[&#39;spam&#39;]= df_dummies<br><br>df_email.drop(&#39;Category&#39; , axis= 1 ,inplace = True)</pre><p>Now we converting the Message(text data ) into numerical data in order to do this we use the Count Vectorizer</p><p>— — Count Vectorizer</p><p>It is a text preprocessing technique used in natural language processing (NLP) to convert text data into a numerical format that can be used by machine learning algorithms. It represents the text documents as a matrix of token counts, where each row corresponds to a document, and each column corresponds to a unique word (token) in the entire corpus of documents</p><pre>vectorizer = CountVectorizer() <br><br>X = vectorizer.fit_transform(df_email[&#39;emails&#39;])<br><br>y = df_email[&#39;spam&#39;]<br><br>from sklearn.model_selection import cross_val_score<br><br>from sklearn.model_selection import StratifiedKFold<br><br>k = 10 <br><br>skf = StratifiedKFold(n_splits=k, shuffle=True, random_state=42)<br><br>scores = cross_val_score(MultinomialNB()  ,X ,y ,cv=skf  )<br><br>scores.mean()<br><br>Output : 0.9822303945226283</pre><p>Thank You !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3bd94a3c4f39" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[K Fold Cross Validation — KFold , StratifiedKFold]]></title>
            <link>https://medium.com/@nikkbabargccp/k-fold-cross-validation-kfold-stratifiedkfold-d2a96b18f368?source=rss-8cc485f7e083------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2a96b18f368</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[k-cross-validation]]></category>
            <category><![CDATA[stratified-k-fold]]></category>
            <category><![CDATA[k-fold-cross-validation]]></category>
            <dc:creator><![CDATA[Nikhil Babar]]></dc:creator>
            <pubDate>Thu, 26 Sep 2024 13:19:19 GMT</pubDate>
            <atom:updated>2024-09-26T13:19:19.916Z</atom:updated>
            <content:encoded><![CDATA[<h3>K Fold Cross Validation — KFold , StratifiedKFold</h3><h3>K Fold Cross Validation in Machine Learning</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*zJsCIcb9UxALHTmk.png" /></figure><p><strong>We want to know which model is performing well for the particular data or to evaluate the model we uses K fold cross validation technique. It used to estimate how the model is generalize for the unseen new data .</strong></p><p>What the actually the cross validation do is to divide the whole data into equal number of folds which is k and then uses 1 fold for the testing and remaining for the training after that note down the score for this then taking the second fold as testing as we take nth fold for testing and note down score for each and lastly average it .</p><p>How K-Fold Cross Validation Works:</p><p>1) Splitting the Data: Into K Parts</p><p>2) Training and validation : k-1 parts used for training and remaining used for</p><p>3) Repeating the process k times</p><p>4) Averaging the results</p><p>Advantages of K-Fold Cross-Validation:</p><p>1) Robustness : K-Fold Cross-Validation provides a more robust estimate of a models performance compared to a single train-test split.</p><p>2) Optimizing Hyperparameters: It is often used in hyperparameter tuning to select the best combination of hyperparameters for a model.</p><p>3) Maximizes Data Usage: K-Fold Cross-Validation makes efficient use of the available data,</p><p>as each data point is used for both training and testing at least once.</p><p>Implementation Using Scikit Learn :</p><pre>from sklearn.linear_model import LogisticRegression<br>from sklearn.ensemble import RandomForestClassifier<br>import numpy as np<br>from sklearn.datasets import load_digits<br>import warnings<br><br>digits = load_digits()<br><br>from sklearn.model_selection import train_test_split<br>X_train, X_test, y_train, y_test = train_test_split(digits.data,digits.target,test_size=0.3)<br><br>lr = LogisticRegression()<br>lr.fit(X_train,y_train)<br>lr.score(X_test,y_test)<br><br>Output : 0.9611111111111111<br><br>## Random Forest Classifier<br>rf = RandomForestClassifier(n_estimators=40)<br>rf.fit(X_train, y_train)<br>rf.score(X_test, y_test)<br><br>Output : 0.9722222222222222<br><br>#Every time i rerun the train test split the score of models are changing<br># the score is not uniform for that we are using the k Fold cross validation. <br><br>from sklearn.model_selection import KFold<br>kf =KFold(n_splits=3)<br><br>for train_index, test_index in kf.split([1,2,3,4,5,6,7,8,9]):<br>    print(train_index, test_index)<br><br>[3 4 5 6 7 8] [0 1 2]<br>[0 1 2 6 7 8] [3 4 5]<br>[0 1 2 3 4 5] [6 7 8]<br><br>#Above we are seeing that how the k fold cross validation works it created 3 <br># equal splits that 1 uses for test and remaining for training now we are doing the same thing with the digits dataset<br> <br>from sklearn.model_selection import cross_val_score<br><br># Using the KFold<br><br>k = 5<br>kf = KFold(n_splits=k, shuffle=True, random_state=42)<br><br># using the StratifiedKFold<br><br>skf = StratifiedKFold(n_splits=k, shuffle=True, random_state=42)<br><br># The This method ensures that each fold has a similar distribution of target classes as the original dataset.<br><br>cross_val_score(LogisticRegression(),digits.data,digits.target  ,cv=skf , shuffle =False )<br><br># Here cv defines type of fold and n_splits and LogisticRegression() used as a estimator <br><br>output : array([0.90555556, 0.96111111, 0.87777778, 0.92777778, 0.94444444,<br>       0.96666667, 0.95      , 0.93854749, 0.87150838, 0.93854749])<br># with this statement you can calculate the mean <br>lr_score.mean()<br>output : 0.92819<br> <br>#same for RandomForestClassifier <br>score_rfc= cross_val_score(RandomForestClassifier(n_estimators=40),digits.data,digits.target)<br>np.mean(score_rfc)<br> output : 0.9354719900959456</pre><p>Thank You !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2a96b18f368" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Random Forest Algorithm : Supervised Machine learning Algorithm]]></title>
            <link>https://medium.com/@nikkbabargccp/random-forest-algorithm-supervised-machine-learning-algorithm-725f0ba70094?source=rss-8cc485f7e083------2</link>
            <guid isPermaLink="false">https://medium.com/p/725f0ba70094</guid>
            <category><![CDATA[supervised-learning]]></category>
            <category><![CDATA[random-forest]]></category>
            <category><![CDATA[ensemble-learning]]></category>
            <category><![CDATA[classification-algorithms]]></category>
            <category><![CDATA[random-forest-classifiers]]></category>
            <dc:creator><![CDATA[Nikhil Babar]]></dc:creator>
            <pubDate>Thu, 26 Sep 2024 13:15:55 GMT</pubDate>
            <atom:updated>2024-09-26T13:15:55.203Z</atom:updated>
            <content:encoded><![CDATA[<h3>Random Forest Algorithm : Supervised Machine learning Algorithm</h3><p><strong>Random Forest Algorithm : Supervised Machine learning Algorithm</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*r1ZNX-7WYgW0Fe1M.png" /></figure><p>The forest term represents the place which has vast number of trees , In our case the trees are nothing but (Decision Tree algorithm ) and after combining more decision trees we get Random Forest algorithm .</p><p>The combining of multiple models is also known as <strong>Ensemble learning </strong>.</p><p><strong>What is Ensemble Learning </strong>: It is nothing but combining of multiple models prediction to increase performance of final model . As the each model extract some different relationship from the data as we are combining it we get most robust model .</p><p>Example : You are going to watch a latest movie but don’t know which is better so you took suggestion from your friends and watch movie which suggested by majority of the friends .</p><p>There are mainly 2 techniques used in the Ensemble Learning :</p><p><strong>1) Bagging :</strong></p><p>In bagging we train different subset of data on the same algorithm and then combining their predictions.</p><p>It aims to reduce the variance .</p><ul><li>For <strong>classification</strong>, combine the predictions of the individual models by majority voting.</li><li>For <strong>regression</strong>, average the predictions of the individual models.</li></ul><p><strong>2)</strong> <strong>Boosting :</strong></p><p>Boosting aims to reduce both bias and variance of machine learning model . In which models are trained sequentially where each model corrects the errors made by the previous ones.</p><p>Firstly we train base model on the dataset and then feed this predicted point to the next model . The next model more focus on points misclassified or error made by the previous model .</p><p><strong>How Random Forest Works:</strong></p><p><strong>Training :</strong></p><p><strong>Bootstrap Sampling</strong>: Create multiple bootstrap samples from the original training dataset.</p><p><strong>Tree Construction</strong>: Train a decision tree on each bootstrap sample. During the construction of each tree, a random subset of features is considered for splitting at each node.</p><p><strong>Forest Creation</strong>: Combine all the trained decision trees to form a Random Forest.</p><p><strong>Prediction Phase</strong>:</p><p>For Classification tasks , Each model predicts the class and we use voting mechanism for the final result or outcome .</p><p>For regression tasks, each tree predicts a continuous value. The final prediction is the average of all the predictions from the trees.</p><p><strong>Advantages of Random Forest:</strong></p><ul><li><strong>High Accuracy</strong>: By combining multiple decision trees, Random Forest generally achieves higher accuracy and robustness compared to individual decision trees.</li><li><strong>Reduces Overfitting</strong>: The randomness in both data sampling and feature selection helps in reducing overfitting, making it suitable for complex tasks.</li><li><strong>Handles Missing Values</strong>: Random Forest can handle missing values effectively by assigning them to the most frequent class in the trees.</li><li><strong>Feature Importance</strong>: It provides an indication of the importance of different features in making predictions, which can be valuable for understanding the data.</li></ul><p><strong>Disadvantages of Random Forest:</strong></p><ul><li><strong>Computationally Intensive</strong>: Training multiple decision trees can be computationally expensive and time-consuming, especially with large datasets.</li><li><strong>Less Interpretability</strong>: The ensemble of many trees (Random Forest) can become harder to understand the underlying decision process.</li></ul><h3>Applications of Random Forest:</h3><ul><li><strong>Classification</strong>: Email spam detection, image classification, disease diagnosis.</li><li><strong>Regression</strong>: Stock price prediction, house price prediction, predicting sales numbers.</li><li><strong>Feature Selection</strong>: Identifying the most important features in a dataset for further analysis or model building.</li></ul><p><strong>Implementation :</strong></p><pre>import numpy as np<br>import pandas as pd<br>from sklearn.model_selection import train_test_split<br>from sklearn.ensemble import RandomForestClassifier<br>from sklearn.metrics import accuracy_score, confusion_matrix, classification_report<br>from sklearn.datasets import load_iris<br><br># Load the dataset<br>iris = load_iris()<br>X = iris.data<br>y = iris.target<br><br># Split the dataset into training and testing sets<br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)<br><br># Initialize the Random Forest classifier<br>rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)<br><br>#Here n_estimators - number of trees in the forest  , Default =100 <br><br># Train the model<br>rf_classifier.fit(X_train, y_train)<br><br># Make predictions<br>y_pred = rf_classifier.predict(X_test)<br><br># Evaluate the model<br>accuracy = accuracy_score(y_test, y_pred)<br>cm = confusion_matrix(y_test, y_pred)<br>print(f&quot;Accuracy: {accuracy}&quot;)<br><br><br>Accuracy: 1.0</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/0*9hJEKPy4BBsBPniP" /></figure><p>We can use Hyperparameter tunning to tune the parameter used for Random Forest Algorithm</p><p><strong>Thank You !</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=725f0ba70094" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Support vector machine — (SVM) Supervised machine learning]]></title>
            <link>https://medium.com/@nikkbabargccp/support-vector-machine-svm-supervised-machine-learning-6ca6dcb481dd?source=rss-8cc485f7e083------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ca6dcb481dd</guid>
            <category><![CDATA[supervised-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[support-vector-machine]]></category>
            <category><![CDATA[classification-algorithms]]></category>
            <category><![CDATA[svm-algorithm]]></category>
            <dc:creator><![CDATA[Nikhil Babar]]></dc:creator>
            <pubDate>Thu, 26 Sep 2024 13:06:40 GMT</pubDate>
            <atom:updated>2024-09-26T13:06:40.362Z</atom:updated>
            <content:encoded><![CDATA[<h3>Support vector machine — (SVM) Supervised machine learning</h3><p><strong>Support vector machine — (SVM) Supervised machine learning</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/0*LOxHsHP0xGOemJKS.png" /></figure><p>The SVM is powerful machine learning algorithm used to solve both classification and regression problems . It is useful when we are dealing with high dimensional data .</p><p>It is known for the ability to separate two classes by creating hyperplane between them and maximizing the margin between data points and decision boundary .</p><p>It basically draws a line that best separate the classes by maximizing the margin .</p><p><strong>Support Vectors</strong>: The data points that are closest to the hyperplane are called <strong>support vectors</strong>. They are critical because they define the position and orientation of the hyperplane.</p><p>— The support vector draw the hyperplane in n dimension that maximize the margin between classification groups.</p><p><strong>Hyperplane </strong>-</p><p>The hyperplane is the decision boundary that separates data points into two classes.</p><p>The goal of SVM is to find the hyperplane that maximizes the margin, which is the distance between the hyperplane and the nearest data points from each class.</p><p><strong>Kernel </strong>:</p><p>SVM can use a kernel function to transform the original feature space into a higher-dimensional space . This allows SVM to find non-linear decision boundaries in the transformed space, making it a versatile algorithm for various types of data.</p><p>Suppose some time the data is complex and not fit by the line(2 dimensional) then we use kernel function to add 3 rd dimensional that can classify the data into the better way .</p><p><strong>Margin </strong>-</p><p>The margin is the space between the support vectors and the decision boundary. SVM aims to maximize this margin because a larger margin typically results in better generalization to unseen data.</p><p><strong>C Parameter:</strong></p><p>The C parameter controls the trade-off between maximizing the margin and minimizing the classification error.</p><p>Smaller values of C prioritize a larger margin but may allow some misclassification,</p><p>while larger values of C aim to minimize misclassification even if it means a smaller margin.</p><p>Minimizing Misclassification (overfitting) : When you set C to a large value, the SVM cares a lot about getting every data point in the right group. It’s like trying to fit everything perfectly, even if it means having a smaller margin.</p><p>In simple words, a small C says, “I want a bigger gap between groups, even if a few points are on the wrong side.” A large C says, “I want to classify everything perfectly, even if it means a smaller gap.”</p><p><strong>Applications of Support Vector Machines include:</strong></p><p>Text classification and sentiment analysis.</p><p>Image classification and object detection.</p><p>Handwriting recognition.</p><p>Bioinformatics, such as protein structure prediction.</p><p>Financial forecasting and anomaly detection.</p><p><strong>To use SVM in practice, you typically follow these steps:</strong></p><p>Collect and preprocess your data.</p><p>Choose an appropriate kernel function and SVM parameters (e.g., C parameter).</p><p>Train the SVM model on your labeled training data.</p><p>Validate and tune the model using cross-validation techniques.</p><p>Make predictions on new, unseen data.</p><p><strong>Implementation of SVM :</strong></p><pre>import pandas as pd <br>from sklearn.datasets import load_iris<br>import matplotlib.pyplot as plt<br>%matplotlib inline<br><br>iris = load_iris()<br><br>## creating the dataframe of that data<br>df = pd.DataFrame(iris.data , columns=iris.feature_names) <br><br> ## appending the target to dataframe<br>df[&quot;target&quot;] = iris.target<br><br>## Creating datafram for each target value<br>df[&quot;Flower_names&quot;] = df.target.apply(lambda x : iris.target_names[x])<br>df0= df[df.Flower_names==0]<br>df1= df[df.Flower_names==1]<br>df2= df[df.Flower_names==2]<br><br>## Scatter plots <br>plt.title(&quot;Scatter plot of sepal lenth of setosa and versicolor&quot;)<br>plt.xlabel(&quot;sepal length (cm)&quot;)<br>plt.ylabel(&quot;sepal width (cm)&quot;)<br>plt.scatter(df0[&quot;sepal length (cm)&quot;],df0[&quot;sepal width (cm)&quot;],marker=&quot;+&quot;, color=&quot;red&quot;)<br>plt.scatter(df1[&quot;sepal length (cm)&quot;],df1[&quot;sepal width (cm)&quot;],marker=&quot;.&quot;, color=&quot;blue&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/0*RvsRfiYFGjBqZ-LW.png" /></figure><pre>plt.title(&quot;Scatter plot of petal lenth of setosa and versicolor&quot;)<br>plt.xlabel(&quot;petal length (cm)&quot;)<br>plt.ylabel(&quot;petal width (cm)&quot;)<br>plt.scatter(df0[&quot;petal length (cm)&quot;],df0[&quot;petal width (cm)&quot;],marker=&quot;+&quot;, color=&quot;red&quot;)<br>plt.scatter(df1[&quot;petal length (cm)&quot;],df1[&quot;petal width (cm)&quot;],marker=&quot;.&quot;, color=&quot;blue&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/0*-WU4h6foNZhDvgd6.png" /></figure><p><strong>From above graphs we can clearly saw that we can classify the dataset into groups using the svm algorithm</strong></p><pre>## Splitting the dataset<br>from sklearn.model_selection import train_test_split<br>X = df.drop([&#39;target&#39;,&#39;Flower_names&#39;], axis=&#39;columns&#39;)<br>y = df.target<br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)<br><br>## Importing the model <br>from sklearn.svm import SVC<br>model = SVC() ## here we can use kernel=&#39;rbf&#39;  or kernel = &#39;linear&#39; and the c parameter <br>model.fit(X_train, y_train)<br><br>#evaluate the model <br>model.score(X_test, y_test)<br>output : 1.0</pre><p>As we increased the c parameter to train our model very well on the trainning dataset. but there is risk of overfitting</p><p>Thank You !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ca6dcb481dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Decision Tree Algorithm — Supervised machine learning]]></title>
            <link>https://medium.com/@nikkbabargccp/decision-tree-algorithm-supervised-machine-learning-41e8cdc7935f?source=rss-8cc485f7e083------2</link>
            <guid isPermaLink="false">https://medium.com/p/41e8cdc7935f</guid>
            <category><![CDATA[decision-tree]]></category>
            <category><![CDATA[classification-algorithms]]></category>
            <category><![CDATA[decision-tree-algorithm]]></category>
            <category><![CDATA[ensemble-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Nikhil Babar]]></dc:creator>
            <pubDate>Thu, 26 Sep 2024 13:03:55 GMT</pubDate>
            <atom:updated>2024-09-26T13:03:55.342Z</atom:updated>
            <content:encoded><![CDATA[<h3>Decision Tree Algorithm — Supervised machine learning</h3><p><strong>Decision Tree Algorithm — Supervised machine learning</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*T6WUN_pdDtO5M_pH.png" /></figure><p>Decision tree is most popular supervised machine learning algorithm used for classification as well regression tasks . It works by dividing the data into subsets based on input . It creates a tree like structure that used for decision making process .</p><p><strong>Key Parts of a Decision Tree</strong></p><ol><li><strong>Root Node</strong>: The starting point of the tree. The topmost node in a decision tree that represents the entire dataset</li><li><strong>Decision Nodes</strong>: Points in the tree where a decision is made, splitting the data based on a specific feature.</li><li><strong>Leaf Nodes</strong>: End points of the tree that give the final outcome. This can be value or predicted class .</li><li><strong>Splitting : </strong>The process of dividing the node based on certain rules or conditions .</li><li><strong>Pruning</strong>: It does to reduce complexity of decision tree . It removes the sub-nodes from the tree .</li></ol><p><strong>Important Terms :</strong></p><p><strong>Entropy</strong>: A measure of impurity or disorder in a dataset.</p><p>Decision Trees aim to minimize entropy by making splits that result in more homogeneous subsets (Homogenous means a balanced dataset ) .</p><p>It used because we want purest subset of data that represent towards a particular outcome .</p><p><strong>Gini Impurity</strong> (Classification):</p><ul><li>Measures the impurity or impurity of a node, with a lower Gini impurity indicating a better split.</li></ul><p>Gini=1−∑i=1npi2​</p><p>Where pi​ is the probability of an element being classified to a particular class.</p><p><strong>Information Gain</strong> (Classification):</p><p>IG=Entropy(parent)−∑k∣childk∣∣parent∣Entropy(childk)<br>IG = Entropy(parent) — \sum_{k} \frac{|child_k|}{|parent|} Entropy(child_k)</p><p>It simply measures how much entropy is reduced after the split . when the entropy is less information gain is more . Decision Trees select the feature with the highest information gain to split on.</p><p><strong>How It Works:</strong></p><p><strong>1) Selecting the best split :</strong></p><p>The algorithm selects the best features to split measured by a criterion like Gini impurity or Information Gain for classification, and Mean Squared Error (MSE) for regression.</p><p><strong>2) Recursive splitting :</strong></p><p>The process of splitting the nodes into sub nodes is recursively continues until the stopping criterion is met ( maximum depth, minimum number of samples per node)</p><p>3) <strong>Making Predictions:</strong></p><p>For provided given input model traverse through the tree from root node to leaf node and return outcome at leaf node .</p><p><strong>Example :</strong></p><p>Suppose we have to classify the persons salary is more than 100k or not . For that we have features like company , job and degree . the model use features to classify the data . If the company is google and for particular job they have given a more than 100k salary so model used company as a feature to split</p><p><strong>Implementation Using Scikit Learn :</strong></p><pre>import pandas as pd<br><br>#Here is the data overview <br><br>df = pd.read_csv(&quot;data/salaries.csv&quot;)<br><br>df.head(10)<br><br><br># we are creating x(inputs) and y(label or target varible) for the dataset<br>input= df.drop(&quot;salary_more_then_100k&quot;, axis=1)<br>target = df[&quot;salary_more_then_100k&quot;]<br><br># As the ml model better works on the numerical data we have to convert our categorical varibles into the numrical <br># we do this by using the lable encoder and one hot encoding <br>from sklearn.preprocessing import LabelEncoder<br>le = LabelEncoder()<br><br>input[&quot;compnay_n&quot;]= le.fit_transform(input[&quot;company&quot;])<br>input[&quot;job_n&quot;]= le.fit_transform(input[&quot;job&quot;])<br>input[&quot;degree_n&quot;]= le.fit_transform(input[&quot;degree&quot;])<br><br>## Dropping the categorical columns as we converted them into the numerical <br>input_n = input.drop([&#39;company&#39;,&#39;job&#39;,&#39;degree&#39;],axis=&#39;columns&#39;)<br><br># Importing and creating model using the decision tree classifier<br>from sklearn import tree<br>model = tree.DecisionTreeClassifier()<br><br>model.fit(input_n,target)<br># 0 if not else true<br>Output : array([0] , dtype = int64)<br><br># Get feature importances<br>importances =model.feature_importances_<br><br># Print feature importances<br>for i, importance in enumerate(importances):<br>    print(f&quot;Feature {i}: {importance:.4f}&quot;)<br><br>Feature 0: 0.4000<br>Feature 1: 0.3333<br>Feature 2: 0.2667</pre><p>from above values we can see which features the Decision Tree model considers most important for making splits and predictions.</p><p>Thank You !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41e8cdc7935f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multiclass Logistic Regression — Supervised Machine Learning Algorithm]]></title>
            <link>https://medium.com/@nikkbabargccp/multiclass-logistic-regression-supervised-machine-learning-algorithm-a53a7924b9d5?source=rss-8cc485f7e083------2</link>
            <guid isPermaLink="false">https://medium.com/p/a53a7924b9d5</guid>
            <category><![CDATA[regression]]></category>
            <category><![CDATA[regression-data-analysis]]></category>
            <category><![CDATA[multiclass-classification]]></category>
            <category><![CDATA[logistic-regression]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Nikhil Babar]]></dc:creator>
            <pubDate>Thu, 26 Sep 2024 12:59:28 GMT</pubDate>
            <atom:updated>2024-09-26T12:59:28.787Z</atom:updated>
            <content:encoded><![CDATA[<h3>Multiclass Logistic Regression — Supervised Machine Learning Algorithm</h3><p><strong>Multiclass Logistic Regression — Supervised Machine Learning Algorithm</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/0*RZDlbEe0AnPkQhhf.png" /></figure><p>As logistic regression mainly deals with Binary classification problem where the target variable has only two class but when we are dealing with more than one class we used <strong>Multiclass Logistic Regression.</strong></p><p>Example — predict this price is — high , medium or low .</p><p>Multiclass Logistic Regression, also known as <strong>SoftMax Regression . </strong>It<strong> </strong>is an advancement of the binary logistic regression to handle problems where the target variable or the outcome can have more than two classes.</p><p><strong>Difference : </strong>In binary logistic regression, the main aim is to predict one of two possible outcomes (0 or 1), while in multiclass logistic regression there are three or more possible outcomes.</p><p>There are mainly two strategies used in multiclass classification:</p><p><strong>1) One-vs-All (OvA) or One-vs-Rest (OvR):</strong></p><p>In this type we create a separate classifier for each class and each classifier predicts the given input belongs to this class or not .</p><p>For example , If we have to do cat , dog , and rabbit classification then we create a separate classifier for these 3 class where the 1st classifier is predict the given input belongs to cat class or not(cat or not cat ) like this for the we create classifier for dog and Rabbit .</p><p>When we classify the images all three classifier gives the score that how confident they are about there predictions and which ever class has highest prediction select as a result class</p><p><strong>2) One-vs-One (OvO):</strong></p><p>This involves creating a classifiers for every pair of class . If we have K classes then K(K-1)/2 pairs of classes are created for which we create a separate classifier .</p><p><strong>Example : </strong>Suppose we have 3 classes of fruits (Mango , Apple , Banana ) . We compare Mango vs Apple , Mango vs Banana , Apple vs Banana like this and create a classifier for this and check whether the input is belong to one of the two classes or not .</p><p>During prediction, a voting mechanism is used where each classifier votes for one of its two classes, and the class with the most votes is chosen as the final predicted class.</p><p><strong>Implementation using Scikit Learn :</strong></p><pre># Importing Libraries <br><br>import numpy as np<br><br>import pandas as pd<br><br>from sklearn.datasets import load_digits<br><br>from sklearn.model_selection import train_test_split<br><br>from sklearn.linear_model import LogisticRegression<br><br>from sklearn.metrics import accuracy_score, classification_report, confusion_matrix<br><br>import matplotlib.pyplot as plt<br><br>import seaborn as sns<br><br><br># Load the digits dataset digits = load_digits() X = digits.data y = digits.target # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)<br><br># Initialize the logistic regression model with the OvR strategy (default)<br>log_reg = LogisticRegression(multi_class=&#39;ovr&#39;, max_iter=200)<br><br># Fit the model to the training data<br>log_reg.fit(X_train, y_train)<br><br># Predict the class labels for the test set<br>y_pred = log_reg.predict(X_test)<br><br># Calculate accuracy<br>accuracy = accuracy_score(y_test, y_pred)<br>print(f&#39;Accuracy: {accuracy:.2f}&#39;)<br><br># Generate a classification report<br>print(&#39;Classification Report:&#39;)<br>print(classification_report(y_test, y_pred))<br><br># Generate a confusion matrix<br>conf_matrix = confusion_matrix(y_test, y_pred)<br>print(&#39;Confusion Matrix:&#39;)<br>print(conf_matrix)<br><br># Visualize the confusion matrix<br>plt.figure(figsize=(10,7))<br>sns.heatmap(conf_matrix, annot=True, fmt=&#39;d&#39;, cmap=&#39;Blues&#39;, xticklabels=digits.target_names, yticklabels=digits.target_names)<br>plt.xlabel(&#39;Predicted&#39;)<br>plt.ylabel(&#39;Actual&#39;)<br>plt.title(&#39;Confusion Matrix&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/0*W8kzGcge0b6sljqs" /><figcaption>Confusion Matrix</figcaption></figure><p>Thank You !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a53a7924b9d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Logistic Regression — Supervised Machine learning Algorithm]]></title>
            <link>https://medium.com/@nikkbabargccp/logistic-regression-supervised-machine-learning-algorithm-3b8715164b0a?source=rss-8cc485f7e083------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b8715164b0a</guid>
            <category><![CDATA[logistic-regression]]></category>
            <category><![CDATA[supervised-learning]]></category>
            <category><![CDATA[sigmoid-function]]></category>
            <category><![CDATA[binary-classification]]></category>
            <category><![CDATA[classification-algorithms]]></category>
            <dc:creator><![CDATA[Nikhil Babar]]></dc:creator>
            <pubDate>Thu, 26 Sep 2024 12:56:44 GMT</pubDate>
            <atom:updated>2024-09-26T12:56:44.723Z</atom:updated>
            <content:encoded><![CDATA[<h3>Logistic Regression — Supervised Machine learning Algorithm</h3><p><strong>Logistic Regression — Supervised Machine learning Algorithm</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/0*Xt_f8am1hdFhIYrh" /></figure><p>The Logistic Regression is a supervised machine learning algorithm used for classification tasks .</p><p>It also known as binary classifier . As it predicts the probability of input belong to the particular class .</p><p>Logistic regression is used instead of linear regression when the dependent variable or outcome variable is categorical, binary, or nominal in nature.</p><p>Here are some key reasons why logistic regression is preferred for such cases over linear regression:</p><p>If the problems are like about the categorical variable and binary outcomes such as yes/no then in that case we use the logistic regression</p><p>Example — the email is spam or not , customer will by this or not like this</p><p><strong>Sigmoid Function</strong>:</p><p>The sigmoid function, also known as the logistic function, is a mathematical function that maps any real-valued number</p><ul><li>Logistic regression uses the sigmoid function to map predicted values to probabilities.</li><li>The sigmoid function is defined as: σ(z)=11+e−z​</li><li>The output of the sigmoid function ranges from 0 to 1.</li><li>this function used the Euler number which is (2.71828)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/0*7nz40jgxDbpUr8g9" /></figure><p><strong>Decision Boundary</strong>:</p><ul><li>A decision boundary is set at a probability threshold (commonly 0.5) to classify instances into one of the two classes.</li></ul><p><strong>Cost Function</strong>:</p><ul><li>Logistic regression uses the log-loss (cross-entropy) as the cost function, which measures the performance of the classification model.</li></ul><p><strong>Implementation :</strong></p><pre>import numpy as np<br>import pandas as pd<br>from sklearn.datasets import load_iris<br>from sklearn.model_selection import train_test_split<br>from sklearn.linear_model import LogisticRegression<br>from sklearn.metrics import accuracy_score, classification_report, confusion_matrix<br>import matplotlib.pyplot as plt<br><br># Load the Iris dataset<br>iris = load_iris()<br>X = iris.data<br>y = iris.target<br><br># For binary classification, we&#39;ll choose one class  vs. the rest<br>y_binary = (y == 0).astype(int)<br><br># Split the data into training and testing sets<br>X_train, X_test, y_train, y_test = train_test_split(X, y_binary, test_size=0.2, random_state=0)<br><br># Initialize the logistic regression model<br>log_reg = LogisticRegression()<br><br># Fit the model to the training data<br>log_reg.fit(X_train, y_train)<br><br># Predict the probabilities for the test set<br>y_pred_prob = log_reg.predict_proba(X_test)[:, 1]<br><br># Predict the class labels for the test set<br>y_pred = log_reg.predict(X_test)<br><br># Calculate accuracy<br>accuracy = accuracy_score(y_test, y_pred)<br>print(f&#39;Accuracy: {accuracy:.2f}&#39;)<br><br># Generate a classification report<br>print(&#39;Classification Report:&#39;)<br>print(classification_report(y_test, y_pred))<br><br><br>Accuracy: 1.00<br>Classification Report:<br>              precision    recall  f1-score   support<br><br>           0       1.00      1.00      1.00        19<br>           1       1.00      1.00      1.00        11<br><br>    accuracy                           1.00        30<br>   macro avg       1.00      1.00      1.00        30<br>weighted avg       1.00      1.00      1.00        30</pre><p>Thank You !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b8715164b0a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>