Skip to content

Commit ba5cdd1

Browse files
committed
Converting the logistic regression Scikit-learn model trained to the Core ML Format.
1 parent 948793d commit ba5cdd1

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Author/ Learner: Nguyen Truong Thinh
2+
# Contact me: [email protected] || +84393280504
3+
#
4+
# Use case: Create a Logistic Regression model that can be used to convert into
5+
# the Core ML Format via CoreML Tool .
6+
# The model will be trained & converted on the popular UCI ML Pima Indians Diabetes dataset.
7+
8+
import coremltools
9+
10+
from usecases.tabular_classifier.logistic_regression.logistic_regression_classifier import trained_model
11+
12+
coreml_model = coremltools.converters.sklearn.convert(trained_model, ['Pregnancies', 'Glucose',
13+
'BloodPressure', 'SkinThickness', 'Insulin',
14+
'BMI', 'DiabetesPedigreeFunction', 'Age'],
15+
'Outcome')
16+
17+
coreml_model.author = 'Nguyen Truong Thinh'
18+
coreml_model.short_description = 'A logistic regression model trained on the Kaggle.com version of th Pima Indians ' \
19+
'diabetes dataset.'
20+
# Features description
21+
coreml_model.input_description['Pregnancies'] = 'Number of pregnancies.'
22+
coreml_model.input_description['Glucose'] = 'Plasma glucose concentration after 2 hours in an oral glucose tolerance ' \
23+
'test.'
24+
coreml_model.input_description['BloodPressure'] = 'Diastolic blood pressure.'
25+
coreml_model.input_description['SkinThickness'] = 'Thickness of the triceps skin folds.'
26+
coreml_model.input_description['BMI'] = 'Body mass index.'
27+
coreml_model.input_description[
28+
'DiabetesPedigreeFunction'] = 'A function that determines the risk of diabetes based on family history.'
29+
coreml_model.input_description['Age'] = 'The age of the subject.'
30+
# Description of target variable
31+
coreml_model.output_description['Outcome'] = 'A binary value, 1 indicates the patient has type-2 diabetes.'
32+
coreml_model.save('diabetes_indian.mlpackage')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"fileFormatVersion": "1.0.0",
3+
"itemInfoEntries": {
4+
"F2D67E27-AF2E-4D54-89B0-9990C4DF7371": {
5+
"author": "com.apple.CoreML",
6+
"description": "CoreML Model Specification",
7+
"name": "model.mlmodel",
8+
"path": "com.apple.CoreML/model.mlmodel"
9+
}
10+
},
11+
"rootModelIdentifier": "F2D67E27-AF2E-4D54-89B0-9990C4DF7371"
12+
}

usecases/tabular_classifier/logistic_regression/logistic_regression_classifier.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
# Train a logistic regression model
5050
model = LogisticRegression(penalty='l2', fit_intercept=True, solver="liblinear", multi_class='ovr')
51-
trained_model = model.fit(df_diabetes_features_train, df_diabetes_target_train.values.ravel())
51+
trained_model_kfcv = model.fit(df_diabetes_features_train, df_diabetes_target_train.values.ravel())
5252

5353
# Get predictions
5454
predictions = model.predict(df_diabetes_features_test)

0 commit comments

Comments
 (0)