Disease Prediction using Decisional Trees

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vmishra1
    New Member
    • Oct 2014
    • 6

    #1

    Disease Prediction using Decisional Trees

    I have made cancer prediction based on symptoms using decision trees but i am not able to run my code

    Please Help ME!! Thanks in Advance!!!

    Code:
    #include<iostream>
    using namespace std;
    #include<conio.h>
    #include<string.h>
    
    struct dectree
    {
    	char symptom[50];
    	
    	struct disease
    	{
    		char disea[50];
    	}dis[20];
    }arr[40];
    
    void datainsert()
    {
    strcpy(arr[0].symptom,"Uneasiness");
    strcpy(arr[0].dis[0].disea,"Pancreas");
    strcpy(arr[0].dis[1].disea,"Stomach");
    strcpy(arr[0].dis[2].disea,"Esophagus");
    strcpy(arr[0].dis[3].disea,"Kidney");
    strcpy(arr[0].dis[4].disea,"Lung");
    strcpy(arr[0].dis[5].disea,"Bone");
    strcpy(arr[0].dis[6].disea,"Brain");
    strcpy(arr[0].dis[7].disea,"Leukmia");
    strcpy(arr[0].dis[8].disea,"Colon");
    
    strcpy(arr[1].symptom,"Unexplained Weight Loss");
    strcpy(arr[1].dis[0].disea,"Pancreas");
    strcpy(arr[1].dis[1].disea,"Stomach");
    strcpy(arr[1].dis[2].disea,"Esophagus");
    strcpy(arr[1].dis[3].disea,"Kidney");
    strcpy(arr[1].dis[4].disea,"Lung");
    
    strcpy(arr[2].symptom,"Fatigue");
    strcpy(arr[2].dis[0].disea,"Bone");
    strcpy(arr[2].dis[1].disea,"Brain");
    strcpy(arr[2].dis[2].disea,"Leukmia");
    strcpy(arr[2].dis[3].disea,"Colon");
    
    strcpy(arr[3].symptom,"Stomach Ache");
    strcpy(arr[3].dis[0].disea,"Pancreas");
    strcpy(arr[3].dis[1].disea,"Stomach");
    strcpy(arr[3].dis[2].disea,"Esophagus");
    strcpy(arr[3].dis[3].disea,"Kidney");
    
    strcpy(arr[4].symptom,"Chronic Cough");
    strcpy(arr[4].dis[0].disea,"Lung");
    
    strcpy(arr[5].symptom,"Changing Bowel Habits");
    strcpy(arr[5].dis[0].disea,"Colon");
    
    strcpy(arr[6].symptom,"Pain");
    strcpy(arr[6].dis[0].disea,"Bone");
    strcpy(arr[6].dis[1].disea,"Leukmia");
    strcpy(arr[6].dis[2].disea,"Brain");
    
    strcpy(arr[7].symptom,"Hypertension");
    strcpy(arr[7].dis[0].disea,"Pancreas");
    strcpy(arr[7].dis[1].disea,"Kidney");
    
    strcpy(arr[8].symptom,"Swallowing Difficulty");
    strcpy(arr[8].dis[0].disea,"Stomach");
    strcpy(arr[8].dis[1].disea,"Esophagus");
    
    strcpy(arr[13].symptom,"Head Ache");
    strcpy(arr[13].dis[0].disea,"Brain");
    
    strcpy(arr[14].symptom,"Body Ache");
    strcpy(arr[14].dis[0].disea,"Bone");
    strcpy(arr[14].dis[1].disea,"Leukmia");
    
    strcpy(arr[15].symptom,"Change in urine");
    strcpy(arr[15].dis[0].disea,"Kidney");
    
    strcpy(arr[17].symptom,"Heartburn");
    strcpy(arr[17].dis[0].disea,"Esophagus");
    
    strcpy(arr[29].symptom,"Physical Intolerance");
    strcpy(arr[29].dis[0].disea,"Leukmia");
    
    strcpy(arr[30].symptom,"Bone swelling");
    strcpy(arr[30].dis[0].disea,"Bone");
    	
    }
    void result(int n)
    {
    	cout<<"Based on the following symptom you have chances of following CANCERS : \n";
    	
    	for(int i=0;i<9;i++)
    	{
    		cout<<arr[n].dis[i].disea<<"\t";
    	}
    	cout<<endl;
    }
    void search()
    {
    int	i=0;
    int count=1;
    int root=i;	
    int lchild;
    int rchild;
    char syn[40];
    int queue[20];
    int k=1;
    
    lchild=2*root+1;
    rchild=2*root+2;
    
    
    while(arr[lchild].symptom!= NULL && arr[rchild].symptom!= NULL)
    {
    	
    cout<<"Enter the symptom "<<"\t";
    cin.getline(syn,40);
    
    queue[count]=lchild;
    count++;
    queue[count]=rchild;
    count++;
    
    
    if(strcmp(arr[root].symptom,syn)==0)
    {
     root=queue[k];
     lchild=2*root+1;
     rchild=2*root+2;
     k++; 
    	
    }
    else if( queue[k]!=NULL)
    {
    	root=queue[k];
     	k++;	
    	if(strcmp(arr[root].symptom,syn)==0)
    {
     root=queue[k];
     lchild=2*root+1;
     rchild=2*root+2;
     k++; 
    	
    }
    }
    else
    {
    	break;
    }
    }
    
    result(root);
    	
    }
    main()
    {
    datainsert();
    search();
    getch();	
    }
    Last edited by vmishra1; Oct 19 '14, 04:58 AM. Reason: Putting up the entire code for better understanding
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Is there a reason you are using C++ but are attempting to code in C ?

    You should be using be using C++ string objects, classes and a map container. That avoids all of the tree code.

    If you are writing in C then use the C #includes and the scanf/printf functions.

    If you continue your way, then use your debugger to step through the code.

    C++ might kind of look like:
    Code:
    class Disease
    {
       private:
         string diseaseName;
    };
    
    class Symptom
    {
        private:
           vector<Disease> dis;
    };
    Your database is:

    Code:
    map<string, Symptom> > database;
    This data model assumes a key symptom which is looked up in the map container. The Symptom object has an embedded array of Disease objects which are the diseases associated with the symptom. All of the lookup tree code is done in the map container which is a C++ Standard Library object.

    Comment

    • vmishra1
      New Member
      • Oct 2014
      • 6

      #3
      Thank You!

      What i am doing is to incorporate both of them for the simplicity of the program.

      I have found that my loop is not ending properly.. If you could please see that and give your input/rectification about the traversal or the search part.

      If you have time..Please try running my code then you can see the error i am facing

      Thank You!!

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        You mention run-time problems. Are you getting any compiler warnings?

        Line 113 checks if the symptom field is NULL, but symptom is a char array not a char pointer. I'm surprised you didn't get a compiler warning for this. Likewise for other NULL test on line 133.

        Can you describe in words the intent behind your search() function? Variable names lchild and rchild suggest a binary tree; but the arr array does not look a tree.

        Comment

        • vmishra1
          New Member
          • Oct 2014
          • 6

          #5
          Thank You!!!

          The compiler is not showing any warning but the loop is running infinitely.

          The purpose behind this is the prediction of the disease based on symptom and yes its a binary tree made up using arrays. If you observe the array positions are such that it will form a tree.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            If you observe the array positions are such that it will form a tree.

            Array positions are not usually used for trees.

            Trees are made up of nodes where the node contains a pointer to keys larger than the search key and a pointer to the keys smaller then the search key and a pointer to the data value if the search key is equal. In the case of 1-2-3 trees there may be even more pointers.

            Have you implemented a tree structure out of a book? If so, you can use the book example to debug your code. If not, you may want to find a tree structure you are comfortable with and implement that.

            I would advise against placing values in known positions. You will be better off using addresses so you can place data indirectly thereby removing the requirement for a data structure. Instead your structure is defined by following an address to a node.

            Comment

            • vmishra1
              New Member
              • Oct 2014
              • 6

              #7
              Thank you !

              I haven't gone through the book structure

              I want to implement the tree structure without nodes just using the array positions

              In case if you have run the code you would have found that the loop runs infinitely and everytime it asks for symptoms

              The thing that I want to end the loop conditionally an I am not able to run that

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Each dectree structure associates one symptom with up to 20 diseases; arr is an array of 40 of these structures. Please explain the intended logic of the search function.
                • Do you intend to merely search the array for the first matching symptom and then you're done?
                • Do you intend to dance through the array in some disease-specific order?

                You say the array is organized as a tree, but I don't understand what that means. A key attribute of a tree is the notion of a link to the next item (next right; next left). For example, what is the next item on the right of arr[7] (hypertension)? Which array entry is the root of the tree?

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Traditionally, a decision tree is a structure where you start with a question; and each possible answer to that question either leads to another decision tree (another question) or to a final decision. How you answer the questions determines your path through the structure. The path is typically different each time.

                  What mechanism do you use to vary your path through your decision tree?

                  Comment

                  • vmishra1
                    New Member
                    • Oct 2014
                    • 6

                    #10
                    Thank You!!!

                    Ok ...so if you want to make a tree out of the array positions I have given then do 2i+1 and 2i+2

                    For example lets take the hypertension its at array 7 position so now to find out its root you will do (I-1)/2 ie 7-1=6 -> 6/2 = 3 now see array 3 ,...you will find stomach ache ... And see if you don't find the correspondent number like you are searching for that means its not there in the list(ie the node doesn't have Xchild)

                    See what I want to do is that first if a person has uneasiness then I will ask further symptom and each symptom is associated with some amount of cancer ...finally I come to a specific point where a person stops...you can make the tree with the above formulas

                    Thanks !!!

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #11
                      Your loop terminates because either
                      • The loop condition on line 113 is satisfied.
                      • You reach the break instruction on line 148: root symptom is not the entered symptom (line 125) and queue[k]==NULL (line 133).

                      As I pointed out in my first reply, none of your comparisons to NULL will do what you expect. This is likely why your loop doesn't terminate.

                      By the way, the lchild and rchild values of "Physical intolerance" and "Bone swelling" will be larger than the size of your array. You should trap these out-of-range values before using them to access the array. Accessing past the end of an array is Undefined Behavior.

                      Comment

                      • vmishra1
                        New Member
                        • Oct 2014
                        • 6

                        #12
                        Thank You!!!

                        The problem with the code was that lchild and rchild were such that it wasn't getting to null

                        and about out of range it won't give error as the values are not there that means its null

                        I would like to share that I have successfully run the code after rectifying the traversal mistake

                        BIG THANK YOU TO ALL WHO HELPED !!

                        Mission accomplished !!!

                        Comment

                        Working...