반응형
# https://machinelearningmastery.com/save-load-machine-learning-models-python-scikit-learn/
# Save Model Using Pickle
import pandas
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
import pickle
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv"
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
dataframe = pandas.read_csv(url, names=names)
array = dataframe.values
X = array[:,0:8]
Y = array[:,8]
test_size = 0.33
seed = 7
X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size=test_size, random_state=seed)
# Fit the model on training set
model = LogisticRegression(max_iter=1000)
model.fit(X_train, Y_train)
# save the model to disk
#filename = 'finalized_model.sav'
#pickle.dump(model, open(filename, 'wb'))
# some time later...
# load the model from disk
#loaded_model = pickle.load(open(filename, 'rb'))
#result = loaded_model.score(X_test, Y_test)
#print(result)
from sklearn.linear_model import LogisticRegression
# X_train and y_train are your training data
model = LogisticRegression(max_iter=1000)
model.fit(X_train, Y_train)
import joblib
# Save the trained model to a file
joblib.dump(model, 'logistic_regression_model.pkl')
# Load the trained model from the file
loaded_model = joblib.load('logistic_regression_model.pkl')
# X_test is your test data
predictions = loaded_model.predict(X_test)
print(X_test)
print(predictions)
[[ 1. 90. 62. ... 27.2 0.58 24. ]
[ 7. 181. 84. ... 35.9 0.586 51. ]
[ 13. 152. 90. ... 26.8 0.731 43. ]
...
[ 4. 118. 70. ... 44.5 0.904 26. ]
[ 7. 152. 88. ... 50. 0.337 36. ]
[ 7. 168. 88. ... 38.2 0.787 40. ]]
[0. 1. 1. 0. 0. 0. 0. 0. 1. 0. 1. 0. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1.
0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 1. 1. 1. 0. 0. 0. 0. 1. 0.
1. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 1. 0. 1. 1. 1. 0. 1. 1. 0. 1. 1.
0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 1. 0. 1. 0. 1. 1. 0. 0.
1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 1. 0. 0. 1. 0. 0. 0. 1. 1.
0. 0. 0. 0. 0. 1. 0. 1. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 1.
0. 0. 0. 0. 1. 1. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.
0. 1. 0. 1. 1. 0. 0. 0. 1. 1. 1. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.
1. 1. 0. 0. 1. 0. 0. 1. 0. 1. 1. 1. 0. 0. 0. 0. 0. 0. 1. 1. 0. 0. 0. 0.
0. 1. 0. 0. 0. 1. 1. 0. 0. 1. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 1. 1. 0.
1. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 1. 1. 1.]
반응형
댓글