{"id":8248,"date":"2023-11-29T09:29:16","date_gmt":"2023-11-29T17:29:16","guid":{"rendered":"https:\/\/live-cometml.pantheonsite.io\/?p=8248"},"modified":"2025-04-24T17:04:17","modified_gmt":"2025-04-24T17:04:17","slug":"tracking-your-naive-bayes-model-using-comet","status":"publish","type":"post","link":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet\/","title":{"rendered":"Tracking Your Naive Bayes Model Using Comet"},"content":{"rendered":"\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet\">\n\n\n\n<h1 class=\"wp-block-heading mc md fr be me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt mu mv mw mx my mz bj\" id=\"01ad\"><strong class=\"al\">Introduction<\/strong><\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"283f\">The Naive Bayes model is a popular machine learning algorithm for classification tasks. It uses probability concepts to make predictions based on feature independence.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"1ce0\">You can gain valuable insights into your model\u2019s behavior, compare different iterations, and make intelligent choices about its performance by incorporating Comet into your Naive Bayes workflow. Tracking your Naive Bayes model using Comet can significantly improve your ability to analyze and optimize its performance, whether working on a personal project or collaborating with a team.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"b148\">In this article, we will look at the principles of a Naive Bayes Model and the concept of tracking Naive Bayes models with Comet, a powerful tool for experiment management and monitoring. Comet is a comprehensive platform for visualizing key metrics, hyperparameters, and other relevant information throughout the model development lifecycle.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"7ded\">What we\u2019ll cover:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What is a Naive Bayes Model<\/li>\n\n\n\n<li>Overview of Comet<\/li>\n\n\n\n<li>Importance Of Tracking a Naive Bayes Model<\/li>\n\n\n\n<li>Performing a Naive Bayes Project<\/li>\n<\/ul>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"8fb2\">Let\u2019s get started.<\/p>\n\n\n\n<h1 class=\"wp-block-heading mc md fr be me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt mu mv mw mx my mz bj\" id=\"346a\">What Is a Naive Bayes Model?<\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"7e80\">A Naive Bayes model is a machine learning algorithm and a probabilistic machine learning model used for classification processes. It\u2019s called \u201cnaive\u201d because it assumes that a data point\u2019s features (or characteristics) are independent. In other words, it presumes that one attribute\u2019s presence or absence does not affect another\u2019s presence or absence.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"7a5b\">For example, assume we want to create a model that can predict whether or not an email is spam based on the words it contains. To predict spam in new, unseen emails, the model must learn from labeled emails (some are marked as spam and others as non-spam).<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"8239\">The Naive Bayes model determines whether an email is spam based on the frequency of words in each category. It examines how frequently certain words appear in spam emails versus how often they appear in non-spam emails. The model then makes predictions based on these probabilities.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"5de9\">When a new email arrives, the Naive Bayes model uses the words in the message to determine whether it is spam. It counts the chances of each word appearing in spam or non-spam emails while accounting for the independence assumption. Finally, it compares the probabilities and classifies the email as spam or non-spam based on the higher probability.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"d3d8\">Naive Bayes is based on Bayes\u2019 theorem, which states that the probability of an event A occurring, given that event B has already occurred, is equal to the likelihood of event A occurring times the probability of event B occurring given that event A has already happened, divided by the possibility of event B occurring. It is expressed as:<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"8815\">P(A|B) = (P(B|A) * P(A)) \/ P(B)<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"f2fb\">The Naive Bayes model can effectively classify or predict the class labels of new data instances by applying this principle. It computes the probabilities of various classes based on observed features and updates them using Bayes\u2019 theorem as new evidence is introduced. As a prediction, the model chooses the class with the highest probability. Despite its simplicity and assumptions, Naive Bayes performs well in practice, particularly in text classification and other similar tasks.<\/p>\n\n\n\n<h1 class=\"wp-block-heading mc md fr be me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt mu mv mw mx my mz bj\" id=\"87d1\">Overview of Comet<\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"adf3\">Comet is a comprehensive machine learning platform that offers many features and capabilities to assist data scientists and ML practitioners in managing and tracking their experiments effectively. It is a repository for all your experiment data, including code, metrics, parameters, and artifacts. This makes replicating experiments, debugging models, and comparing various approaches simple.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"c30c\">Here are some of the features and capabilities of Comet. ML<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong class=\"be ok\">Experiment Tracking: <\/strong>Comet keeps track of all the critical details of your experiments, such as the code you used, the parameters you set, the metrics you collected, and the artifacts you generated. This data is kept in a centralized location, making it simple to replicate experiments and debug models.<\/li>\n\n\n\n<li><strong class=\"be ok\">Model Production Monitoring: <\/strong>Comet can be used to track the performance of your models in the field. Metrics such as accuracy, latency, and throughput can be tracked, which can assist you in recognizing and fixing any issues with your models.<\/li>\n\n\n\n<li><strong class=\"be ok\">Visualization:<\/strong> The platform provides interactive visualizations that allow you to explore and analyze the results of your experiments. Charts, graphs, and histograms can be used to gain insights into model performance, metrics, and trends. Visualizations assist you in making data-driven decisions and comprehending the behavior of your models.<\/li>\n\n\n\n<li><strong class=\"be ok\">Comparison:<\/strong> Model comparison is made more accessible by the platform\u2019s ability to compare different iterations or versions of your models. You can compare and contrast their performance, allowing you to identify improvements or regressions. Comparisons assist you in making educated decisions about model selection and improvement strategies.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading mc md fr be me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt mu mv mw mx my mz bj\" id=\"cee9\">Importance Of Tracking A Naive Bayes Model<\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"ec24\">Tracking a Naive Bayes model is integral to verifying that the model is performing optimally and making reliable predictions.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"583b\">These are some of the benefits of tracking a Naive Bayes Model:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can track the performance of a Naive Bayes model over time by tracking metrics such as accuracy, precision, recall, and F1 score. Monitoring performance allows you to detect any degradation or improvement in model performance and make the necessary modifications or improvements.<\/li>\n\n\n\n<li>Tracking a Naive Bayes model can also assist in detecting changes in the data used to train the model. This is significant because changes in the data can affect the model\u2019s performance. For instance, if a new feature is added to the data, the model may need to be retrained to accommodate the new feature.<\/li>\n\n\n\n<li>Tracking allows you to record all the information required to replicate an experiment. This includes documenting the versions of libraries, datasets, and code used in model training. Reproducibility is critical for maintaining transparency in your work and facilitating cooperation with other researchers or team members.<\/li>\n\n\n\n<li>Tracking can assist you in determining the root cause of problems or unexpected actions with your Naive Bayes model. You can investigate and debug potential problems by examining the recorded metrics, hyperparameters, and other data. Tracking provides valuable insights into the model\u2019s behavior, allowing you to diagnose and resolve issues more effectively.<\/li>\n<\/ul>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"2f4a\">Now that we\u2019ve learned everything there is to know about Naive Bayes models let\u2019s get creative and build one to track using Comet.<\/p>\n\n\n\n<h1 class=\"wp-block-heading mc md fr be me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt mu mv mw mx my mz bj\" id=\"e42d\">Overview of the Project<\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"5e7d\">We will be dealing with weather prediction for this project. We will use the Naive Bayes Model to create a weather prediction program. The \u201c<a class=\"af ol\" href=\"https:\/\/www.kaggle.com\/datasets\/petalme\/seattle-weather-prediction-dataset\/data\" target=\"_blank\" rel=\"noopener ugc nofollow\">Seattle-weather<\/a>\u201d dataset was used in this project and can be obtained from Kaggle.<\/p>\n\n\n\n<h1 class=\"wp-block-heading mc md fr be me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt mu mv mw mx my mz bj\" id=\"73f0\">Installation and Importation of Libraries<\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"e62c\">The first step is to <a class=\"af ol\" href=\"https:\/\/www.comet.com\/site\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">create an account on Comet<\/a> and install it by running the command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"c085\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\"><span class=\"hljs-addition\">!pip install comet_ml<\/span><\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"50a1\">Next, we will import all the libraries needed for this project:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"88d3\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> pandas <span class=\"hljs-keyword\">as<\/span> pd\n<span class=\"hljs-keyword\">from<\/span> sklearn.<span class=\"hljs-property\">preprocessing<\/span> <span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-title.class\">LabelEncoder<\/span>\n<span class=\"hljs-keyword\">from<\/span> sklearn.<span class=\"hljs-property\">naive_bayes<\/span> <span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-title.class\">GaussianNB<\/span>\n<span class=\"hljs-keyword\">from<\/span> sklearn.<span class=\"hljs-property\">metrics<\/span> <span class=\"hljs-keyword\">import<\/span> classification_report, confusion_matrix\n<span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n<span class=\"hljs-keyword\">import<\/span> seaborn <span class=\"hljs-keyword\">as<\/span> sns\n<span class=\"hljs-keyword\">import<\/span> matplotlib.<span class=\"hljs-property\">pyplot<\/span> <span class=\"hljs-keyword\">as<\/span> plt\n<span class=\"hljs-keyword\">from<\/span> sklearn.<span class=\"hljs-property\">model_selection<\/span> <span class=\"hljs-keyword\">import<\/span> train_test_split<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"0f8d\">We then proceed to set up the Comet experiment:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"282f\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\">from comet_ml import Experiment\nexperiment = Experiment(\napi_key=<span class=\"hljs-string\">\"insert your API key here\"<\/span>,\nproject_name=<span class=\"hljs-string\">\"your project name\"<\/span>,\nworkspace=<span class=\"hljs-string\">\"koded-ii\"<\/span>,\n)<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"ed00\">You are to insert your API key and the name you would like to give your project in the fields labeled \u201cyour API key\u201d and \u201cyour project name,\u201d respectively.<\/p>\n\n\n\n<h1 class=\"wp-block-heading mc md fr be me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt mu mv mw mx my mz bj\" id=\"8dd5\">Load the Data<\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"d9ba\">The next step involves loading our dataset. We can type the following program to load our data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"fded\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">data<\/span>=pd.read_csv(\u2018seattle-weather.csv\u2019)\n<span class=\"hljs-keyword\">data<\/span><\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"4438\">This command will reveal the contents of your datasets (in rows and columns).<\/p>\n\n\n\n<figure class=\"wp-block-image om on oo op oq pd pa pb paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:411\/1*SWOrPMpw-o82B8bsi1VqAg.jpeg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"4ef0\">Before we proceed further, let\u2019s do something interesting. Let us log and visualize our dataset using the Comet data panel. To learn more about this, click on <a class=\"af ol\" href=\"https:\/\/heartbeat.comet.ml\/log-and-visualize-tabular-data-using-comet-data-panel-498598d802a5\" target=\"_blank\" rel=\"noopener ugc nofollow\">Visualizing Data Using the Comet Data Panel<\/a>.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"3f18\">We can go ahead and type out the code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"da70\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\">experiment<span class=\"hljs-selector-class\">.log_table<\/span>(\u2018seattle-weather.csv\u2019)<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"e702\">Then, we make an instance of the sklearn.preprocessing library\u2019s LabelEncoder() class. We\u2019re attempting label encoding, a technique for converting categorical variables into numerical form. As a result, we\u2019re instantiating the LabelEncoder class and assigning it to the variable \u201cdigits.\u201d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"d851\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\"><span class=\"hljs-attr\">Digits<\/span>=LabelEncoder()<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"354e\">The fit() method of the LabelEncoder() class takes a list of categorical values as input and assigns a unique numerical label to each distinct category. The transform() method then takes a list of categorical values as input and returns an array of numerical labels. We\u2019ll use both methods to convert the inputs in the weather column to numerical values while also changing the column\u2019s name.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"6554\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">data<\/span>[\u2018weather_value\u2019]=Digits.fit_transform(<span class=\"hljs-keyword\">data<\/span>[\u2018weather\u2019])\n<span class=\"hljs-keyword\">data<\/span><\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image om on oo op oq pd pa pb paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:490\/1*Xi6OfjUoZ3EyOekM4ltehA.jpeg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"a9f4\">When this code runs, we\u2019ll see the changes that fit_transform() performs on the data.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"93aa\">Next, we want to drop the \u201cweather\u201d column since we\u2019ve replaced it with a column containing its numerical values. We will also remove the \u201cdate\u201d column for the reasons below.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"71ee\">The Naive Bayes model is based on the premise that the features are independent. This means that the value of one feature has no bearing on the value of another. The date in a weather prediction dataset is not independent of the other features. For example, the weather on a Thursday is more likely to be similar to the weather on other Thursdays than on a Saturday.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"835e\">If the date column is not removed, the Naive Bayes model will discover that it is an important feature. Overfitting will happen, which means the model will perform well on the training data but will not generalize well to new data.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"f200\">With this explanation, we can go ahead and carry out the removal.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"4d4c\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">data<\/span>.drop(\u2018date\u2019,axis=<span class=\"hljs-number\">1<\/span>,inplace=True)\n<span class=\"hljs-keyword\">data<\/span>.drop(<span class=\"hljs-string\">'weather'<\/span>,axis=<span class=\"hljs-number\">1<\/span>, inplace =True)\n<span class=\"hljs-keyword\">data<\/span><\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"45e7\">Running this code will remove the columns as planned.<\/p>\n\n\n\n<figure class=\"wp-block-image om on oo op oq pd pa pb paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:377\/1*POf0t3sspUIRLhoUgSeRlA.jpeg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"c1b2\">The values from the \u2018weather_value\u2019 column of the \u2018data\u2019 DataFrame are now assigned to the \u2018target\u2019 variable. We\u2019re doing this because, as the name implies, the \u201cweather_value column\u201d will be our output variable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"6c10\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\">target=<span class=\"hljs-keyword\">data<\/span>[\u2018weather_value\u2019]\ntarget<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"835a\">After that, we store our data and target as independent and dependent variables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"71fd\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\"><span class=\"hljs-attr\">X<\/span>= data\n<span class=\"hljs-attr\">y<\/span>=target<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"4c2b\">The train_test_split function from the scikit-learn library is then used to separate the dataset into training and testing subsets.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"7a50\">The Gaussian Naive Bayes algorithm is then initialized.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"377f\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\">X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\nclassifier=GaussianNB()<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"ecb0\">Now, we want to log the parameters of our project. This means we\u2019re keeping track of the hyperparameters and other settings used to train the model. This information can help you understand how the model works and troubleshoot issues. We can accomplish this by running this code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"f126\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\">experiment<span class=\"hljs-selector-class\">.log_parameters<\/span>({\u201cModel\u201d: \u201cNaive Bayes\u201d})<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"d6f6\">We are now to train our model with our training data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"3a01\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\">classifier<span class=\"hljs-selector-class\">.fit<\/span>(X_train,y_train)<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"54f2\">We provide the training data to the classifier and start the training process by calling the classifier.fit(X_train, y_train). The classifier will learn the data\u2019s underlying patterns and relationships using input features (X_train) and the corresponding target values (y_train).<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"6f8e\">Next, we create a variable y_pred, which will be used to make predictions using the trained classifier.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"a75e\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\"><span class=\"hljs-attr\">y_pred<\/span> = classifier.predict(X_test)<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"e21f\">After running this code, the variable y_pred will contain the predicted target values for the X_test inputs. These predictions can then be used for various purposes, including assessing the model\u2019s performance and making decisions based on them.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"0b6e\">We can calculate the accuracy of our classifier and log it to our experiment on Comet. We type the code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"5f3e\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\">accuracy = classifier.score(X_test, y_test)\nexperiment.log_metric(<span class=\"hljs-string\">\"Accuracy\"<\/span>, accuracy)<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"0716\">Once this is done, the accuracy of our model gets logged to our experiment on Comet, and we can check it by clicking on the URL provided when we started the project.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"ebc0\">We can finally end our experiment with this code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"63c4\" class=\"ov md fr os b bf ow ox l oy oz\" data-selectable-paragraph=\"\">experiment.<span class=\"hljs-keyword\">end<\/span>()<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"5005\">Once we\u2019ve written this code, a summary of the experiment comes up.<\/p>\n\n\n\n<figure class=\"wp-block-image om on oo op oq pd pa pb paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*Qt_nqRnjudJ0HRC6hzYDbg.jpeg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"5e4a\">You can click the URL link to check out your experiment in Comet.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"ef5f\">If you want to view the dataset you logged to Comet, click the link to see the dashboard.<\/p>\n\n\n\n<figure class=\"wp-block-image om on oo op oq pd pa pb paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*CZM-1QDAiXFfkitLkKR7KQ.jpeg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"274a\">Then, before the experiment icon, click the \u201cPanel\u201d icon to find your dataset.<\/p>\n\n\n\n<figure class=\"wp-block-image om on oo op oq pd pa pb paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*gzOLK9DyM2jRZ627mXBYCQ.jpeg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"471f\">Return to the experiment icon to view the other features you\u2019ve logged to Comet. You can experiment with logging more features, as additional features can be logged to Comet.<\/p>\n\n\n\n<h1 class=\"wp-block-heading mc md fr be me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt mu mv mw mx my mz bj\" id=\"6fa1\">Conclusion<\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"95f4\">We learned the basics of a Naive Bayes model, analyzed what Comet is all about, and worked on a project that helps integrate our model with Comet. You can explore the Comet platform, find tutorials, and even practice more. You can get familiar with the functions and applications of the Naive Bayes Model and Comet as a machine learning platform.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph na nb fr be b nc nx ne nf ng ny ni nj nk nz nm nn no oa nq nr ns ob nu nv nw fk bj\" id=\"c77e\">I hope this article was helpful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The Naive Bayes model is a popular machine learning algorithm for classification tasks. It uses probability concepts to make predictions based on feature independence. You can gain valuable insights into your model\u2019s behavior, compare different iterations, and make intelligent choices about its performance by incorporating Comet into your Naive Bayes workflow. Tracking your Naive [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"customer_name":"","customer_description":"","customer_industry":"","customer_technologies":"","customer_logo":"","footnotes":""},"categories":[9,7],"tags":[],"coauthors":[209],"class_list":["post-8248","post","type-post","status-publish","format-standard","hentry","category-product","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Tracking Your Naive Bayes Model Using Comet - Comet<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tracking Your Naive Bayes Model Using Comet\" \/>\n<meta property=\"og:description\" content=\"Introduction The Naive Bayes model is a popular machine learning algorithm for classification tasks. It uses probability concepts to make predictions based on feature independence. You can gain valuable insights into your model\u2019s behavior, compare different iterations, and make intelligent choices about its performance by incorporating Comet into your Naive Bayes workflow. Tracking your Naive [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet\" \/>\n<meta property=\"og:site_name\" content=\"Comet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cometdotml\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-29T17:29:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T17:04:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/miro.medium.com\/v2\/resize:fit:411\/1*SWOrPMpw-o82B8bsi1VqAg.jpeg\" \/>\n<meta name=\"author\" content=\"Tioluwani Oyedele\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Cometml\" \/>\n<meta name=\"twitter:site\" content=\"@Cometml\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tioluwani Oyedele\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Tracking Your Naive Bayes Model Using Comet - Comet","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet","og_locale":"en_US","og_type":"article","og_title":"Tracking Your Naive Bayes Model Using Comet","og_description":"Introduction The Naive Bayes model is a popular machine learning algorithm for classification tasks. It uses probability concepts to make predictions based on feature independence. You can gain valuable insights into your model\u2019s behavior, compare different iterations, and make intelligent choices about its performance by incorporating Comet into your Naive Bayes workflow. Tracking your Naive [&hellip;]","og_url":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet","og_site_name":"Comet","article_publisher":"https:\/\/www.facebook.com\/cometdotml","article_published_time":"2023-11-29T17:29:16+00:00","article_modified_time":"2025-04-24T17:04:17+00:00","og_image":[{"url":"https:\/\/miro.medium.com\/v2\/resize:fit:411\/1*SWOrPMpw-o82B8bsi1VqAg.jpeg","type":"","width":"","height":""}],"author":"Tioluwani Oyedele","twitter_card":"summary_large_image","twitter_creator":"@Cometml","twitter_site":"@Cometml","twitter_misc":{"Written by":"Tioluwani Oyedele","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet#article","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet\/"},"author":{"name":"Tioluwani Oyedele","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/796adc609509246fa42a5b22de92b414"},"headline":"Tracking Your Naive Bayes Model Using Comet","datePublished":"2023-11-29T17:29:16+00:00","dateModified":"2025-04-24T17:04:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet\/"},"wordCount":1967,"publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:411\/1*SWOrPMpw-o82B8bsi1VqAg.jpeg","articleSection":["Product","Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet\/","url":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet","name":"Tracking Your Naive Bayes Model Using Comet - Comet","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet#primaryimage"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:411\/1*SWOrPMpw-o82B8bsi1VqAg.jpeg","datePublished":"2023-11-29T17:29:16+00:00","dateModified":"2025-04-24T17:04:17+00:00","breadcrumb":{"@id":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet#primaryimage","url":"https:\/\/miro.medium.com\/v2\/resize:fit:411\/1*SWOrPMpw-o82B8bsi1VqAg.jpeg","contentUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:411\/1*SWOrPMpw-o82B8bsi1VqAg.jpeg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.comet.com\/site\/blog\/tracking-your-naive-bayes-model-using-comet#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.comet.com\/site\/"},{"@type":"ListItem","position":2,"name":"Tracking Your Naive Bayes Model Using Comet"}]},{"@type":"WebSite","@id":"https:\/\/www.comet.com\/site\/#website","url":"https:\/\/www.comet.com\/site\/","name":"Comet","description":"Build Better Models Faster","publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.comet.com\/site\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.comet.com\/site\/#organization","name":"Comet ML, Inc.","alternateName":"Comet","url":"https:\/\/www.comet.com\/site\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","width":310,"height":310,"caption":"Comet ML, Inc."},"image":{"@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cometdotml","https:\/\/x.com\/Cometml","https:\/\/www.youtube.com\/channel\/UCmN63HKvfXSCS-UwVwmK8Hw"]},{"@type":"Person","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/796adc609509246fa42a5b22de92b414","name":"Tioluwani Oyedele","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/image\/02158b7c3ec74d03855139e9736c6e94","url":"https:\/\/secure.gravatar.com\/avatar\/79bb464826d380f2cf7bb2197c43114d6b1360a47a95c48a8ea47c815dbc1406?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/79bb464826d380f2cf7bb2197c43114d6b1360a47a95c48a8ea47c815dbc1406?s=96&d=mm&r=g","caption":"Tioluwani Oyedele"},"url":"https:\/\/www.comet.com\/site\/blog\/author\/tioluwanioyedele542gmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/comments?post=8248"}],"version-history":[{"count":1,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8248\/revisions"}],"predecessor-version":[{"id":15442,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8248\/revisions\/15442"}],"wp:attachment":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/media?parent=8248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/categories?post=8248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/tags?post=8248"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/coauthors?post=8248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}