difference in random values

Asked 1 years ago, Updated 1 years ago, 98 views

I had a similar problem with some programming study sites, so I'd like to ask you an example. I copied and ran the following sample code while studying in Python in the Python machine learning programming, and the output was different and the number of misclassified samples was 4.I had other people enter the same code, but I was the only one with a different answer.I thought the random value was different, but it might be another reason.Please let me know if you know the cause.

Python version is 3.5.6

Sample Code

import numpy as np
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import acuity_score
from sklearn.linear_model importLogisticRegression
from sklearn.linear_model import Perceptron
import matplotlib.pyplot asplt


# for sklearn 0.18's alternative syntax
from distutils.version import LooseVersion as Version
from sklearn import__version_assklearn_version
if Version(sklearn_version)<'0.18':
    from sklearn.grid_search import train_test_split
else:
    from sklearn.model_selection import train_test_split

iris=datasets.load_iris()
X=iris.data[:,[2,3]]
y = iris.target
print('Class labels:',np.unique(y))

X_train, X_test, y_train, y_test=train_test_split(
    X, y, test_size = 0.3, random_state = 0)

sc=StandardScaler()
sc.fit(X_train)
X_train_std=sc.transform(X_train)
X_test_std = sc.transform(X_test)

ppn=Perceptron (n_iter=40, eta0=0.1, random_state=0)
ppn.fit(X_train_std,y_train)
print('Yarray shape',y_test.shape')

y_pred=ppn.predict(X_test_std)
print('Misclassified samples: %d'%(y_test!=y_pred).sum())

python python3 machine-learning

2022-09-30 21:39

1 Answers

I think it depends on the scikit-learn version.I checked in my environment and found that in version 0.18 4,

$pip install scikit-learn==0.18
Collecting scikit-learn == 0.18
  Downloading https://files.pythonhosted.org/packages/e9/fc/d923732ac9ddee7eb883d94dd3d127425280c9986ef47bae8656db34fe9f/scikit_learn-0.18-cp35-cp35m-manylinux1_x86_64.whl (11.3MB)
    100% | ████ 11████████████ .███████ s | 3 |█ %MBMB 1█ . 100 / | 11.3MB 1.0MB/s 
Installing collected packages:scikit-learn
Successfully installed script-learn-0.18
$ python3 test.py 
Class labels: [0 12]
Yarray shape (45,)
Misclassified samples—4

In version 0.20, it was 9.

$pip uninstall scikit-learn
Uninstalling script-learn-0.18:
  Would remove:
    /home/tamura/todel/venv/lib/python 3.5/site-packages/scikit_learn-0.18.dist-info/*
    /home/tamura/todel/venv/lib/python 3.5/site-packages/sklearn/*
Proceed(y/n)?y
  Successfully uninstalled script-learn-0.18
$ pip install scikit-learn == 0.20
Collecting scikit-learn == 0.20
  Using cached https://files.pythonhosted.org/packages/dc/8f/416ccf81408cd8ea84be2a38efe34cc885966c4b6edbe705d2642e22d208/scikit_learn-0.20.0-cp35-cp35m-manylinux1_x86_64.whl
Requirement already satified: numpy>=1.8.2 in./venv/lib/python 3.5/site-packages (from script-learn==0.20) (1.16.3)
Requirement already satified —scipy>=0.13.3 in./venv/lib/python 3.5/site-packages (from script-learn==0.20) (1.2.1)
Installing collected packages:scikit-learn
Successfully installed script-learn-0.20.0
$ python3 test.py 
Class labels: [0 12]
/home/tamura/todel/venv/lib/python3.5/site-packages/sklearn/linear_model/stochastic_gradient.py:130: DeprecationWarning: n_iter parameter is degraded in 0.19 and will be removed in 0.21.Use max_iter and tool installed.
  DeprecationWarning)
Yarray shape (45,)
Misclassified samples—9


2022-09-30 21:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.