<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="https://acb-prgm.github.io/ACB-Blog/feed.xml" rel="self" type="application/atom+xml" /><link href="https://acb-prgm.github.io/ACB-Blog/" rel="alternate" type="text/html" /><updated>2023-04-25T15:23:51-07:00</updated><id>https://acb-prgm.github.io/ACB-Blog/feed.xml</id><title type="html">Aaron C. Bastian</title><subtitle>personal description</subtitle><author><name>Aaron Bastian</name><email>aaronbastian31@gmail.com</email></author><entry><title type="html">Sentiment Analysis of Twitter Data</title><link href="https://acb-prgm.github.io/ACB-Blog/posts/2023/03/Phase4Project/" rel="alternate" type="text/html" title="Sentiment Analysis of Twitter Data" /><published>2023-03-29T00:00:00-07:00</published><updated>2023-03-29T00:00:00-07:00</updated><id>https://acb-prgm.github.io/ACB-Blog/posts/2023/03/blog-post-4</id><content type="html" xml:base="https://acb-prgm.github.io/ACB-Blog/posts/2023/03/Phase4Project/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;This project aims to develop a machine learning model for predicting sentiment about various products on Twitter. Social media platforms like Twitter provide valuable data for analyzing consumer opinions, but manual analysis can be time-consuming and resource-intensive. Machine learning automates sentiment analysis, enabling businesses to quickly and accurately analyze social media data and gain valuable insights. By leveraging machine learning algorithms, businesses can make data-driven decisions, improve customer satisfaction, and stay competitive in the marketplace.&lt;/p&gt;

&lt;p&gt;The data science steps used in this project include data collection, data cleaning, text processing, feature engineering, and machine learning model development and evaluation.&lt;/p&gt;

&lt;p&gt;The goals for this project were to:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Develop a machine learning model to predict sentiment about apple products on Twitter&lt;/li&gt;
  &lt;li&gt;Demonstrate the effectiveness of the model in predicting sentiment&lt;/li&gt;
  &lt;li&gt;Outline the benefits and limitations of using a sentiment analysis model for business consumer opinion research&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;methods&quot;&gt;Methods&lt;/h2&gt;
&lt;h3 id=&quot;data-collection&quot;&gt;Data Collection&lt;/h3&gt;
&lt;p&gt;Two datasets were combined and filtered resulting in ~6,000 tweets about various products and services&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://data.world/crowdflower/brands-and-product-emotions&quot;&gt;CrowdFlower Dataset&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;Human raters rated the sentiment in over 9,000 Tweets as positive, negative, or neither.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.kaggle.com/competitions/tweet-sentiment-extraction/data&quot;&gt;Sentiment Analysis: Emotion in Text&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;Kaggle Dataset with &amp;gt;25k tweets&lt;/li&gt;
      &lt;li&gt;filtered for those with reference to products and services (the products used as filters can be found in the jupyter notebook)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There was a signifficant class imbalance, and the majority class (neutral), was undersampled to address this in part.  Class weights were used to address the remaining imbalances during model training.&lt;/p&gt;

&lt;p&gt;The data was split into Train, Validation, and Test sets.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Models learned on the train set&lt;/li&gt;
  &lt;li&gt;Tweaked using the validation set performance&lt;/li&gt;
  &lt;li&gt;Evaluated on the test set performance&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;most-common-words-by-sentiment&quot;&gt;Most Common Words by Sentiment&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/228379606-cd223ed4-f4bf-45f8-9747-7f1b5e5d2a56.png&quot; alt=&quot;MostCommonWords&quot; width=&quot;1000&quot; align=&quot;center&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;text-processing&quot;&gt;Text Processing&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Cleaning&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Remove irrelevant data such as URLs, digits, non-ASCII characters, single letter words, capitalization, and punctuation.&lt;/li&gt;
      &lt;li&gt;Replaced filtered swear words denoted with repeating asterisks with the word “cuss”&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Normalization&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Removed Stop words via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nltk&lt;/code&gt; library&lt;/li&gt;
      &lt;li&gt;Lemmatized words to their base form&lt;/li&gt;
      &lt;li&gt;Replaced all references to specific products/services with the word “product”&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Vectorization&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Using a technique called bag-of-words, each word is assigned a numerical value.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/228379516-09e93837-5050-488f-893f-dcc610c8617e.png&quot; alt=&quot;TextProcessing&quot; width=&quot;500&quot; align=&quot;center&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;model-development-and-evaluation&quot;&gt;Model Development and Evaluation&lt;/h3&gt;
