-
Notifications
You must be signed in to change notification settings - Fork 113
Added beginner friendly example of diabetes dataset #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aiwithqasim
wants to merge
6
commits into
tensorflow:main
Choose a base branch
from
aiwithqasim:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
593df6a
added diabetes dataset for beginner usage example
aiwithqasim f63f475
beginner friendly example of TFDF
aiwithqasim 693d528
added proper README of examples
aiwithqasim 7776735
Merge pull request #1 from aiwithqasim/work
aiwithqasim 2e99f73
shorten the description of "beginner_diabetes"
aiwithqasim 096173b
updated the suggested possible improvements
aiwithqasim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
### TensorFlow Decision Forests (TF-DF) | ||
|
||
|
||
| Tutorial | Description | | ||
|---------------------------------------------|-------------------------------------------------------------------------------------------------------------| | ||
| [minimal](./minimal.py) | display and evaluate a Random Forest model on the adult dataset | | ||
| [beginner_diabetes](./beginner_diabetes.py) | Beginner-friendly usage example of TensorFlow Decision Forests (TF-DF) using pima India's Diabetes dataset | | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Copyright 2022 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Beginner-friendly usage example of TensorFlow Decision Forests (TF-DF). | ||
|
||
This example trains, display and evaluate a Random Forest model on the pima India's Diabetes dataset | ||
aiwithqasim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This example works with the pip package. | ||
|
||
Usage example (in a shell): | ||
|
||
pip3 install tensorflow_decision_forests | ||
python3 beginner_diabetes.py | ||
|
||
More examples are available in the documentation's colabs. | ||
""" | ||
|
||
"""About | ||
|
||
TensorFlow Decision Forests (TF-DF) is a collection of state-of-the-art algorithms for the training, | ||
serving and interpretation of Decision Forest models. The library is a collection of Keras models | ||
and supports classification, regression and ranking. | ||
for more details [link](https://pypi.org/project/tensorflow-decision-forests/) | ||
""" | ||
|
||
# Installing the tensorflow_decision_forests | ||
# NOTE: Uncomment the below command If you don't have tensorflow_decision_forests package | ||
aiwithqasim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# !pip install tensorflow_decision_forests | ||
|
||
# Python libraries | ||
# Classic,data manipulation and linear algebra | ||
import pandas as pd | ||
import numpy as np | ||
|
||
# Data processing, metrics and modeling | ||
import tensorflow_decision_forests as tfdf | ||
|
||
# Check the current version of TensorFlow Decision Forests | ||
print("Found TF-DF v" + tfdf.__version__) | ||
|
||
"""# NOTE: | ||
aiwithqasim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This notebook is to train the same model that I had trained in July 2021 using Decision Tree algorithm | ||
[Pima Indians Diabetes - EDA & Prediction](https://www.kaggle.com/code/qasimhassan/eda-decision-tree) | ||
but now in this notebook will all about how to use **TensorFlow Decision Forests (TF-DF**). | ||
You can access the dataset from this [link](https://www.kaggle.com/code/qasimhassan/eda-decision-tree/data) | ||
""" | ||
|
||
# loading dataset | ||
pima = pd.read_csv(".datasets/diabetes.csv") | ||
|
||
pima.head() | ||
|
||
#selecting the important features and target variable | ||
feature_cols = ['Insulin', 'BMI', 'Age','Glucose','BloodPressure','DiabetesPedigreeFunction', 'Outcome'] | ||
dataset_df = pima[feature_cols] | ||
|
||
# Split the dataset into a training and a testing dataset into 70-30 ratio. | ||
test_indices = np.random.rand(len(dataset_df)) < 0.30 | ||
test_ds_pd = dataset_df[test_indices] | ||
train_ds_pd = dataset_df[~test_indices] | ||
print(f"{len(train_ds_pd)} examples in training" | ||
f", {len(test_ds_pd)} examples for testing.") | ||
|
||
# Converts a Pandas dataset into a tensorflow dataset | ||
train_ds = tfdf.keras.pd_dataframe_to_tf_dataset(train_ds_pd, label="Outcome") | ||
test_ds = tfdf.keras.pd_dataframe_to_tf_dataset(test_ds_pd, label="Outcome") | ||
|
||
# Trains the model. | ||
model = tfdf.keras.RandomForestModel(verbose=2) | ||
model.fit(x=train_ds) | ||
|
||
# Summary of the model structure. | ||
model.summary() | ||
|
||
# Evaluate the model on the validation dataset. | ||
model.compile(metrics=["accuracy"]) | ||
evaluation = model.evaluate(test_ds) | ||
|
||
# Export the model to the SavedModel format for later re-use e.g. TensorFlow | ||
# Serving. | ||
model.save("/temp/my_saved_model") | ||
|
||
# Look at the feature importances. | ||
model.make_inspector().variable_importances() | ||
|
||
"""Comparison | ||
|
||
When I had used Simple Decision Tree from sklearn after optimizing the final testing accuracy that I got | ||
aiwithqasim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
was 77% (reference: [check link](https://www.kaggle.com/code/qasimhassan/eda-decision-tree)) | ||
but by using TensorFlow Decision Forests (TF-DF) I got the testing accuracy of about 81%. | ||
""" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.