{"id":9107,"date":"2024-02-05T11:39:55","date_gmt":"2024-02-05T19:39:55","guid":{"rendered":"https:\/\/live-cometml.pantheonsite.io\/?p=9107"},"modified":"2025-04-24T17:03:19","modified_gmt":"2025-04-24T17:03:19","slug":"room-occupancy-detection","status":"publish","type":"post","link":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection\/","title":{"rendered":"Room Occupancy Detection"},"content":{"rendered":"\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/0*f4InbS30yYQNzidw\" alt=\"a white empty room\"\/><\/figure>\n\n\n\n<p class=\"graf graf--p\">This project aims to predict whether a room is occupied based on the data collected from the sensors. The data set is collected from the UCI Machine Learning Repository. The data set contains 7 attributes: date, temperature, humidity, light, CO2, humidity ratio, and occupancy. The data set is divided into 2 data sets for training and testing. It provides experimental data for binary classification (room occupancy of an office room) from Temperature, Humidity, Light, and CO2. Ground-truth occupancy was obtained from time-stamped pictures that were taken every minute.<\/p>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Data Dictionary<\/h3>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*X1FX_zVEp-97_X6a-Tl-mg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 1: Data Dictionary of the&nbsp;dataset<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">Importing the required libraries<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n<span class=\"hljs-keyword\">import<\/span> pandas <span class=\"hljs-keyword\">as<\/span> pd\n<span class=\"hljs-keyword\">import<\/span> matplotlib.pyplot <span class=\"hljs-keyword\">as<\/span> plt\n<span class=\"hljs-keyword\">import<\/span> seaborn <span class=\"hljs-keyword\">as<\/span> sns<\/span><\/pre>\n\n\n\n<p class=\"graf graf--p\">Loading two datasets and combining them into one dataset<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#loading the datasets<\/span>\ndf1 = pd.read_csv(<span class=\"hljs-string\">'datatest.csv'<\/span>)\ndf2 = pd.read_csv(<span class=\"hljs-string\">'datatraining.csv'<\/span>)<\/span><\/pre>\n\n\n\n<p class=\"graf graf--p\">Combining the datasets<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\">df = pd.concat([df1,df2])\ndf.head()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*NypoTUY6rulBpwVsbprnDg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 2: Data&nbsp;frame<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Data Preprocessing<\/h3>\n\n\n\n<p class=\"graf graf--p\">Checking the number of rows and columns in the dataset<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#number of rows and columns<\/span>\ndf.shape<\/span><\/pre>\n\n\n\n<p class=\"graf graf--p\">Checking for null\/missing values<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\">df.isnull().<span class=\"hljs-built_in\">sum<\/span>()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*UNykBlYiTCm88KUL1HGIwQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 3: null value&nbsp;count<\/figcaption><\/figure>\n\n\n\n<p class=\"graf graf--p\">Checking for duplicate values<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\">df.duplicated().<span class=\"hljs-built_in\">sum<\/span>()\n=&gt; <span class=\"hljs-number\">27<\/span><\/span><\/pre>\n\n\n\n<p class=\"graf graf--p\">The dataset has 27 duplicate values. I will drop these duplicate values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#removing the duplicate values<\/span>\ndf.drop_duplicates(inplace=<span class=\"hljs-literal\">True<\/span>)<\/span><\/pre>\n\n\n\n<p class=\"graf graf--p\">Checking the data types of each column<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#checking data types<\/span>\ndf.dtypes<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*g90IvitksFkzju6_jDnfHA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 4: Dataset datatypes<\/figcaption><\/figure>\n\n\n\n<p class=\"graf graf--p\">Since the column date has an object datatype, I am converting it to the datetime datatype.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#converting the date and time to datetime format<\/span>\ndf[<span class=\"hljs-string\">'date'<\/span>] = pd.to_datetime(df[<span class=\"hljs-string\">'date'<\/span>])<\/span><\/pre>\n\n\n\n<p class=\"graf graf--p\">Descriptive Statistics<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#checking the descriptive statistics<\/span>\ndf.describe()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*_et-l2wu56io96KMTEsKxA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 5: Descriptive Statistics<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Exploratory Data&nbsp;Analysis<\/h3>\n\n\n\n<p class=\"graf graf--p\">In the exploratory data analysis, we will be looking at the distribution of the data, along with the time series of the data. We will also be looking at the correlation between the variables.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Visualizing the Temperature Fluctuations Over&nbsp;Time<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#lineplot for temperature changes for time<\/span>\nplt.figure(figsize=(<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-number\">10<\/span>))\nsns.lineplot(x=<span class=\"hljs-string\">'date'<\/span>,y=<span class=\"hljs-string\">'Temperature'<\/span>,data=df)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*qCSEa5n6p8YGeh5fy8zt-Q.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 6: Temperature vs. Time&nbsp;Graph<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">The spikes in the graph indicate that the room temperature increases suddenly, which might be due to the presence of people in the room. The room&#8217;s temperature may increase due to the heat emitted by the human body.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Visualizing the Humidity Fluctuations Over&nbsp;Time<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#lineplot for humidity changes for time<\/span>\nplt.figure(figsize=(<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-number\">10<\/span>))\nsns.lineplot(x=<span class=\"hljs-string\">'date'<\/span>,y=<span class=\"hljs-string\">'Humidity'<\/span>,data=df)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*Hp54nn3SFoYA9CUeuajdWw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 7: Humidity vs. Time&nbsp;Graph<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">The line graph between the 3rd and 6th of February is similar to the temperature graph, possibly due to the presence of people in the room. However, from the 7th of February onwards, there has been a significant rise in the humidity levels, which might be due to the cleaning of the room or a change in the weather conditions. Room cleaning, such as sweeping the floor, might be the reason for the sudden rise in the humidity levels. But it couldn&#8217;t explain the increase in the humidity levels near the 10th of February.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Visualizing the Light Fluctuations Over&nbsp;Time<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#lineplot for light changes for time<\/span>\nplt.figure(figsize=(<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-number\">10<\/span>))\nsns.lineplot(x=<span class=\"hljs-string\">'date'<\/span>,y=<span class=\"hljs-string\">'Light'<\/span>,data=df)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*tBLpQA9WtHQhtP_zPahQzg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 8: Light Intensity vs. Time&nbsp;Graph<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">Looking closely, we can see that the number of peaks in this graph and the temperature graph are the same. This indicates that the lights were turned on when a person was in the room. This is a good indicator of the occupancy of the room.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Visualizing the CO2 Fluctuations Over&nbsp;Time<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#lineplot for co2 changes for time<\/span>\nplt.figure(figsize=(<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-number\">10<\/span>))\nsns.lineplot(x=<span class=\"hljs-string\">'date'<\/span>,y=<span class=\"hljs-string\">'CO2'<\/span>,data=df)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*Un4z0Nel4k67xoO0KHA-eA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 9: CO2 Levels vs. Time&nbsp;graph<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">The CO2 graph also shows the spikes in the CO2 levels, which indicates the presence of a person in the room, assuming that there is no other source of CO2 in the room. In addition, the spikes correspond with the temperature graph and light graph. However, from the 7th to the 9th of February, the CO2 levels were minimal, indicating that the room was not occupied during that time. This observation contradicts the humidity graph and temperature graph.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Visualizing the Humidity Ratio Fluctuations Over&nbsp;Time<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#lineplot for humidity ratio changes for time<\/span>\nplt.figure(figsize=(<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-number\">10<\/span>))\nsns.lineplot(x=<span class=\"hljs-string\">'date'<\/span>,y=<span class=\"hljs-string\">'HumidityRatio'<\/span>,data=df)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*WlWOGYlsTcLqXyj_g1ekYA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 10: Humidity Ratio Vs Time&nbsp;graph<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">The humidity ratio graph is quite similar to the humidity graph. The spikes in the graph indicate the presence of people in the room. Moreover, the same assumption is made about the humidity ratio after the 9th of February.<\/p>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Correlation Between the Variables<\/h3>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Correlation Heatmap<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#correlation heatmap<\/span>\nplt.figure(figsize=(<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-number\">10<\/span>))\nsns.heatmap(df.corr(),annot=<span class=\"hljs-literal\">True<\/span>)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*j4HvY1cXdWAz2sSo1cEZYw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 11: Correlation Heatmap<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">There is a strong correlation between light and occupancy as well as between humidity and humidity ratio. The CO2 levels and temperature also show a strong correlation with the occupancy. However, the humidity and humidity ratio has little correlation with the occupancy.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Temperature and Occupancy<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#violinplot for temperature<\/span>\nsns.violinplot(y = df[<span class=\"hljs-string\">'Temperature'<\/span>],x = df[<span class=\"hljs-string\">'Occupancy'<\/span>])\nplt.xlabel(<span class=\"hljs-string\">'Occupancy'<\/span>)\nplt.ylabel(<span class=\"hljs-string\">'Temperature'<\/span>)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*B0T-9R4zm8PoAJjZS8pteQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 12: Temperature vs. Occupancy graph<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">The temperature and occupancy graph shows that the room temperature increases when a person is in the room because of the heat emitted by the human body. The room&#8217;s temperature decreases when there is no person in the room. This proves the hypothesis regarding the peaks in the temperature graph.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Light and Occupancy<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#boxplot for light<\/span>\nsns.boxplot(y = df[<span class=\"hljs-string\">'Light'<\/span>],x = df[<span class=\"hljs-string\">'Occupancy'<\/span>])\nplt.xlabel(<span class=\"hljs-string\">'Occupancy'<\/span>)\nplt.ylabel(<span class=\"hljs-string\">'Light'<\/span>)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*j27c41mqU4QWEm6m74p1Cg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 13: Light Intensity Vs Occupancy Graph<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">The light intensity of the room increases when there is a person in the room. This is because the lights are turned on when a person is in the room. The light intensity of the room decreases when there is no person in the room. This proves the hypothesis regarding the peaks in the light graph. The outliers in the boxplot and the curves in the light graph might be due to sunlight entering the room.<\/p>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">CO2 and Occupancy<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#violinlot for co2<\/span>\nsns.violinplot(y = df[<span class=\"hljs-string\">'CO2'<\/span>],x = df[<span class=\"hljs-string\">'Occupancy'<\/span>])\nplt.xlabel(<span class=\"hljs-string\">'Occupancy'<\/span>)\nplt.ylabel(<span class=\"hljs-string\">'CO2'<\/span>)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*jfdfOYIBDia8K3x2nMM6Tw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 14: CO2 Levels Vs Occupancy Graph<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">The CO2 levels of the room increase when there is a person in the room because of the CO2 emitted by the human body. The CO2 levels of the room decrease when there is no person in the room. This proves the hypothesis regarding the peaks in the CO2 graph.<\/p>\n\n\n\n<p class=\"graf graf--p\">From the above EDA, it is clear that the room&#8217;s temperature, light, and CO2 levels are good occupancy indicators. Therefore, we will be using these three variables for our classification model.<\/p>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Data Preprocessing 2<\/h3>\n\n\n\n<p class=\"graf graf--p\">Dropping the columns to reduce the dataset dimensionality<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#dropping columns humidity, date and humidity ratio<\/span>\ndf.drop([<span class=\"hljs-string\">'Humidity'<\/span>,<span class=\"hljs-string\">'date'<\/span>,<span class=\"hljs-string\">'HumidityRatio'<\/span>],axis=<span class=\"hljs-number\">1<\/span>,inplace=<span class=\"hljs-literal\">True<\/span>)<\/span><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\">df.head(<span class=\"hljs-number\">10<\/span>)<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*jCj2ebU_0aNZhWDZj9XGGA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 15: Final&nbsp;Dataset<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Train Test&nbsp;Split<\/h3>\n\n\n\n<p class=\"graf graf--p\">Splitting the dataset for training and testing the machine learning model<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-keyword\">from<\/span> sklearn.model_selection <span class=\"hljs-keyword\">import<\/span> train_test_split\nx_train,x_test,y_train,y_test = train_test_split(df.drop([<span class=\"hljs-string\">'Occupancy'<\/span>],axis=<span class=\"hljs-number\">1<\/span>),df[<span class=\"hljs-string\">'Occupancy'<\/span>],test_size=<span class=\"hljs-number\">0.2<\/span>,random_state=<span class=\"hljs-number\">42<\/span>)<\/span><\/pre>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Model Building<\/h3>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Random Tree Classifier<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-keyword\">from<\/span> sklearn.ensemble <span class=\"hljs-keyword\">import<\/span> RandomForestClassifier\nrfc = RandomForestClassifier()\nrfc<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*wpWA8WM2eASzVq3hrl0mlQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 16: Random Forest Classifier<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">Training the model<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#training the model<\/span>\nrfc.fit(x_train,y_train)\n<span class=\"hljs-comment\">#training accuracy<\/span>\nrfc.score(x_train,y_train)\n=&gt; <span class=\"hljs-number\">0.96<\/span><\/span><\/pre>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Model Evaluation<\/h3>\n\n\n\n<p class=\"graf graf--p\">Predicting values from the model<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\">rfc_pred = rfc.predict(x_test)<\/span><\/pre>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Confusion Matrix&nbsp;Heatmap<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#confusion matrix heatmap<\/span>\n<span class=\"hljs-keyword\">from<\/span> sklearn.metrics <span class=\"hljs-keyword\">import<\/span> confusion_matrix\nsns.heatmap(confusion_matrix(y_test,rfc_pred),annot=<span class=\"hljs-literal\">True<\/span>)\nplt.ylabel(<span class=\"hljs-string\">'Predicted'<\/span>)\nplt.xlabel(<span class=\"hljs-string\">'Actual'<\/span>)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*cRnvhOsERN5nmfAnWduQHw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 17: Confusion Matrix&nbsp;Heatmap<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Distribution Plot<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#distribution plot for the predicted and actual values<\/span>\nax = sns.distplot(y_test,hist=<span class=\"hljs-literal\">False<\/span>,label=<span class=\"hljs-string\">'Actual'<\/span>, color=<span class=\"hljs-string\">'r'<\/span>)\nsns.distplot(rfc_pred,hist=<span class=\"hljs-literal\">False<\/span>,label=<span class=\"hljs-string\">'Predicted'<\/span>,color=<span class=\"hljs-string\">'b'<\/span>,ax=ax)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*cMrqZKIhBLOzQSna7iK7Pw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 18: Distribution Plot<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Classification Report<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-keyword\">from<\/span> sklearn.metrics <span class=\"hljs-keyword\">import<\/span> classification_report\n<span class=\"hljs-built_in\">print<\/span>(classification_report(y_test,rfc_pred))<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*sbmLs0e041FuuBi7SAu8Iw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 19: Classification Report<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Model Metrics Evaluation<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-keyword\">from<\/span> sklearn.metrics <span class=\"hljs-keyword\">import<\/span> accuracy_score\n<span class=\"hljs-keyword\">from<\/span> sklearn.metrics <span class=\"hljs-keyword\">import<\/span> precision_score\n<span class=\"hljs-keyword\">from<\/span> sklearn.metrics <span class=\"hljs-keyword\">import<\/span> recall_score\n<span class=\"hljs-keyword\">from<\/span> sklearn.metrics <span class=\"hljs-keyword\">import<\/span> f1_score\n\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'Accuracy Score : '<\/span> + <span class=\"hljs-built_in\">str<\/span>(accuracy_score(y_test,rfc_pred)))\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'Precision Score : '<\/span> + <span class=\"hljs-built_in\">str<\/span>(precision_score(y_test,rfc_pred)))\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'Recall Score : '<\/span> + <span class=\"hljs-built_in\">str<\/span>(recall_score(y_test,rfc_pred)))\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'F1 Score : '<\/span> + <span class=\"hljs-built_in\">str<\/span>(f1_score(y_test,rfc_pred)))<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*S2CtzZt8ee95gw4dWL6ZIA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 20: Model&nbsp;Metrics<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Testing the Model on the New&nbsp;Dataset<\/h3>\n\n\n\n<p class=\"graf graf--p\">Loading the dataset<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\">df_new = pd.read_csv(<span class=\"hljs-string\">'datatest2.csv'<\/span>)\ndf_new.head()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*hbihIYy5fp8yfsvwV6j56A.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 21: Testing&nbsp;Dataset<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">Making similar changes to the testing dataset<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#dropping columns humidity, date and humidity ratio<\/span>\ndf_new.drop([<span class=\"hljs-string\">'Humidity'<\/span>,<span class=\"hljs-string\">'date'<\/span>,<span class=\"hljs-string\">'HumidityRatio'<\/span>],axis=<span class=\"hljs-number\">1<\/span>,inplace=<span class=\"hljs-literal\">True<\/span>)\n\n<span class=\"hljs-comment\">#splitting the target variable<\/span>\nx = df_new.drop([<span class=\"hljs-string\">'Occupancy'<\/span>],axis=<span class=\"hljs-number\">1<\/span>)\ny = df_new[<span class=\"hljs-string\">'Occupancy'<\/span>]<\/span><\/pre>\n\n\n\n<p class=\"graf graf--p\">Predicting the values from the test dataset<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#predicting the values<\/span>\npred = rfc.predict(x)<\/span><\/pre>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Confusion Matrix&nbsp;Heatmap<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#confusion matrix heatmap<\/span>\nsns.heatmap(confusion_matrix(y,pred),annot=<span class=\"hljs-literal\">True<\/span>)\nplt.ylabel(<span class=\"hljs-string\">'Predicted'<\/span>)\nplt.xlabel(<span class=\"hljs-string\">'Actual'<\/span>)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*9NcEq-SmD-gEBxBN098kwQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 22: Confusion Matrix&nbsp;Heatmap<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Distribution Plot<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-comment\">#distribution plot for the predicted and actual values<\/span>\nax = sns.distplot(y,hist=<span class=\"hljs-literal\">False<\/span>,label=<span class=\"hljs-string\">'Actual'<\/span>, color=<span class=\"hljs-string\">'r'<\/span>)\nsns.distplot(pred,hist=<span class=\"hljs-literal\">False<\/span>,label=<span class=\"hljs-string\">'Predicted'<\/span>,color=<span class=\"hljs-string\">'b'<\/span>,ax=ax)\nplt.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*kH83pSslDS1g-ZEWd2t4RA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 23: Distribution Plot<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Classification Report<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-built_in\">print<\/span>(classification_report(y,pred))<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*IDXgwChGGBVk01O2g2_PLw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 24: Classification Report<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading graf graf--h4\">Model Metrics<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"pre--content\"><span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'Accuracy Score : '<\/span> + <span class=\"hljs-built_in\">str<\/span>(accuracy_score(y,pred)))\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'Precision Score : '<\/span> + <span class=\"hljs-built_in\">str<\/span>(precision_score(y,pred)))\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'Recall Score : '<\/span> + <span class=\"hljs-built_in\">str<\/span>(recall_score(y,pred)))\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'F1 Score : '<\/span> + <span class=\"hljs-built_in\">str<\/span>(f1_score(y,pred)))<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*Kso-IGkq4sxBIIo8uXPFyg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Figure 25: Model&nbsp;Metrics<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Conclusion<\/h3>\n\n\n\n<p class=\"graf graf--p\">The above models show that the Random Forest Classifier has the highest accuracy score of 98%. Therefore, we will use the Random Forest Classifier for our final model. The exploratory data analysis found that the change in room temperature, CO levels, and light intensity can be used to predict the room occupancy in place of humidity and humidity ratio.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This project aims to predict whether a room is occupied based on the data collected from the sensors. The data set is collected from the UCI Machine Learning Repository. The data set contains 7 attributes: date, temperature, humidity, light, CO2, humidity ratio, and occupancy. The data set is divided into 2 data sets for training [&hellip;]<\/p>\n","protected":false},"author":120,"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":[7],"tags":[],"coauthors":[217],"class_list":["post-9107","post","type-post","status-publish","format-standard","hentry","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>Room Occupancy Detection - Comet<\/title>\n<meta name=\"description\" content=\"In this project, walk through a tutorial to detect room occupancy using machine learning. Learn more today.\" \/>\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\/room-occupancy-detection\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Room Occupancy Detection\" \/>\n<meta property=\"og:description\" content=\"In this project, walk through a tutorial to detect room occupancy using machine learning. Learn more today.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection\" \/>\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=\"2024-02-05T19:39:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T17:03:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn-images-1.medium.com\/max\/800\/0*f4InbS30yYQNzidw\" \/>\n<meta name=\"author\" content=\"Sukhman Singh\" \/>\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=\"Sukhman Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Room Occupancy Detection - Comet","description":"In this project, walk through a tutorial to detect room occupancy using machine learning. Learn more today.","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\/room-occupancy-detection","og_locale":"en_US","og_type":"article","og_title":"Room Occupancy Detection","og_description":"In this project, walk through a tutorial to detect room occupancy using machine learning. Learn more today.","og_url":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection","og_site_name":"Comet","article_publisher":"https:\/\/www.facebook.com\/cometdotml","article_published_time":"2024-02-05T19:39:55+00:00","article_modified_time":"2025-04-24T17:03:19+00:00","og_image":[{"url":"https:\/\/cdn-images-1.medium.com\/max\/800\/0*f4InbS30yYQNzidw","type":"","width":"","height":""}],"author":"Sukhman Singh","twitter_card":"summary_large_image","twitter_creator":"@Cometml","twitter_site":"@Cometml","twitter_misc":{"Written by":"Sukhman Singh","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection#article","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection\/"},"author":{"name":"Sukhman Singh","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/03fee0ceb719563b09dcf21796f85455"},"headline":"Room Occupancy Detection","datePublished":"2024-02-05T19:39:55+00:00","dateModified":"2025-04-24T17:03:19+00:00","mainEntityOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection\/"},"wordCount":1075,"publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection#primaryimage"},"thumbnailUrl":"https:\/\/cdn-images-1.medium.com\/max\/800\/0*f4InbS30yYQNzidw","articleSection":["Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection\/","url":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection","name":"Room Occupancy Detection - Comet","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection#primaryimage"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection#primaryimage"},"thumbnailUrl":"https:\/\/cdn-images-1.medium.com\/max\/800\/0*f4InbS30yYQNzidw","datePublished":"2024-02-05T19:39:55+00:00","dateModified":"2025-04-24T17:03:19+00:00","description":"In this project, walk through a tutorial to detect room occupancy using machine learning. Learn more today.","breadcrumb":{"@id":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection#primaryimage","url":"https:\/\/cdn-images-1.medium.com\/max\/800\/0*f4InbS30yYQNzidw","contentUrl":"https:\/\/cdn-images-1.medium.com\/max\/800\/0*f4InbS30yYQNzidw"},{"@type":"BreadcrumbList","@id":"https:\/\/www.comet.com\/site\/blog\/room-occupancy-detection#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.comet.com\/site\/"},{"@type":"ListItem","position":2,"name":"Room Occupancy Detection"}]},{"@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\/03fee0ceb719563b09dcf21796f85455","name":"Sukhman Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/image\/cbb8e2cab1e32e43711b4e44c7711e76","url":"https:\/\/secure.gravatar.com\/avatar\/2e74502dcaa20f8fe126f4e1eef424b9227d93803c9cc857de010fbca0b41265?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2e74502dcaa20f8fe126f4e1eef424b9227d93803c9cc857de010fbca0b41265?s=96&d=mm&r=g","caption":"Sukhman Singh"},"url":"https:\/\/www.comet.com\/site\/blog\/author\/sukhmansinghbhogalgmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/9107","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\/120"}],"replies":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/comments?post=9107"}],"version-history":[{"count":1,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/9107\/revisions"}],"predecessor-version":[{"id":15392,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/9107\/revisions\/15392"}],"wp:attachment":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/media?parent=9107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/categories?post=9107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/tags?post=9107"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/coauthors?post=9107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}