from sklearn.datasets import load_iris
iris_dataset = load_iris()
iris_dataset.feature_names
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
iris_dataset.target
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
X_feature = iris_dataset.data
Y_target = iris_dataset.target
X_feature[-2:-1]
array([[6.2, 3.4, 5.4, 2.3]])
X_new = [[3,5,4,1],[5,3,4,2],[1,2,3,4],[5.9, 3. , 5.1, 1.8]]
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
logreg.fit(X_feature,Y_target)
D:\Users\kevin.zhang\Anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py:763: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
n_iter_i = _check_optimize_result(
LogisticRegression()
logreg.predict(X_new)
array([0, 1, 2, 2])
import pickle as pkl
persist_model = pkl.dumps(logreg)
persist_model
b'\x80\x04\x95\x04\x03\x00\x00\x00\x00\x00\x00\x8c\x1esklearn.linear_model._logistic\x94\x8c\x12LogisticRegression\x94\x93\x94)\x81\x94}\x94(\x8c\x07penalty\x94\x8c\x02l2\x94\x8c\x04dual\x94\x89\x8c\x03tol\x94G?\x1a6\xe2\xeb\x1cC-\x8c\x01C\x94G?\xf0\x00\x00\x00\x00\x00\x00\x8c\rfit_intercept\x94\x88\x8c\x11intercept_scaling\x94K\x01\x8c\x0cclass_weight\x94N\x8c\x0crandom_state\x94N\x8c\x06solver\x94\x8c\x05lbfgs\x94\x8c\x08max_iter\x94Kd\x8c\x0bmulti_class\x94\x8c\x04auto\x94\x8c\x07verbose\x94K\x00\x8c\nwarm_start\x94\x89\x8c\x06n_jobs\x94N\x8c\x08l1_ratio\x94N\x8c\x0en_features_in_\x94K\x04\x8c\x08classes_\x94\x8c\x15numpy.core.multiarray\x94\x8c\x0c_reconstruct\x94\x93\x94\x8c\x05numpy\x94\x8c\x07ndarray\x94\x93\x94K\x00\x85\x94C\x01b\x94\x87\x94R\x94(K\x01K\x03\x85\x94h\x1c\x8c\x05dtype\x94\x93\x94\x8c\x02i4\x94\x89\x88\x87\x94R\x94(K\x03\x8c\x01<\x94NNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00t\x94b\x89C\x0c\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x94t\x94b\x8c\x07n_iter_\x94h\x1bh\x1eK\x00\x85\x94h \x87\x94R\x94(K\x01K\x01\x85\x94h(\x89C\x04d\x00\x00\x00\x94t\x94b\x8c\x05coef_\x94h\x1bh\x1eK\x00\x85\x94h \x87\x94R\x94(K\x01K\x03K\x04\x86\x94h%\x8c\x02f8\x94\x89\x88\x87\x94R\x94(K\x03h)NNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00t\x94b\x89C`&\xb7\x0c?\xb0\xc2\xda\xbf\x8d\x93\x94D5\xec\xee?}\x84\xe7\x9a\x10+\x04\xc0p\x94MitX\xf1\xbf-\xbf\xa5\xc9+\xf8\xe0?\x91\xed\xbeM\x86\x1f\xd4\xbfgF\xb2Wn\x88\xc9\xbf,\xa5\x96\xa9YW\xee\xbf\x13\x1d\xfbP\x9d\xb6\xbc\xbf\xac\x1c\xb5\x1dr\xdc\xe4\xbf\xaa\xa8b\x80\x97\xc3\x05@\x81s\x0c\x9f\x10B\x00@\x94t\x94b\x8c\nintercept_\x94h\x1bh\x1eK\x00\x85\x94h \x87\x94R\x94(K\x01K\x03\x85\x94h;\x89C\x18G\xc8\xd1\xf7\xef\xac#@\xd1\x13\x84;\x08\xbf\x01@<\xce\xb2\x06\xb2\x1c(\xc0\x94t\x94b\x8c\x10_sklearn_version\x94\x8c\x060.24.2\x94ub.'
import joblib
joblib.dump(logreg, 'logreg.pkl')
['logreg.pkl']
new_logreg_estimator = joblib.load('logreg.pkl')
new_logreg_estimator.predict(X_new)
array([0, 1])