&lt;p&gt;The pre-processed text data was then used to develop machine learning models for sentiment analysis. Two categories of models were developed: a binary classification model for predicting positive or negative sentiment, and a multiclass classification model which added a Neutral class. In each category, four types of models were created and evaluated:, Logistic Regression, SVC, Recurrent Neural Network, Hybrid Convolutional Neural Network.&lt;/p&gt;

&lt;p&gt;The machine learning models were trained and evaluated on training and validation datasets respectively. The performance of the models was then evaluated and compared on metrics of accuracy, precision, recall, and macro-averaged F1 score to determine their effectiveness in predicting sentiment about products on Twitter.&lt;/p&gt;

&lt;h2 id=&quot;results&quot;&gt;Results&lt;/h2&gt;
&lt;h3 id=&quot;binary-models&quot;&gt;Binary Models&lt;/h3&gt;
&lt;p&gt;The SVC model performed the best with an accuracy of 90% and an average F1-Score of 81, indicating that it accurately predicts each sentiment. This indicates that the level of complexity of the problem is suited to the level of complexity of the SVC model.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/228379627-42c3dd07-11dc-4393-adfd-b8ed54db4155.png&quot; alt=&quot;BinaryResults&quot; width=&quot;500&quot; align=&quot;center&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;multi-class-models&quot;&gt;Multi-Class Models&lt;/h3&gt;
&lt;p&gt;The SVC model performed the best with an accuracy of 63% and an average F1-Score of 61. The poor performance is due to the difficulty of determining the Neutral class due to the significant overlap as described earlier.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/228379639-3fb07f92-ea89-4860-b10b-dd743fa58c7b.png&quot; alt=&quot;MultiResults&quot; width=&quot;500&quot; align=&quot;center&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;value-and-deployment&quot;&gt;Value and Deployment&lt;/h2&gt;
&lt;h3 id=&quot;use-cases&quot;&gt;Use Cases&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Identifying areas for improvement&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;For example, if the model detects a high volume of negative sentiment related to a particular product feature or customer service issue, Apple can investigate the issue and take steps to address it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Respond to customer feedback&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;By monitoring sentiment in real-time, Apple can identify and address customer issues and concerns before they escalate, improving customer satisfaction and loyalty.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Informing product development&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;By understanding which features or products are most commonly associated with positive or negative sentiment, Apple can prioritize development efforts and make more informed decisions about product features and design.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Monitoring competitors&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The sentiment analysis model can be used to monitor sentiment about Apple’s competitors on Twitter, providing insights into the strengths and weaknesses of competing products and brands.
    &lt;h3 id=&quot;deployment-suggestion&quot;&gt;Deployment Suggestion&lt;/h3&gt;
    &lt;p&gt;Our binary sentiment analysis model can output a probability score that indicates how confident it is in its prediction. By setting a threshold of 75%, we can include only highly confident tweets, improving the quality and reliability of our predictions. Confidence thresholding ensures that the sentiment analysis model provides valuable insights to businesses.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In conclusion, this project demonstrates the value of sentiment analysis for businesses looking to gain insights into consumer opinion and improve decision-making. By accurately predicting sentiment about products on Twitter, businesses can identify areas for improvement, respond to customer feedback, and increase customer satisfaction and loyalty.&lt;/p&gt;

&lt;p&gt;Binary sentiment classification (positive and negative) achieved higher accuracy compared to the multi-class model. The difficulty in training models in this space is due to a number of factors. Firstly, tweets are a particularly difficult medium to perform NLP on because of the short length, use of slang, and intentional and unintentional grammatical and spelling errors. Second is the size of our dataset. After undersampling the majority class, we were left with less than 6k tweets. Many of the highest performing NLP models are trained on millions if not billions of datapoints. Finally, I believe that the dataset’s labeling contains many errors and/or tweets that people may not label into any one category as they contain both positive and negative aspects.&lt;/p&gt;

&lt;p&gt;While the results may be limited by the size and diversity of the dataset, sentiment analysis of tweets can provide valuable insights into customer opinions and attitudes towards products or services. This work demonstrates the potential for NLP sentiment analysis to be used in real-world applications for business decision-making. The machine learning models developed in this project provide a proof of concept for the effectiveness of sentiment analysis in consumer opinion research. Further work can be done to improve the accuracy and robustness of the models and to apply them to larger datasets and in more complex scenarios.&lt;/p&gt;</content><author><name>Aaron Bastian</name><email>aaronbastian31@gmail.com</email></author><category term="DataScience" /><category term="NLP" /><category term="Sentiment_analysis" /><category term="Machine-Learning" /><summary type="html">Introduction This project aims to develop a machine learning model for predicting sentiment about various products on Twitter. Social media platforms like Twitter provide valuable data for analyzing consumer opinions, but manual analysis can be time-consuming and resource-intensive. Machine learning automates sentiment analysis, enabling businesses to quickly and accurately analyze social media data and gain valuable insights. By leveraging machine learning algorithms, businesses can make data-driven decisions, improve customer satisfaction, and stay competitive in the marketplace.</summary></entry><entry><title type="html">Predicting Hospital Readmissions Using Machine Learning</title><link href="https://acb-prgm.github.io/ACB-Blog/posts/2023/05/Phase5Project/" rel="alternate" type="text/html" title="Predicting Hospital Readmissions Using Machine Learning" /><published>2023-03-29T00:00:00-07:00</published><updated>2023-03-29T00:00:00-07:00</updated><id>https://acb-prgm.github.io/ACB-Blog/posts/2023/05/blog-post-5</id><content type="html" xml:base="https://acb-prgm.github.io/ACB-Blog/posts/2023/05/Phase5Project/">&lt;h2 id=&quot;abstract&quot;&gt;Abstract&lt;/h2&gt;
&lt;p&gt;This project focuses on predicting the likelihood of readmission within 30 days of initial discharge for diabetes patients using machine learning algorithms. I developed and evaluated various models, including random forests, SVC with RBF kernel, gradient boosting, and neural networks, to identify high-risk patients and provide targeted interventions to reduce the likelihood of readmission. By analyzing admission, demographic, clinical, medication, and discharge data, we were able to develop a predictive model that can be used by healthcare providers to optimize resource allocation, improve care coordination, and inform policy and practice in the field of diabetes care and readmission prevention. The insights gained from this project demonstrate the potential of data science and machine learning in healthcare and contribute to efforts to improve patient outcomes and reduce healthcare costs.&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Hospital readmissions within 30 days of discharge are a common problem, especially for patients with chronic conditions like diabetes. Not only do readmissions negatively impact patient outcomes, but they also increase healthcare costs and strain the capacity of the healthcare system. In response to this issue, Medicare established the Hospital Readmissions Reduction Program (HRRP), which financially penalizes hospitals with higher-than-expected rates of readmissions. To address this problem, I developed machine learning models that can identify patients who are at risk for 30-day readmissions. These models analyze key features and patterns to inform treatment plans and reduce the likelihood of readmissions. By developing and implementing these models, we aim to address the challenges of preventable readmissions, improve patient outcomes, and reduce healthcare costs. Additionally, our models can be used in real-time to predict the probability of a patient’s readmission, enabling providers to modify treatment plans dynamically and optimize patient care.&lt;/p&gt;

&lt;h3 id=&quot;project-goals&quot;&gt;Project Goals&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Develop&lt;/strong&gt; a machine learning model to predict &amp;lt;30-day readmission based on patient treatment and discharge data&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Identify&lt;/strong&gt; key patient and treatment features that are most predictive of readmissions&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Provide recommendations&lt;/strong&gt; to help reduce the number of preventable readmissions&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;methods&quot;&gt;Methods&lt;/h2&gt;
&lt;h3 id=&quot;data&quot;&gt;Data&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://archive-beta.ics.uci.edu/dataset/296/diabetes+130+us+hospitals+for+years+1999+2008&quot;&gt;Source&lt;/a&gt; [1]&lt;/p&gt;

&lt;h4 id=&quot;overview&quot;&gt;Overview&lt;/h4&gt;
&lt;p&gt;The “Diabetes 130-US hospitals for years 1999-2008” dataset from the University of California - Irvine, Machine Learning Repository represents 10 years of clinical care at 130 US hospitals and integrated delivery networks. It includes over 101,000 instances with more than 50 features, such as demographic information, health history, admission and treatment information, and discharge information.&lt;/p&gt;
&lt;h4 id=&quot;preparation&quot;&gt;Preparation&lt;/h4&gt;
&lt;p&gt;The dataset includes three classes for readmission, which were simplified into a binary classification for the purpose of the analysis: Class 0 includes NO and &amp;gt;30 readmissions, while Class 1 includes &amp;lt;30 readmissions. The dataset underwent several preprocessing steps, including de-noising, feature engineering, and undersampling to address class imbalance. These steps were taken to ensure the quality and accuracy of the data and to improve the performance of the machine learning models used in the analysis. The data was split into train (80%) and validation (20%) sets.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/233452006-59e2a030-5de2-4c83-8b4e-4a1d197c8ee0.png&quot; alt=&quot;Undersampling&quot; width=&quot;850&quot; align=&quot;center&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;models&quot;&gt;Models&lt;/h3&gt;
&lt;p&gt;I developed and evaluated several machine learning models, including Random Forest, SVC (Support Vector Classifier), LightGBM &amp;amp; CatBoost (Gradient Boosting), and Recurrent Neural Network (RNN) on their accuracy, precision, recall, and f-1 scores.&lt;/p&gt;

&lt;h2 id=&quot;results&quot;&gt;Results&lt;/h2&gt;
&lt;h3 id=&quot;models-performance&quot;&gt;Models’ Performance&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/233452981-cdf0a7cd-853c-496a-8069-cbe771d10d4a.png&quot; alt=&quot;Undersampling&quot; width=&quot;850&quot; align=&quot;center&quot; /&gt;
After training, tuning, and evaluation, all models achieved similar metrics, indicating that I have likely extracted the maximum amount of information from the dataset. Interestingly, the simple random forest model achieved the same accuracy as the more complex LGBM and Catboost models, but achieved a higher target F-1 score. Given the task of predicting the likelihood of readmission within 30 days of initial discharge for diabetes patients, I prioritized minimizing false negative errors over false positive errors. Therefore, I chose the random forest model for further feature analysis.&lt;/p&gt;

&lt;h3 id=&quot;feature-importances&quot;&gt;Feature Importances&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/233454146-b67bd2e4-f3d1-4afd-a750-ac057bd305c7.png&quot; alt=&quot;Undersampling&quot; width=&quot;850&quot; align=&quot;center&quot; /&gt;
It’s important to note that the relationships observed between certain variables and the risk of readmission may be correlational rather than causal. For example, hospital visits, the number of diagnoses, the number of medications, and the discharge facility may be secondary to a patient’s health status, which may be the primary predictor of readmission risk. However, it’s interesting to note that more procedures appear to be associated with a lower risk of readmission, suggesting that this may be a potentially important factor to consider when developing strategies for preventing readmissions, although more research is necessary.&lt;/p&gt;

&lt;h3 id=&quot;discharge-facilities-by-readmission-probability&quot;&gt;Discharge Facilities by Readmission Probability&lt;/h3&gt;
&lt;p&gt;I realized there were several important features relating to the discharge facility, so I investigated this further:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/233455184-93370427-028c-445b-88f6-2faadd98a9e4.png&quot; alt=&quot;Undersampling&quot; width=&quot;500&quot; align=&quot;center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;My analysis revealed that rehab facilities had the highest predicted readmission probability, while discharge home had the lowest predicted readmission probability. Additionally, transferring a patient to inpatient care at the same hospital was associated with a lower readmission probability compared to transferring to a different hospital. It’s important to note that these findings demonstrate a correlation between certain variables and the risk of readmission, but do not necessarily indicate causation. Nonetheless, these insights can inform clinical decision-making and help me as a healthcare provider develop targeted interventions to reduce the risk of readmissions for diabetes patients.&lt;/p&gt;

&lt;h2 id=&quot;application-and-value&quot;&gt;Application and Value&lt;/h2&gt;
&lt;h3 id=&quot;deployment&quot;&gt;Deployment&lt;/h3&gt;
&lt;p&gt;The machine learning model can be integrated with electronic health records (EHRs) to assess a patient’s risk of readmission in real-time. This integration would allow healthcare providers to intervene early and provide targeted interventions and informed care plans, potentially reducing the risk of readmission for diabetes patients. By leveraging the predictive capabilities of the model and integrating it with EHRs, healthcare providers can take a proactive approach to managing the health of their patients and improving health outcomes.&lt;/p&gt;
&lt;h3 id=&quot;value&quot;&gt;Value&lt;/h3&gt;
&lt;p&gt;The machine learning model’s real-time risk assessment capabilities can enable healthcare providers to develop informed care and discharge plans for high-risk patients. This targeted approach to patient care can potentially reduce the risk of readmission for diabetes patients and optimize resource allocation within healthcare systems. In addition, leveraging the predictive capabilities of the model can inform quality improvement (QI) initiatives, allowing healthcare providers to continuously improve patient outcomes and reduce healthcare costs associated with readmissions.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In conclusion, hospital readmissions within 30 days of discharge negatively impact patient outcomes, decrease profits through Medicare’s Hospital Readmissions Reduction Program (HRRP), increase healthcare costs, and strain healthcare system capacity. Our machine learning model provides healthcare providers with a valuable tool to address these challenges by identifying high-risk patients and enabling the development of informed care and discharge plans. By leveraging the model’s predictive capabilities, healthcare providers can improve patient outcomes, reduce healthcare costs, and optimize resource allocation, ultimately improving the quality of care for diabetes patients.&lt;/p&gt;

