Scikit-Learn
27
from sklearn import random_projection
rannge = np.random.RandomState(0)
X = range.rand(10,2000)
X = np.array(X, dtype = 'float32')
X.dtype
Transformer_data = random_projection.GaussianRandomProjection()
X_new = transformer.fit_transform(X)
X_new.dtype
Output
dtype('float32')
dtype('float64')
In the above example, we can see that X is
float32
which
is cast to
float64
by
fit_transform(X).
Refitting & Updating Parameters
Hyper-parameters of an estimator can be updated and refitted after it has been
constructed via the
set_params()
method. Let’s see the following example to understand
it:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.svm
import SVC
X, y = load_iris(return_X_y=True)
clf = SVC()
clf.set_params(kernel='linear').fit(X, y)
clf.predict(X[:5])
Output
Scikit-Learn
28
array([0, 0, 0, 0, 0])
Once the estimator has been constructed, above code will change the default kernel
rbf
to linear via
SVC.set_params().
Now, the following code will change back the kernel to
rbf
to refit the estimator and to
make a second prediction.
clf.set_params(kernel='rbf', gamma='scale').fit(X, y)
clf.predict(X[:5])
Output
array([0, 0, 0, 0, 0])
Complete code
The following is the complete executable program:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.svm import SVC
X, y = load_iris(return_X_y=True)
clf = SVC()
clf.set_params(kernel='linear').fit(X, y)
clf.predict(X[:5])
clf.set_params(kernel='rbf', gamma='scale').fit(X, y)
clf.predict(X[:5])
Dostları ilə paylaş: