Machine Learning for Healthcare
Key Takeaways
The video demonstrates the application of machine learning in healthcare using Python analytics libraries, focusing on analyzing patient insurance metrics and training a clustering model. It also covers writing clear prompts to generate Python code and interpreting AI-generated responses.
Full Transcript
Hello everyone and welcome to today's session. My name is Ree and I'll be your host today. We're going to get started very shortly at the top of the hour. We're just waiting so everyone has a chance to join. If you haven't done so already, please make sure that you register for today's session. You can do so by uh scanning the QR code that is uh linked or not scanning the QR code that's on screen, not linked anywhere else. Excuse me. Um if you would like to code along with us today or prompt along with us today, we do have a data lab notebook that we're going to be working in. So that link uh the link to the resources which contains the link to create a copy of the notebook is in the chat. It's pinned in the chat and it's also in the video description. So have a look there if you'd like to join in with today's session. It should be a super easy setup today. And we've also unlocked the notebook so that you have as many AI prompts as you need. So please let me know if you have any issues with the notebook today. However, it should be all perfect and ready to go for you. So yeah, please do have a look at that early. That gives me a little bit more time to fix it if there are any issues. So yeah, for anyone that's just joined, thank you so much for joining early. We're going to be getting started very shortly. Welcome to the second session in our uh AI powered Python series. Yesterday we looked at uh Chicago service data and today we're looking at machine learning for healthcare. So don't worry if you're a complete no novice. Today we will be running through the basics and uh yeah running through this prompt along. So yeah, please do stick around uh for the entirety of the session. We're going to be answering your questions at the end of the session. So, do make sure you stick around for that as well. Let us know where you're joining from. It's always good to see the variety of locations we've got people joining from. And also, if this is your first time at a data camp session, please let us know. And if you're a returning uh attendee, please also let us know. Um, Jennifer's just asked, are there any workarounds for organizations that don't support the notebook loading? I these sessions do not require a paid data camper account. So you can do these for free uh from a personal device. So if you do if you are part of an organization that already has access to data camp and it's blocked for you because we've got the AI features enabled, I would recommend joining from a personal device and personal account. Again, you do not need a data camp premium account to go along with us today. Everything is for free and you will get unlimited prompts as well. So yeah, please do uh use a personal account if you're having any issues. Brilliant. I think that's it. So I think we'll uh we'll get started today. So let me kill the music and I will appear on screen. Hello everyone and welcome to the second session in our air powered python series. Uh this one's going to be really really good. I've already had a look at the notebook and yeah we have a fantastic session lined up for you. So without further ado, let's get started. So as we mentioned yesterday, Python is the world's most popular programming language and if you want to be a data scientist, you need to know Python. These days, AI can write code well. So it's a great way to learn uh via prompting and carefully examining the output. Yesterday's focus was on data manipulation. Today we're going to look at machine learning. Our guest is Oha Insarat and she's a data senior data scientist from first leaf and a co-founder at Insight Delta. So I will bring her on screen. Pleasure to have you on. >> Thanks. >> Um brilliant. Uh she is a data scientist and entrepreneur and her in at her current data scientist role she builds wine recommendation models. Aha also founded data science software provider inside Delta and she was also an AI consultant at Google. She's also the instructor of two data camp courses including reshaping data with pandas. So yeah without further ado over to you. >> Thanks thanks. So uh hi everyone and and thanks for joining uh this session. Uh as as mentioned my name is and today we are going to do something really cool. uh we are going to use machine learning to segment patients into meaningful groups and the best part as as we mentioned is that we are going to use AI to help us write the code. So even if you're just getting started with Python then you will be able to follow along. So uh let's jump in. uh what we have planned for today is like first I will give you a quick intro on what clustering is and and what to look for when you work with health data and then we'll do a step-by-step hand ons uh with the help of AI that you are here uh to see that and at the end uh we'll have a few minutes for questions uh so you we can answer any doubt that that you have so to start uh what is uh clustering when I mentioned that uh we are going to segment the patients uh into meaningful group that is uh what I mentioned uh what I refer to clustering. So in simple terms uh clustering is finding groups in your data without telling the computer or the algorithm what to look for. We are not uh telling uh what is the features that are important. The algorithm discovers by themselves. So they discover uh patterns inside the data and then form groups uh with these uh different uh in this case uh patients. Uh so imagine that you have a pile of photos of animals. Uh if I give you to you those then you can probably tell which is a a set of cats, a set of dogs and a set of other type of animal. So that is like what clustering does. Um so now the thing is like uh to understand is like how we uh or the algorithm understand which patients or which uh objects are clustered together which form the same group. Well for that we need to use distance. The distance uh the most common one is the oblivion distance and it's the one that we work uh every day like when you say if you go from a point A to a point B then you will uh have 10 kilometers there is a distance it's a linear distance and it works very well for numeric data but uh sometimes we have other features that they are not numerical that they are categorical and that is the case of the healthcare their data. Sometimes we don't have only numerical data but we have a mixed data and that is where it gets very interesting and that is why we are going to test the AI on how does the AI handles this type of mixed data and that is why our knowledge uh becomes very important when working with AI to tell them uh this that you told me is correct or this is uh this is not. So uh to jump into what data we are going to use uh we are going to use a data set from Kaggle that is called patient segmentation. Uh you will have it uh the link in the in the notebook that uh R uh shared with you that that you will have and uh this uh segmentation data set has different records on patients. It has age, gender, state, city, uh any clinical features like uh the BMA, the number of chronic conditions, uh the pattern of of those uh patients uh to visit the the the the doctor or the hospital and also some financial uh data as uh billing amount or insurance type. As you can see uh we said and I mentioned before this is like a challenge for any clustering algorithm because when we have numeric numeric data is easier to find distance and it's easier to say which one is close to the other one but with categorical variables that is gets more interested and complicated. So uh we are going to ask the AI probably the AI and I will mention this several times will tell us uh that the Cummins that is the most common clustering algorithm will be good and it will be done in one encoding. This means translating uh different categories into the presence or the absence of that feature would tell us that this is correct. And there as I mentioned before we have to get all the knowledge of machine learning that we have and tell them if she is correct or that is a better way to handle that clustering uh patterns and for that we are going to use another uh distance that is called lower distance. And we are going to use another type of clustering algorithm that is called hierarchic clustering. So uh as I mentioned we have here health data and this could uh be true for any type of data but particularly for health data. We have some cases where we already know that when we work with a data set related to patients then we are going to have for example a lot of missing values. Uh and this is true because sometimes uh this data is collected from doctors from um hospital staff and sometimes instead of like just putting uh zero of putting like a no then they leave the blank and we have a lot of missing data and we have to handle this because machine learning does not uh do well with missing data. Also, we're going to uh need to in scale the features and where is that? Because as I mentioned, distance h determines how close the patients are. So if we have a feature like for example age that go from zero to 100 and we have another uh feature that for example is billing it will go from zero to a few hundred or thousands of dollars and those features are in different scale and if we inputting them like that the clustering won't understand what we are doing and and will fail uh in clustering correctly as I mentioned several times we are going to work with mixed data type that is is very common in health data So we really need to handle them well uh to find patterns in in our in our patient groups. Also for sure in any data set but particularly in health data you are going to have non-informative columns such as like patient ID or city or state which don't tell us a lot about uh the patients or how they look similar. We need to see the distributions uh and the outliers to understand uh how they how they feature distributes if they are normally distributed if there are extreme values uh that we need to account for and finally and and more critical especially for today's webinar is understand that AI is very helpful today it can write like very good code but also uh can be wrong or also can suggest us something that is not correct. effect for our approach or for the knowledge uh domain knowledge that we already have. So it's always important to check what is the output of the AI, what is the code that the I has uh suggested and some of the of the work that we have to do is tell the AI no uh you need to try something different no for this data that doesn't work. So that is also part of the job and that is also part of what we need to understand when we use AI to help us go. So uh as I mentioned here's the road map uh for the code insertion. First we will load and explore the data. Then we will clean that data. Uh then we are going to see how that data looks like in what we call uh exploratory data analysis. We will ask first the AI for a clustering. we will be very um general and see what the ATA that AI digest and then we are going to propose an alternative approach for the AI to help us cluster it the patients in a in a better way and get better results and lastly uh when we work with clustering data what is important is not only to get the machine learning algorithm and to get the the the group but also understand what those profiles what those groups uh are telling us because we need the The most important part of the machine learning is not only applying and finding the more accurate uh uh machine learning but also to understand how we can use the data. cell uh for a call along uh with me uh then you can if you don't have it already then you can create the data d that account and uh you can open uh the link that link that uh I have shared or you can scan the QR code that is already shar and mentioned you will have several options uh you can do this the easy way where you will have a notebook where you already have the prompts that I will be using you can be uh doing the difficult one that is like you will create the prompts. But anyway, in any case, you will also have the completed notebook to review it and and and check with what you have done. So before we jump into the notebook, there is one thing that if you uh also join yesterday uh you have already seen but it's like when you tell the AI to help you code, you need to follow certain rule of thumbs. you need to follow certain uh patterns. If you have a vague prompt like for example in this case it's like analyze data then you will have a result but probably it's not the one that you want. So if you go to like for example a coffee a coffee shop and then you ask for a coffee probably you will get a coffee but probably not the one that you want. So you have to go to the coffee shop and tell them like I want a large uh latte. I want a large black coffee with cream and you will get two different coffees depending on what you tell uh the staff and in this case is the same. You have to be specific. You have to include the column names. You have to tell them which library you want to use in the case that that is appropriate. You always uh have to tell them that what is the desired output. The more specific that you are, the the the better the code will look like in what you are looking for and also you have to review and understand what mistakes and and and and the AI is doing and catch them uh before continue with the code. So yeah now uh let's move to the to the notebook then uh here. So uh you will see that in the notebook there is a a lot of explanation and that is like uh for you when you are following the the notebook to understand what is the situation in which you can apply this that I I have told you. So uh the first thing that we need to do uh when we start any notebook and we start any machine learning uh algorithm or or project is to import all and set up all the notebook to have the libraries in Python that you need. So uh for that I will tell the AI that I'm working with the data set and I need to do data exploration. I will do v visualization and clustering. >> Sorry to interrupt. Could we just switch on to the other tab? Yes, >> sorry. [laughter] Sorry. Sorry. I forgot to do that. Thanks a lot, Chris for that. >> So, yeah, as I was saying and and sorry for for that. Um you have the noted here and and you can see that you have like a lot of uh text and uh this is for you to follow and understand what you are uh what you are building to have a context. uh but the first thing that uh we are going to do uh is to tell the AI to import the libraries and to set up the notebook and for that I will I will not tell them which data I have uh because uh I I I don't know I maybe don't know I'm starting a notebook so I will be very specific but also like very general so I will I will tell them which process do I want to follow I want to do that exploration I want to do visualization and clustering But I don't know how the data looks like. So in that in this case we are going to tell the the AI to import these libraries and you can see here that they gave us like data manipulation like pandas and numpy. It also gave us some libraries for uh visualization like mplot lib and seaborn that they are important also plotly that is very important for uh interactive visualization machine learning uh libraries maybe we don't use them all but the doesn't know and and then we are uh we are going to see what it picks for for for this project um and uh lastly it sets some options to uh display the the max columns because this is very common that if we have like the a data set that has like a lot of columns sometimes you cannot see it completely. Okay. So after that we are going to load the data and one thing that I want to show you is like here I I inserted a SQL um cell and I'm going to read from this uh CSV file. But to do able to do that, you need to go to this part of the notebook and upload at the data set. You can upload it from uh uh your computer or from other sources. But you need first to do that in case uh that you want to to work with that. And for that you can have several options. You can set up as as a data source or you can save or you can read it directly from CSV. And you have to tell the cell which option do you want. So in this case I want that data to be read from CSV. I want to have it as a data frame and I can put here the name of the of the variable that I want to store that data. So when I run this uh the cell understand uh which file I want to read and converts that into a data frame. Okay. Uh so we can see here that we have like 2,000 rows uh that most of them uh there are 16 uh columns uh and some of them are numbers some of them are strings uh so you can see what I meant by uh mixed data right we have date here um and and with showing already the data we can see um what is like our data looks like and we can get an idea of what we should ask the AI to do and and what we should get from the AI. So the first thing the the the second thing that I will ask the AI to do is show me like the shape the data types a statistical summary of the patients. I check look that I already tell them in which data frame I want to uh the data to uh work with and now we only have way one data frame but when we advance in the notebook we are going to create uh different uh data frames we are going to manipulate them and store them in different variables so if I don't tell the AI which data is that I'm working with maybe it just picks another one maybe I manipulate the data maybe I feel a missing value and then uh the the AI doesn't understand that. So let's run this and see what we have. Okay. So uh let's check what the AI gave us. So you can see here that we have the shape uh the the D types which type of uh variables are there and statistical summary and also some valued counts. Why? Because for the numerical uh variables it's good to have like mean, max mean and uh mode but for the categorical various we need to check for other things. So we need to check how many observation are in each category. So as as I mentioned before we have 2,00 rows this 2,000 patients and 16 variables. You can uh see here the column names and also which type of data we have there. You can see we have integers, we have floats, we have object that they are a string. So those points out to be categories. We have a statical summary. Uh we can see the count, the unique values, the mean, uh the the maximum, the the the median, and the interquartile range. You can you can you can see there. So we can already start to to to see what the data looks like. For example, here uh we have the city and and okay, sorry about that. Uh you have uh the city and you can uh see for example for uh for Albany how many do you have? Uh anxiety we have 175. So we have the count values of of those ones. Okay. Uh now uh we can see that uh one of the one of the things that is important when uh we work with data is to have to have a look at the missing values. And that is why and because um machine learning doesn't handle well missing values. it doesn't know what to do. If we have a a new value, then the machine learning can't work with that. It we cannot make um operations on that. We cannot assign it to anything. So for the first thing or one of the most important things to look for is missing values. So I'm going to be very like vague here and tell them to look for missing values in the patient data frame. Okay. Uh so when uh I run this probably if you run it in a in a in a different notebook um you will see and and I have this maybe I can show it here. I already have another one prepared because the AI one of the things that has AI is like it learns from from what you already have done and in case that I already have this when you just tell them to look for the missing value the first thing if you run it in a new notebook then you will see that your code is different and you will see that uh you will have no missing value. So the AI founds no missing value. And why is that? Because if we look into the for example the city, we have like 1,000 uh rows that has unknown. And unknown is a string. It's a valid string. So the AI and pandas won't flag that as a missing value. But it's but uh it is uh a missing value right and also we can see uh in the let me check we can also see in the primary condition right that we have uh none as a string and we have almost 500 patients that has none as a string and in those cases they are valid strings but they are not flag as missing value. So when uh we the first time we check for missing values and we are being b the code produces no missing value but we know that they are missing values because we are seeing it and this is like the first thing uh that we really need to check for the output of the AI. So the here because also pandas doesn't flag this the AI is not fully understanding our data set. So what we are going to do is we are going to tell them like uh sorry uh okay we are going to tell them that the none and unknown should be considered missing values and that they should check again and now that it will that should replace those values with actual nump type nans and save that data in a variable that we call patients non df and let's see what the what the AI does. So now it creates a function uh that understand that none and unknown are these guys missing values. They are not blanks. They are not news in in the numpy um sense but they are missing value. we don't have information of them and now it just uh replace them by by new and then saves them in this data frame that is called patients than DF and why we want this uh we want this with because we need to handle them if we could leave it as unknown or we can leave it as none but it will won't give us an information when we are going to do a machine learning especially when we are going to interpreter uh those uh results we need to understand uh which string was there, which category was there and that is why we want to handle the missing value. So uh as I mentioned before, we have 1,000 uh values uh in city and we have almost 500 in primary condition. So 1,000 out of 2,00 is like almost 50% of um of data that is missing in city. And this is very difficult because we cannot interpolate 50% of the data right but also we have here the advantage that city is not an informative column for the clustering that we want to do. So in that case we are not considered city. So we are not handling that missing value but primary condition is an important one for our clustering because probably it can it can be a good feature to cluster patients together. It's not the same for a patients to have like a very severe primary condition that probably will go most time more times to the to the hospital than to have like a like a mild uh primary condition probably the visit will be less and if we are working with that with our hospital and we want to try to get for example those patients then they are different groups. So this is an important column and we need to handle the missing [clears throat] data and we need to tell the AI how to handle that missing data because there are several ways in which we can uh handle missing data. So what we are going to do well we know from before that when the primary condition was absent then it was none. So this can tell us um a little bit that probably those patients don't have a condition and that is why it was none or was missing value. So we're going to tell the AI okay take this data frame where I have the missing values I have missing values in the primary condition column. So I know that where the condition is present then there is always a a a name for the disease. So that means that you need to fill those missing values with no condition and then save them in another uh in another variable called patients nan replace and verify again that now we don't have any missing value. So the AI creates a a copy then fills all the nans in the primary condition with no condition and finally it checks and we can see that there are no missing values after the replacement and now we have the other one that is called no condition and this is more informative for us. This is more informative for when we check the cluster and we understand the data. Okay. Now uh we have handle handled the data and we want to start to uh take a more closer look and how each variable looks like. So the first thing that we are going to do is we are going to do a numerical data and categorical data and ask the AI to create histograms to so we can see the distribution and to create count plot for the categorical values. And when we do that, we cannot create a histogram for a categorical values, right? It's not a continuous number. So we need to to see other ways uh to understand um that data. We tell them which data frame we want to uh use. We tell them which library to use and also to set a palette that we want. This is like uh at your what you like. uh but for example in this case I want to set that pallet I tell the AI and let's see what we have okay so here we have the histograms that the produces we need to check both two things we first we need to check that the I did a good job so that we really don't see anything that uh is odd and uh we also need to check what our data is telling So we can see that uh age is u almost normally distributed the same that height the same that uh like weight is uh almost uniform uh distributed uh we can see uh the number of chronic conditions they are more like discrete you can see that you you have zero you have one you have two or three uh number of chronic conditions we can also see the annual visits that go from zero to 12. Uh we see the day seen last visit. Uh we and then the prevented care flag which probably is the one that could be used uh in the case that we are doing like a regression or or or a classification in this case is not something that that we want. And we we want and also we check the average billing amount and we see that it has a skew, right? And then we check for uh the categorical variables. We see that we have a balance. Uh this is important for the categorical ones. We have we have a balance in the gender. Um we can see the insurance type. Most of them are in the Medicare. Some of them are private and uh there are a few with self-pay. And uh lastly we can see that most of the conditions especially the primary conditions are um the patient don't have a condition and then we see some related to hypertension obesity and uh some related to anxiety arthritis depression and uh on on on the more right one we can see a heart disease and and a respiratory disease. So now we when we work with numerical data it's also important to check the correlation and what is the correlation how two variables associated with each one. If two variables are correlated it means like if one goes high then the other one probably can go high or lower. So they the moving one variable also has an impact in another variable and this is sometimes depends on the algorithm that we will work. This is not desirable. Why? because sometimes we see effects that they are related to one variable but we see also in on the other variable and it's not because that variable is uh having an impact in in in in my target variable in the case that I will be doing uh regression but so maybe here we have like one feature that is very important for clustering and we have another feature that doesn't make sense when we analyze it but also looks like it's important and that is why they are correlated So we really need to check for correlation. So we're going to tell the AI to create a correlation heat map. We are going to tell them to only use the numeric columns. We are we tell them which one which data frame to use. And in this case uh we we also are going to use zip and we use the cold warm pallet. We also tell them to annotate the heat map so we can see the correlation values and the correlation. The heat map and sometimes has a a square and we only want to see the lower triangle because it is more visual. Okay, we ask the AI. It only includes the numeric. You can see here that selects only the numeric ones. It just performs the correlation and then mask to only have the triangle. And here it show us like the correlation in in the bar. You can see that the more red it means like that is positive correlated and when it's blue then it's negative correlated. So uh we can see here that for example age is very highly correlated with the number of chronic condition which is makes sense usually older people tends to have more chronic disease. [snorts] We can see here for example the uh BMI is highly correlated with weight. That makes sense because BMI is is um calculated from the weight and the height. So it's not this these two variables is is not something that we want to input together. As I mentioned probably we will choose only one. You can see that here for example we have a set of uh correlated or malcorrelated ones like the average bill amount is uh somehow correlated with the number of conditions. This makes sense because if you have more chronic conditions probably you will move to to the hospital. Uh also the annual visit is m correlated with age. This also makes sense. So why we are we are doing this analysis because even we ask the AI to code us this is also important to check that it makes sense so that the AI and the results are important and that we also know what is the the the condition of our data set before we even ask to do a clustering. Okay. So we have seen now that we have some numerical um uh variables some categorical variables. We also seen that there are some variables on numerical barriers that they are correlated. So let's take a first attempt and ask the AI to cluster to find groups and patterns in this patients with a clustering algorithm. So what I will going to do it I will ask the to use the patients nonreplace data data frame uh to se to select some numerical variables. Yes, we are not going to for example select a weight and height because we already see that they are highly correlated with FMAI. And then for the categorical values I will use gender ensure I type and primary condition. I will tell them to save this in a new data frame that is called final patients df. And let's see what the AI does. Okay. Uh it it selected the ones that that we told them. So that is correct. And it show us the final data set that it will say in the final patient DF. Now I will tell the AI using that data frame where that with the selected features cluster the patients into meaningful segments. I will tell them to use the most suitable method to to choose the K case. how many clusters uh do we want when we find as I mentioned that we don't tell the algorithm which features are important we don't tell them which number of group we want we just let the algorithm find patterns and understand how many groups we have there but there are ways that we can find what is the uh best number of groups and that is why I tell like choose the most suitable method for uh finding this case, finding this number of groups. And uh I this is I make like a little bit of of um I cheat like a little bit because I already run this and I know that the has an error and and in the one hunt encoder it fails uh because uh it one hyperparameter is deprecated. So I already told it to set that um that hyperparameter to false instead of using the the one that it was using. Okay, let's see what the AI has to say. Okay, so one thing here, let's see what the AI gave us. First of all, it imports standard scaler and one hot encoder. And why is that? Well, standard scaler is as I mentioned in the slides, we need to scale the data. Why? Ages go from zero to 100. Billing go from zero to thousands. If we take them as they are and we don't consider these things probably the billing will have a much higher impact in the in the in the costing or maybe the age moving from zero to one uh will um will uh have a small impact or for example from uh moving from 20 to 50 is only 30 uh in in in absolute numbers and it it will look like there is no such a huge impact for the clustering but we know that age is not the same to have 20 that to have 50 and in the billing for example is different why because it's not the same is if if I I am charged $20 or $50 is almost on the same range the difference will be if I'm charged 20 that if I'm charged 1,000 so you can see there that scales are different and the algorithm doesn't know so we need to tell we need to have a way to scale them and uh scale the impact of those features in the clustering and that is why the AI input the standard scaler one hot encoder as I mentioned the categorical features need to be um converted into something that the algorithm understand and is numbers. So what uh the one code then does is like for example for the categories if you have like um the insurance type you have prepaid or you have a self-pay then it will create one column for it category and put a zero if that patient doesn't have that uh insurance type or one if it does. So uh it clearly identified which are the number uh the numerical features the categorical features and uh then creates a preprocessor where it first scale the the numerical features and then one hog encode uh the categorical ones and as I mentioned the first choice of the AI is K means and that makes completely sense uh because it could be the to go cluster algorithm for any new data data scientist. Why? Because the key means is fast. It works well in a lot is simple in a lot of process. But here we know that we have mixed data. So we have asked the the AI to to what to understand right that probably it will fail. So now uh what I will want to understand is like sorry uh now I will uh want to understand how those cluster looks like and for understanding this uh we should be able to read this uh method for um for finding the optimal k. So what this graph tell us is like in the x uh axis we have the number of clusters and the in the yaxis we have how different or or h how different the points that they are in the same group are. So when you see that that inertia or within cluster sum of squares starts to fall it means like each cluster has point that look like very similar and we have a point where it does like an elbow and in that point is where the optimal K is found. So I will ask the AI now to fit a come in with the optimal K that it found and plot how the uh how the the the clusters looks like. Okay, this is what the AI gave me. You can see here it found the number three as the optimal clusters. If we can see here, we can see that this one with this two they are different. This is only 2D. So maybe we see in 3D uh we have like a better approach. Uh but you can see here that there are some they some points that they are a mix. So they are not very different uh cluster zero from cluster two. So maybe we can optimize this. And also we can see that if we see here, yes, it found this three as the optimal K. We could also argue that it's four, but the AI just find three as the number of optimal case. Okay. Now I will go ahead and ask the AI to do something uh extra is to understand what is the feature importance and what is the feature importance is like how important each feature was in predicting the clusters in building those clusters and why that is important because it tell us um which feature to consider or uh it tell us how the the patients that they are in those cluster look like and okay so uh here's what the AI gave us you can see that there are really different uh feature importance the higher this number the more important that those features are probably billing amount uh take all the credit and then it age but it's very different and then the other numerical features So we can see here that there is something odd right I already told you this I already told you the cannons is not the best for this problem but if we if it was the same first time that we are doing this we need to get all the knowledge that we have on clustering on the healthcare patients on on on machine learning to understand okay this is something weird this is not uh one of the feature should not be the one taking all the credit for the cluster I cannot cluster or I can differentiate uh in one feature But it's not the most usual one. Probably it's a combination of features. So here is like we have a t Okay, everyone. Looks like we may have lost just for a moment. So, we'll be back very shortly uh once this is resolved. I don't know if you can hear me, but please let me know in the if in the chat if this is frozen for you. [sighs and gasps] [snorts] Okay. Yes, we are all having the same issue. Um, I will be back very shortly. I need to uh have a look into this issue, but hold tight everyone. This will be fixed in the recording. We will uh we'll trim this out. Um, but yeah, give me one second. Okay, welcome back. >> Yes, we just lost you. >> I saw a message that says like um reconnecting and I was like what is happening? >> Yes. Um we didn't we didn't lose you for too long. It was about 30 seconds or so. So >> Okay. Okay. >> Yeah, just a little rewind. >> Okay. Thanks. Thanks. And sorry. Sorry for that. It was a >> Okay. So I was saying like uh one feature is taking all the credit. So we can know uh that this uh Cummings uh is not doing well. And why is that? We already know we discussed this uh during the webinar several time is because um we have mixed data and for mixed data we need other type of distance. we need other type of algorithm that handle the categorical um data well and that is where go distance uh came in handy. So uh we are going to tell the AI that the Tins approach didn't work because we have a mixed uh numeric and categorical features data type. Also the one hot encoding inflicting the dimensionality of a of our um data set. we uh pass from having probably eight columns to having like 30 columns because one of each category is now a new column. So we are going to use goward distance instead. So we are going to tell them to tell the AI to use the go library if installed is if necessary and uh perform that calculation first. It's okay. So what it the AI does is like first it install the library in case that is not installed and then it's just a very simple uh calculation. It just uses the Gower library to calculate the G matrix and has this G distance matrix variable saved to now use that distances to a new clustering algorithm that is called alomerative clustering. The elomerative clustering what it does is instead of the cumins that uh it finds um only like uh it it it goes and find like the distance between all the points and and the centers and uh check in which cluster each uh data belongs. It start from uh the most distant one and starts to construct which patients looks like. So we are going to tell them to use this clustering. Uh we are going to tell them that use the distant mastric that we already calculate and to use a silhouette score that is the other method to find the best C um clusters to uh now have a a cluster. Okay. So let's go ahead and check what the AI has done. So uh the difference is like uh now we imported this alomerative clustering. Uh it uses silhouette score fits the the alomerative clustering between two and nine clusters and trying to find what is the best number of clusters and it found like six. Remember that the Cummings only found three clusters. And if we are taking all the the features that we have, we and and all the patients that we have, it makes more sense that we have six type of uh patients instead of only three. Now we are going to visualize those uh We are going to visualize uh those um clusters and we are going to see a different graph here for the cumins. We just saw like clouds right like you can see the different groups for the alomerative clusterings. We can see how the different patients are close together and each color indicates a cluster. So we have and and then the the the lines it indicates how many uh what is the distance between those patients. So you can see you can find groups. You can even go uh further and and find more groups. Right? But in in in the in the dendagramgram that we have here we can see that the orange is one group, the pink is another uh another group. The lilac is another group. the green uh yellow and and you can see there the different clusters and also we have a now the uh the feature importance or the or the characteristic of each cluster right we have six clusters and what this charts tell us is like what is the average patient in one of those uh clusters so uh we have six and I we have for example So if we take the the first cluster we can see that the age of that person is 51. Uh the BMA is 30. The number of chronic condition is one and the annual v visits is six. So you can check uh column feature by feature how those cluster looks like for the numerical and also for uh the categorical ones. You can see here the insurance type for example for each cluster which type of uh of insurance uh the patients have and which type of primary conditions. And now we are going to ask the AI okay to use the numeric uh columns and create box plot to understand the same that we did before but now with a graph is is is more uh visual to to create a graph. We wait. Let's see what is happening here. I think that uh it's called dresser summary probably. I can show you. Um in this one here we have right the graph. Uh so we have the feature distribution per cluster look like the edge uh cluster three for example it has like a range between uh 20 and 40. uh the the cluster zero has the the patient distributed between 40 and 60. So you can see more visual how each cluster um how each cluster um looks like, right? And finally uh we can ask also the AI and you can see here in in this prompt um to uh give us like a summary, right? So we have it here. So you can check cluster by cluster what is the average or what is like the the more representative patient of each cluster. That is why we create the cluster. The cluster when we then understand what the cluster um means. We need to understand where is the patient that we are going to target. For example, if we are going to send a notification, imagine that we have an app and uh that we need to understand how our patients look like to understand which patient we should send a message saying like you have a appointment tomorrow but we want to optimize resources. We know that there are certain type of patient that probably will remember and we know that they are uh another other patients that probably are non show. So we have to identify them and we have to see how they look like how the the the representative patient look like in that in that cluster and that is why it's nice to see at the summary is nice to see and how that cluster looks like and uh finally um there is uh other things that we can do uh with the cluster. So imagine that uh we have already have this app uh ongoing. We have this clustering algorithm ongoing in production. If you are working in a data science company and what happens if a new patient comes that is not in this data set that we train the models. Uh should we run every time that the patient joins our our our uh company or that we have a new patient run this to understanding which cluster it is? Well, it's not necessary. We can use the cluster, we can use the models that we already have to predict in which cluster uh each patient will fall. And what I did uh last uh is to ask the AI to predict a patient that I made up. So I I told the AI I want to predict uh which cluster a brand new patient uh belongs to. And it should take you should write the function that takes a dictionary. The dictionary will have the features that I already know that made up the cluster. And then it should calculate the go distance. Uh and then it should compute in which cluster that patient belongs to. And I gave it like a 55 years old female. It has Medicare. It has a hypertension. It has a a BMI of 32. It had two coronic conditions. six annual visit and an average bill of 3,500 and it was like 120 days since the last visit and they create me a function. Yes. So you can see that it has the function with the go distance when it takes the new patient it takes the the matrix before that we already have calculated and now it predicts the cluster and it tells me in which cluster that patient uh falls into cluster number four. So imagine that I I run this data science company. Now I know that this cluster because I already saw it before this cluster is patients that really are an address of no show. Okay, I will send an email or or a message to this new patient telling uh you have appointment tomorrow. You should um do certain things and that that is uh where the cluster um are important. So uh to sum up and and what I wanted you to take away from this is like uh first uh you always uh should explore your data. Uh the second uh the AI assisted coding really assert your workflow. It really helps. It really can uh give you new ways of seeing but you always have to be in the loop. You have to check uh what they told you. You have to check that it's doing a correct job. You have to apply your domain knowledge to understand if the solution that they gave you is correct for the problem that you have. That is irreplaceable. So you cannot replace uh that uh and then for the clustering of course the the algorithm matters. So uh uh there is uh there is also the domain knowledge where you should choose and then you should to tell the AI you are not applying correct algorithm for this problem. and uh that you need to interpret uh the results of the machine learning um output. It's not that just doing a model. It's also understanding what do you are doing there. Again the domain knowledge you can tell the AI to code for you. You can tell the AI to produce the code for you but then the understanding has to be done by you. You have to understand and know how to apply uh that knowledge. Uh so thanks uh again and Reys if you are >> perfect. Thank you so much again. That was uh that was fantastic and lots of good comments from the chat as well. So yeah, keep them coming guys. It really does make a difference in these sessions. Um we've got a couple of questions that we'll run through. So I'll just change the layout ever so slightly. >> Yeah. >> So thank you everyone who asked a question. Um so we had a a recent one in from Eduardo. So Eduardo says, "Uh, great session, very clear methodology. From your experience, what's the biggest challenge in turning these analytical models into real decisions with organizations?" >> Uh, great question. I think that that that's a big challenge and um it has to do uh not I from my experience of course this is not like a the correct answer but it has to do uh not only from the technical you can be uh very good at at creating a model but um I think that uh the decision making has to agree with with your methodology uh or not with the methodology but the results that you have and uh you can create a very accurate model but if it doesn't have a result if it doesn't have an impact then it doesn't make sense so I think that uh turning really is more like the again the domain knowledge not so much the code or the model that you use is more like to understand the impact that model will have and selling to the people who make the decisions that that model really will help the organization or or the business >> got it >> that's a great question. [laughter] >> Um, yeah, cutting right through it. Uh, so we had another one from Chlo Alligator. They say, "How do we make sure that the AI didn't make a mistake when manipulating the data set?" Um, we know AI makes mistakes, but working with big data, you can't necessarily double check manually. So, yeah, what what are the concerns when uh prompting with AI and using AI generated code? >> Yeah, that's a good question. I don't know if you have an answer for that. [laughter] uh but uh yeah I think that yeah it's true you cannot check uh manually probably you can put in place some flags uh to understand more like the output or as I mentioned like looking for a graph or for uh results of that um of that uh AI uh prompt uh to monitor that in the data to to have certain flags that you know if that is happening probably the AI is not manipulating correctly the the the data. Uh but yeah, in in a smaller data set is easier because you can check for example we we did the the missing values and I already knew because you can say it but in big data is harder. So you have to put I think some other um systems in place uh for that. >> Uh so we had this one early on from Emilia. They say, um, I'm not worried about the AI writing code because I can check it. Uh, but when, uh, they're learning, it feels like you're only getting the obvious options for how you would solve a problem and not the more kind of creative solutions. So, yeah, when you want to think about sort of solving these problems creatively and you're only getting the the obvious answers, do you have any workarounds for that or? >> Usually, man, um, my workflow doesn't start with AI. Usually it starts with reading and researching which models are better for the problem that I have. Uh so the as I mentioned can help you materialize that idea that you have but I think that the innovation part should always start with with the person. You should always read, research, understand what is the domain knowledge, the data and which type of model can be applied, what other people did in in that domain and then you can ask the AI to check or to write the code but you need to guide it. You need to assist the I mean the AI assist you but you have to guide it because if not they will give you
Original Description
Maria Eugenia Inzaugarat, a Senior Data Scientist at Firstleaf, will guide you step by step through analyzing patient insurance metrics and behaviour, and training a clustering model using common Python analytics libraries. You’ll practice writing clear, high-quality prompts to generate Python code, learn how to interpret and validate AI-generated responses, and strengthen your ability to read and understand real analytical workflows.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: ML Pipelines
View skill →Related Reads
📰
📰
📰
📰
Scraping Indian government open data in 2026: what actually works
Dev.to · Manideep Chittineni
2026 Enterprise Data Recovery: What IT Teams Should Look For
Medium · AI
I Was Mad About the Jaylen Brown Trade. So I Simulated 10,000 Seasons to See If I Should Be.
Medium · Data Science
Mosaic Plots in Data Visualization: Turning Complex Relationships into Clear Business Insights
Medium · Data Science
🎓
Tutor Explanation
DeepCamp AI