&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;p&gt;[1] Clore,John, Cios,Krzysztof, DeShazo,Jon &amp;amp; Strack,Beata. (2014). Diabetes 130-US hospitals for years 1999-2008. UCI Machine Learning Repository. https://doi.org/10.24432/C5230J.&lt;/p&gt;</content><author><name>Aaron Bastian</name><email>aaronbastian31@gmail.com</email></author><category term="DataScience" /><category term="Health Informatics" /><category term="Health Care" /><category term="Machine-Learning" /><summary type="html">Abstract This project focuses on predicting the likelihood of readmission within 30 days of initial discharge for diabetes patients using machine learning algorithms. I developed and evaluated various models, including random forests, SVC with RBF kernel, gradient boosting, and neural networks, to identify high-risk patients and provide targeted interventions to reduce the likelihood of readmission. By analyzing admission, demographic, clinical, medication, and discharge data, we were able to develop a predictive model that can be used by healthcare providers to optimize resource allocation, improve care coordination, and inform policy and practice in the field of diabetes care and readmission prevention. The insights gained from this project demonstrate the potential of data science and machine learning in healthcare and contribute to efforts to improve patient outcomes and reduce healthcare costs.</summary></entry><entry><title type="html">Maximize Website E-Commerce Revenue by Predicting Users Purchases</title><link href="https://acb-prgm.github.io/ACB-Blog/posts/2023/01/Phase3Project/" rel="alternate" type="text/html" title="Maximize Website E-Commerce Revenue by Predicting Users Purchases" /><published>2023-01-20T00:00:00-08:00</published><updated>2023-01-20T00:00:00-08:00</updated><id>https://acb-prgm.github.io/ACB-Blog/posts/2023/01/blog-post-3</id><content type="html" xml:base="https://acb-prgm.github.io/ACB-Blog/posts/2023/01/Phase3Project/">&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;
&lt;p&gt;The purpose of this notebook is to simulate solving a real world machine learning classification problem.  I use several ensemble tree models (XGBoost and Light GBM) to predict whether a user’s activity on a company website will translate to a sale with ~94% accuracy and good precision.  This would allow for the comapny to optimize their website, better target their marketing, and incorporate dynamic pricing into their site in order to boost revenue.&lt;/p&gt;

&lt;h3 id=&quot;resources&quot;&gt;Resources&lt;/h3&gt;
&lt;p&gt;• &lt;a href=&quot;https://github.com/ACB-prgm/Phase3Project.nosync/blob/master/student.ipynb&quot;&gt;Jupyter Notebook / Code&lt;/a&gt;
• &lt;a href=&quot;https://github.com/ACB-prgm/Phase3Project.nosync/blob/master/Phase_3_Presentation.pdf&quot;&gt;Presentation&lt;/a&gt;
• &lt;a href=&quot;https://github.com/ACB-prgm/Phase3Project.nosync/tree/master/Data&quot;&gt;Data&lt;/a&gt;
• &lt;a href=&quot;https://www.kaggle.com/competitions/online-purchase-prediction/data?select=shop_train.csv&quot;&gt;Kaggle Page&lt;/a&gt;
• &lt;a href=&quot;https://github.com/ACB-prgm/Phase3Project.nosync/commit/3bc555b94ccaea46d81b57e55b0b8e43603d21ef&quot;&gt;Final Kaggle Submission&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;data&quot;&gt;Data&lt;/h3&gt;
&lt;p&gt;Each row represents a user’s session on an online store, with 6165 user sessions in total (~15% of which resulted in purchases).&lt;br /&gt;
The following are the column/feature descriptions:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;admin_pages&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;info_pages&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product_pages&lt;/code&gt; - number of pages in different categories visited by the user&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;admin_seconds&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;info_seconds&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product_seconds&lt;/code&gt; - time spent by the user on different page categories&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;page_value&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bounce_rate&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quit_rate&lt;/code&gt; - numbers from Google Analytics&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;is_holiday&lt;/code&gt; - the proximity of important days for retail (such as the New Year)&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;month&lt;/code&gt; - month (categorical variable)&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;operating_system_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;browser_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;region_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traffic_type_id&lt;/code&gt; are also categorical variables, although they are written as numbers&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;is_new_visitor&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;is_weekend&lt;/code&gt; - binary signs&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;has_purchase&lt;/code&gt; - binary attribute, target variable. It is he who needs to learn to predict.&lt;/p&gt;

&lt;h3 id=&quot;methods&quot;&gt;Methods&lt;/h3&gt;
&lt;h4 id=&quot;previewing-the-data&quot;&gt;Previewing the Data&lt;/h4&gt;
&lt;p&gt;I first inspected the data for abnormalities including Nan values, outliers, odd distributions, etc, and found that this was a fairly clean dataset.  The exception was that the there was a large class imbalance as most users will not purchase anything.  This is part of the reason why I chose to use ensemble methods as they are robust to class imbalances.&lt;/p&gt;
&lt;h4 id=&quot;data-preparation-and-pipeline-construction&quot;&gt;Data Preparation and Pipeline Construction&lt;/h4&gt;
&lt;p&gt;Then I split the data into training and validation data, and began creating my preprocessing pipeline. I created a custom function that would convert the month variable to a numerical and ordinal variable, and added the rows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;total_time&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;avg_time&lt;/code&gt;, which represent the total amount of time the user spent on the website and the average amount of time they spent in the three categories respectively. Next, I used sklearn’s standard scaler to scale the entire dataset to ensure the model would interpret the features on the same scale.  Finally I added a classifier, first XGBoost, then Light GBM.&lt;/p&gt;
&lt;h4 id=&quot;model-training-and-hyperparameter-tuning&quot;&gt;Model Training and Hyperparameter Tuning&lt;/h4&gt;
&lt;p&gt;I fit the pipeline on the training data and scored it on the validation data, looking at the accuracy, f1-score, ROC-AUC, and precision as metrics.  I noticed that the model was overfitting by observing a near perfect performance on the training data and considerably worse performance on the validation data.  I also noticed that the precision and f1-scores were very low.  Thus, I next performed a grid search and selected hyperparameters to affect these issues.  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n_estimators&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_depth&lt;/code&gt; to reduce overfitting, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale_pos_weight&lt;/code&gt; to balance the weight of the positive class (minority class) and increase precision. I then scored the model by submitting the model’s predictions to kaggle.  I repeated the process with the Light GBM model and achieved slightly better results.&lt;/p&gt;

&lt;h3 id=&quot;results&quot;&gt;Results&lt;/h3&gt;
&lt;p&gt;The final LGBM model had an accuracy of 93.76% with a higher false potive count than false negative count.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/212971884-ce35d90a-d889-4e4e-a740-56b4ab77781f.png&quot; alt=&quot;Cfmatrix&quot; width=&quot;500&quot; align=&quot;center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The model found admin and product pages to be very important, and validated that the google statistic “page value” is a good indicator of a page’s sale turnover.  It also showed that direct raffic and new users were more likely to make a sale, thus spending on increasing our findability in Google searches.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/212971886-8789cf0e-cb34-42d3-98fd-f8295756fe51.png&quot; alt=&quot;Cfmatrix&quot; width=&quot;500&quot; align=&quot;center&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;LGBM was considerably faster to train and slightly more performant than XGBoost in this scenario.  This approach proved effective and can be used for real world prediction of purchases as well as other website and application end points.&lt;/p&gt;</content><author><name>Aaron Bastian</name><email>aaronbastian31@gmail.com</email></author><category term="DataScience" /><category term="E-Commerce" /><category term="Machine-Learning" /><summary type="html">Introduction The purpose of this notebook is to simulate solving a real world machine learning classification problem. I use several ensemble tree models (XGBoost and Light GBM) to predict whether a user’s activity on a company website will translate to a sale with ~94% accuracy and good precision. This would allow for the comapny to optimize their website, better target their marketing, and incorporate dynamic pricing into their site in order to boost revenue.</summary></entry><entry><title type="html">Home Features to Maximize Sale Price</title><link href="https://acb-prgm.github.io/ACB-Blog/posts/2022/12/Phase2Project/" rel="alternate" type="text/html" title="Home Features to Maximize Sale Price" /><published>2022-12-06T00:00:00-08:00</published><updated>2022-12-06T00:00:00-08:00</updated><id>https://acb-prgm.github.io/ACB-Blog/posts/2022/12/blog-post-2</id><content type="html" xml:base="https://acb-prgm.github.io/ACB-Blog/posts/2022/12/Phase2Project/">&lt;h2 id=&quot;inroduction&quot;&gt;Inroduction&lt;/h2&gt;
&lt;p&gt;This linear regression analysis of the King County housing dataset will serve to inform real estate construction companies in Washington looking to invest in building a new home or residential community. The goals are to discover which features of a home increase its sale value and to quantitatively define the relationship between those features and the price of a home.&lt;/p&gt;

&lt;p&gt;The GitHub  repository for this project can be found &lt;a href=&quot;https://github.com/ACB-prgm/Phase2Project&quot;&gt;here&lt;/a&gt;. The presentation of my analysis and results can be found at &lt;a href=&quot;https://github.com/ACB-prgm/Phase2Project/blob/master/Presentation.pdf&quot;&gt;Presentation.pdf&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-data&quot;&gt;The Data&lt;/h2&gt;
&lt;p&gt;The King County dataset is a public dataset available on &lt;a href=&quot;https://www.kaggle.com/datasets/harlfoxem/housesalesprediction&quot;&gt;Kaggle&lt;/a&gt; as well the the King County Website.&lt;/p&gt;

&lt;p&gt;This dataset contains over 15k clean data points representing house sales in King County between May 2014 - May 2015. Each data point includes data on amenities, number of bedrooms/bathrooms, square footage, renovations, location, views, and sale price.&lt;/p&gt;

&lt;h2 id=&quot;analysis&quot;&gt;Analysis&lt;/h2&gt;
&lt;h3 id=&quot;data-cleaning-and-preparation&quot;&gt;Data Cleaning and Preparation&lt;/h3&gt;
&lt;p&gt;I first imported and cleaned the data by dropping columns including NaN values and converting the ordinal string values to integers so that I could screen for correlations with price.
Before doing that though, I looked at the distribution of price, our target, and realized it was heavily skewed due to a few extraordinarily high priced outliers.  I removed the outliers using the IQR method, and the distribution of price normalized.
Then I ran pearson correlations with the entire dataframe and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;price&lt;/code&gt; and included all that had medium to strong correlations.  I also included two categorical variables, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;view&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;waterfront&lt;/code&gt;, as their scatter plots appeared linearly related to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;price&lt;/code&gt;.  I turned them into dummy variables, dropping the values with no view and no waterfront to serve as references.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/202476532-c3bfc563-8179-41f0-a78f-9590dbb1cd2d.png&quot; alt=&quot;Heatmapfull&quot; width=&quot;500&quot; align=&quot;center&quot; /&gt; &lt;img src=&quot;https://user-images.githubusercontent.com/63984796/202477228-9e3f8d38-ad89-4be1-a84b-350d52025bcc.png&quot; alt=&quot;SmallHeatmap&quot; width=&quot;500&quot; align=&quot;center&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;baseline-model&quot;&gt;Baseline Model&lt;/h3&gt;
&lt;p&gt;I created a baseline linear regression using these independant variables:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bathrooms&lt;/code&gt; - Number of bathrooms in the house&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sqft_living&lt;/code&gt; - Square footage of living space in the home&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Grade&lt;/code&gt; - Construction and material quality (related to construction cost) on a scale of  1 to 13&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Latitude&lt;/code&gt; - Latitude of the house (distance from southern parts of the county in this case)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sqft_living15&lt;/code&gt; - Square footage of living space for the nearest 15 neighbors&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Floors&lt;/code&gt; - Number of floors in the house&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;View&lt;/code&gt; - The views from the house on the following scale: NONE - FAIR - AVERAGE - GOOD - EXCELLENT&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Waterfront&lt;/code&gt; - Whether or not the house is a waterfront property&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of the assumptions of linear regression were met (Linearity, ~Independance, Normality, and Homoscedasticity).  The model was statistically signifficant and abke to explain ~65% of the variance in the data.&lt;/p&gt;

&lt;h3 id=&quot;final-model&quot;&gt;Final Model&lt;/h3&gt;
&lt;p&gt;To get to the final model, I went through several iterations of small adjustments. The Heterscedasticity plot and qq-plot of the baseline model looked slightly irregular and the target variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;price&lt;/code&gt; had a slightly irregular distribution, so I log transformed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;price&lt;/code&gt;.  This improved the adj. R2 value to .668 (ie. explaining ~67% of the variance).  Then I identified that the number of floors had some interaction terms within it that had higher correlations than the whole, so I created dummy variables out of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;floors&lt;/code&gt; and added them to the independant variable.  This improved our adj. R2 to 0.677.  Adding the interaction terms actually decreased the R2 and added needless complexity to the interpretation of the model, so I backtracked to the previous model. Finally, I applied the natural log to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sqft_living&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sqft_living15&lt;/code&gt; as they seemed to have non-normal distributions and may have a non-linear relationship with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;price&lt;/code&gt;.  This slightly improved the model again, increasing the adj. R2 to 0.682.&lt;/p&gt;

&lt;h2 id=&quot;results&quot;&gt;Results&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/63984796/202478722-073b4baa-b53d-473a-a2c0-9b018179671d.png&quot; alt=&quot;Predictors&quot; width=&quot;300&quot; align=&quot;right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The model statistically signifficantly explains ~68% of the variance in sale price.&lt;/p&gt;

&lt;p&gt;From the normalized results, we can see that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sqft_living&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grade&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lat&lt;/code&gt; have the most effect on our model. Of those, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sqft_living&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grade&lt;/code&gt; are the most actionable for the purposes of a construction company.&lt;/p&gt;

&lt;p&gt;For each additional bathroom, we see an associated increase of ~1.31% in price.&lt;br /&gt;
&lt;strong&gt;For each additional grade, we see an associated increase of ~13.35% in price.&lt;/strong&gt;&lt;br /&gt;
For each additional degree of latitude in King County, we see an associated increase of ~321.30% in price.&lt;br /&gt;
&lt;strong&gt;For each increase of 1% sqft_living, we see an associated increase of ~0.32% in price.&lt;/strong&gt;&lt;br /&gt;
For each increase of 1% sqft_living15, we see an associated increase of ~0.17% in price.&lt;br /&gt;
Houses on a waterfront have a ~42% higher price compared to those not on a waterfront.&lt;br /&gt;
Houses with a “fair” view have a ~20.5% higher price tag than those with “no view”.&lt;br /&gt;
Houses with a “average” view have a ~18.6% higher price tag than those with “no view”.&lt;br /&gt;
Houses with a “good” view have a ~22.4% higher price tag than those with “no view”.&lt;br /&gt;
Houses with a “excellent” view have a ~31.4% higher price tag than those with “no view”.&lt;br /&gt;
Houses with 1.5 floors have a ~14.8% higher price tag than those with one floor.&lt;br /&gt;
Houses with 2.5 floors have a ~11.1% higher price tag than those with one floor.&lt;/p&gt;

&lt;h2 id=&quot;recommendations&quot;&gt;Recommendations&lt;/h2&gt;
&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;With respect to `sqft_living’, It would be most profitable to maximize living squarefootage in any home constructed. This does not necessarily mean sacrificing non-living space, but is just a reccomendation to prioritize the size of areas such as living rooms and bedrooms over non-living areas.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;With respect to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grade&lt;/code&gt;, It would be most profitable to spend more on higher quality construction materials and have experienced contractors building the homes. This becomes an issue of diminishing marginal returns at a point that was not explained by the model. IE: at what point does it not become profitable to spend more on materials. This will need to be something decided by the individual construction company and their respective budgets.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;discussion-and-caveats&quot;&gt;Discussion and Caveats&lt;/h2&gt;
&lt;p&gt;Though we have a decent model, several of the predictors are not particularly useful.  Construction companies can rarely control whether their houses will be waterfront properties, have excellent views, or be further from the equator.  I considered removing latitude as it did not seem logical to me, but I left it in as I don’t know enough about the geography of King County, Washington to say definitively that northernmost areas are considered more desireable.  For instance, maybe northernmost houses are closer to big cities, or are nearer to better school districts.  Grade, while a useful variable describing the quality of contruction and materials, is not speciffic enough to generate any real insight other than “you get what you pay for”.&lt;/p&gt;</content><author><name>Aaron Bastian</name><email>aaronbastian31@gmail.com</email></author><category term="DataScience" /><category term="Housing" /><category term="RealEstate" /><summary type="html">Inroduction This linear regression analysis of the King County housing dataset will serve to inform real estate construction companies in Washington looking to invest in building a new home or residential community. The goals are to discover which features of a home increase its sale value and to quantitatively define the relationship between those features and the price of a home.</summary></entry></feed>