Python sklearn.exceptions.NotFittedError() Examples

The following are 30 code examples of sklearn.exceptions.NotFittedError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sklearn.exceptions , or try the search function .
Example #1
Source File: test_model.py    From gordo with GNU Affero General Public License v3.0 7 votes vote down vote up
def test_keras_autoencoder_scoring(model, kind, n_features_out):
    """
    Test the KerasAutoEncoder and KerasLSTMAutoEncoder have a working scoring function
    """
    Model = pydoc.locate(f"gordo.machine.model.models.{model}")
    model = Pipeline([("model", Model(kind=kind))])
    X = np.random.random((8, 2))

    # Should be able to deal with y output different than X input features
    y = np.random.random((8, n_features_out))

    with pytest.raises(NotFittedError):
        model.score(X, y)

    model.fit(X, y)
    score = model.score(X, y)
    logger.info(f"Score: {score:.4f}") 
Example #2
Source File: test_voting.py    From Mastering-Elasticsearch-7.0 with MIT License 7 votes vote down vote up
def test_notfitted():
    eclf = VotingClassifier(estimators=[('lr1', LogisticRegression()),
                                        ('lr2', LogisticRegression())],
                            voting='soft')
    ereg = VotingRegressor([('dr', DummyRegressor())])
    msg = ("This %s instance is not fitted yet. Call \'fit\'"
           " with appropriate arguments before using this method.")
    assert_raise_message(NotFittedError, msg % 'VotingClassifier',
                         eclf.predict, X)
    assert_raise_message(NotFittedError, msg % 'VotingClassifier',
                         eclf.predict_proba, X)
    assert_raise_message(NotFittedError, msg % 'VotingClassifier',
                         eclf.transform, X)
    assert_raise_message(NotFittedError, msg % 'VotingRegressor',
                         ereg.predict, X_r)
    assert_raise_message(NotFittedError, msg % 'VotingRegressor',
                         ereg.transform, X_r) 
Example #3
Source File: fm_classifier.py    From muffnn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def predict(self, X):
        """Compute the predicted class.

        Parameters
        ----------
        X : numpy array or sparse matrix [n_samples, n_features]
            Data.

        Returns
        -------
        numpy array [n_samples]
            Predicted class.
        """
        if not self._is_fitted:
            raise NotFittedError("Call fit before predict!")
        return self.classes_[self.predict_proba(X).argmax(axis=1)] 
Example #4
Source File: test_model.py    From baikal with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_nested_model(teardown):
    x_data = iris.data
    y_t_data = iris.target

    # Sub-model
    x = Input()
    y_t = Input()
    h = PCA(n_components=2)(x)
    y = LogisticRegression()(h, y_t)
    submodel = Model(x, y, y_t)

    # Model
    x = Input()
    y_t = Input()
    y = submodel(x, y_t)
    model = Model(x, y, y_t)

    with raises_with_cause(RuntimeError, NotFittedError):
        submodel.predict(x_data)

    model.fit(x_data, y_t_data)
    y_pred = model.predict(x_data)
    y_pred_sub = submodel.predict(x_data)

    assert_array_equal(y_pred, y_pred_sub) 
Example #5
Source File: test_stack.py    From picknmix with MIT License 6 votes vote down vote up
def test_stack_copy_function_only_model(self):
        first_layer = Layer([LinearRegression(), LogisticRegression()])
        second_layer = Layer([LinearRegression()])
        model = Stack([first_layer, second_layer])

        X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
        y = np.dot(X, np.array([1, 2])) + 3
        model.fit(X, y)
        model2 = model.copy()
        gotError = False
        try:
            model2.predict([1, 2])
        except(NotFittedError):
            gotError = True

        assert gotError, "Model failed the copy Test: When copying, a deep copy was produced" 
Example #6
Source File: test_stack.py    From picknmix with MIT License 6 votes vote down vote up
def test_stack_copy_function_model_and_preprocessor(self):
        first_layer = Layer(models=[LogisticRegression(), LinearRegression()], preprocessors=[MinMaxScaler(), None])
        second_layer = Layer([LinearRegression()], preprocessors=[MinMaxScaler()])
        model = Stack([first_layer, second_layer])

        X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
        y = np.dot(X, np.array([1, 2])) + 3
        model.fit(X, y)
        model2 = model.copy()
        gotError = False
        try:
            model2.predict([1,2])
        except(NotFittedError):
            gotError = True

        assert gotError, "Model failed the copy Test: When copying, a deep copy was produced" 
Example #7
Source File: phate.py    From PHATE with GNU General Public License v2.0 6 votes vote down vote up
def diff_op(self):
        """The diffusion operator calculated from the data
        """
        if self.graph is not None:
            if isinstance(self.graph, graphtools.graphs.LandmarkGraph):
                diff_op = self.graph.landmark_op
            else:
                diff_op = self.graph.diff_op
            if sparse.issparse(diff_op):
                diff_op = diff_op.toarray()
            return diff_op
        else:
            raise NotFittedError(
                "This PHATE instance is not fitted yet. Call "
                "'fit' with appropriate arguments before "
                "using this method."
            ) 
Example #8
Source File: test_gaussian_mixture.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_gaussian_mixture_predict_predict_proba():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)
    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]
        Y = rand_data.Y
        g = GaussianMixture(n_components=rand_data.n_components,
                            random_state=rng, weights_init=rand_data.weights,
                            means_init=rand_data.means,
                            precisions_init=rand_data.precisions[covar_type],
                            covariance_type=covar_type)

        # Check a warning message arrive if we don't do fit
        assert_raise_message(NotFittedError,
                             "This GaussianMixture instance is not fitted "
                             "yet. Call 'fit' with appropriate arguments "
                             "before using this method.", g.predict, X)

        g.fit(X)
        Y_pred = g.predict(X)
        Y_pred_proba = g.predict_proba(X).argmax(axis=1)
        assert_array_equal(Y_pred, Y_pred_proba)
        assert_greater(adjusted_rand_score(Y, Y_pred), .95) 
Example #9
Source File: test_elliptic_envelope.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_elliptic_envelope():
    rnd = np.random.RandomState(0)
    X = rnd.randn(100, 10)
    clf = EllipticEnvelope(contamination=0.1)
    assert_raises(NotFittedError, clf.predict, X)
    assert_raises(NotFittedError, clf.decision_function, X)
    clf.fit(X)
    y_pred = clf.predict(X)
    scores = clf.score_samples(X)
    decisions = clf.decision_function(X)

    assert_array_almost_equal(
        scores, -clf.mahalanobis(X))
    assert_array_almost_equal(clf.mahalanobis(X), clf.dist_)
    assert_almost_equal(clf.score(X, np.ones(100)),
                        (100 - y_pred[y_pred == -1].size) / 100.)
    assert(sum(y_pred == -1) == sum(decisions < 0)) 
Example #10
Source File: test_event.py    From brainiak with Apache License 2.0 6 votes vote down vote up
def test_event_transfer():
    es = EventSegment(2)
    sample_data = np.asarray([[1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1]])

    with pytest.raises(NotFittedError):
        seg = es.find_events(sample_data.T)[0]
        pytest.fail("Should need to set variance")

    with pytest.raises(NotFittedError):
        seg = es.find_events(sample_data.T, np.asarray([1, 1]))[0]
        pytest.fail("Should need to set patterns")

    es.set_event_patterns(np.asarray([[1, 0], [0, 1]]))
    seg = es.find_events(sample_data.T, np.asarray([1, 1]))[0]

    events = np.argmax(seg, axis=1)
    assert np.array_equal(events, [0, 0, 0, 1, 1, 1, 1]),\
        "Failed to correctly transfer two events to new data" 
Example #11
Source File: _preprocessors.py    From pytorch-widedeep with MIT License 6 votes vote down vote up
def transform(self, df: pd.DataFrame) -> Union[sparse_matrix, np.ndarray]:
        try:
            self.one_hot_enc.categories_
        except:
            raise NotFittedError(
                "This WidePreprocessor instance is not fitted yet. "
                "Call 'fit' with appropriate arguments before using this estimator."
            )
        df_wide = df.copy()[self.wide_cols]
        if self.crossed_cols is not None:
            df_wide, _ = self._cross_cols(df_wide)
        if self.already_dummies:
            X_oh_1 = df_wide[self.already_dummies].values
            dummy_cols = [
                c for c in self.wide_crossed_cols if c not in self.already_dummies
            ]
            X_oh_2 = self.one_hot_enc.transform(df_wide[dummy_cols])
            return np.hstack((X_oh_1, X_oh_2))
        else:
            return self.one_hot_enc.transform(df_wide[self.wide_crossed_cols]) 
Example #12
Source File: _preprocessors.py    From pytorch-widedeep with MIT License 6 votes vote down vote up
def transform(self, df: pd.DataFrame) -> np.ndarray:
        try:
            self.vocab
        except:
            raise NotFittedError(
                "This TextPreprocessor instance is not fitted yet. "
                "Call 'fit' with appropriate arguments before using this estimator."
            )
        texts = df[self.text_col].tolist()
        self.tokens = get_texts(texts)
        sequences = [self.vocab.numericalize(t) for t in self.tokens]
        padded_seq = np.array([pad_sequences(s, maxlen=self.maxlen) for s in sequences])
        if self.verbose:
            print("The vocabulary contains {} tokens".format(len(self.vocab.stoi)))
        if self.word_vectors_path is not None:
            self.embedding_matrix = build_embeddings_matrix(
                self.vocab, self.word_vectors_path, self.min_freq
            )
        return padded_seq 
Example #13
Source File: base.py    From xam with MIT License 6 votes vote down vote up
def transform(self, X, y=None):
        """Binarize X based on the fitted cut points."""

        # scikit-learn checks
        X = check_array(X)

        if self.cut_points is None:
            raise NotFittedError('Estimator not fitted, call `fit` before exploiting the model.')

        if X.shape[1] != len(self.cut_points):
            raise ValueError("Provided array's dimensions do not match with the ones from the "
                             "array `fit` was called on.")

        binned = np.array([
            np.digitize(x, self.cut_points[i])
            if len(self.cut_points[i]) > 0
            else np.zeros(x.shape)
            for i, x in enumerate(X.T)
        ]).T

        return binned 
Example #14
Source File: base.py    From muffnn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _compute_output(self, X):
        """Get the outputs of the network, for use in prediction methods."""

        if not self._is_fitted:
            raise NotFittedError("Call fit before prediction")

        X = self._check_X(X)

        # Make predictions in batches.
        pred_batches = []
        start_idx = 0
        n_examples = X.shape[0]
        with self.graph_.as_default():
            while start_idx < n_examples:
                X_batch = \
                    X[start_idx:min(start_idx + self.batch_size, n_examples)]
                feed_dict = self._make_feed_dict(X_batch)
                start_idx += self.batch_size
                pred_batches.append(
                    self._session.run(self.output_layer_, feed_dict=feed_dict))
        y_pred = np.concatenate(pred_batches)
        return y_pred 
Example #15
Source File: text2mat.py    From hypertools with MIT License 6 votes vote down vote up
def _fit_models(vmodel, tmodel, x, model_is_fit):
    if model_is_fit==True:
        return
    if vmodel is not None:
        try:
            check_is_fitted(vmodel, ['vocabulary_'])
        except NotFittedError:
            vmodel.fit(np.vstack(x).ravel())
    if tmodel is not None:
        try:
            check_is_fitted(tmodel, ['components_'])
        except NotFittedError:
            if isinstance(tmodel, Pipeline):
                tmodel.fit(np.vstack(x).ravel())
            else:
                tmodel.fit(vmodel.transform(np.vstack(x).ravel())) 
Example #16
Source File: fm_classifier.py    From muffnn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def predict_log_proba(self, X):
        """Compute log p(y=1).

        Parameters
        ----------
        X : numpy array or sparse matrix [n_samples, n_features]
            Data.

        Returns
        -------
        numpy array [n_samples]
            Log probabilities.
        """
        if not self._is_fitted:
            raise NotFittedError("Call fit before predict_log_proba!")
        return np.log(self.predict_proba(X)) 
Example #17
Source File: common_tests.py    From kenchi with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_predict_notffied(self):
        self.assertRaises(NotFittedError, self.sut.predict, self.X_test) 
Example #18
Source File: test_pipeline.py    From kenchi with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_plot_graphical_model_notfitted(self):
        self.assertRaises(NotFittedError, self.sut.plot_graphical_model) 
Example #19
Source File: mixins.py    From fsfc with MIT License 5 votes vote down vote up
def _get_support_mask(self):
        if not self._check_scores_set():
            raise NotFittedError('Feature Selector is not fitted')
        mask = np.zeros(self._get_scores().shape, dtype=bool)
        mask[np.argsort(self._get_scores(), kind="mergesort")[-self._get_k():]] = 1
        return mask 
Example #20
Source File: _base.py    From ibex with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def x_columns(self):
        """
        The X columns set in the last call to fit.

        Set this property at fit, and call it in other methods:

        """
        try:
            return self.__x_cols
        except AttributeError:
            raise NotFittedError() 
Example #21
Source File: test_pipeline.py    From kenchi with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_featurewise_anomaly_score_notfitted(self):
        self.assertRaises(
            NotFittedError, self.sut.featurewise_anomaly_score, self.X_test
        ) 
Example #22
Source File: test_model.py    From baikal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_with_non_fitted_non_trainable_step(self, teardown):
        x = Input()
        y_t = Input()
        z = PCA()(x, trainable=False)
        y = LogisticRegression()(z, y_t)
        model = Model(x, y, y_t)
        with raises_with_cause(RuntimeError, NotFittedError):
            # this will raise an error when calling compute
            # on PCA which was flagged as trainable=False but
            # hasn't been fitted
            model.fit(iris.data, iris.target) 
Example #23
Source File: test_statistical.py    From kenchi with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_plot_partial_corrcoef_notfitted(self):
        self.assertRaises(NotFittedError, self.sut.plot_partial_corrcoef) 
Example #24
Source File: test_statistical.py    From kenchi with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_plot_graphical_model_notfitted(self):
        self.assertRaises(NotFittedError, self.sut.plot_graphical_model) 
Example #25
Source File: test_model.py    From baikal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_predict_with_not_fitted_steps(self, teardown):
        x_data = iris.data

        x = Input(name="x")
        xt = PCA(n_components=2)(x)
        y = LogisticRegression(multi_class="multinomial", solver="lbfgs")(xt)

        model = Model(x, y)
        with raises_with_cause(RuntimeError, NotFittedError):
            model.predict(x_data) 
Example #26
Source File: test_statistical.py    From kenchi with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_featurewise_anomaly_score_notfitted(self):
        self.assertRaises(
            NotFittedError, self.sut.featurewise_anomaly_score, self.X_test
        ) 
Example #27
Source File: genetic.py    From gplearn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def predict(self, X):
        """Perform regression on test vectors X.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            Input vectors, where n_samples is the number of samples
            and n_features is the number of features.

        Returns
        -------
        y : array, shape = [n_samples]
            Predicted values for X.

        """
        if not hasattr(self, '_program'):
            raise NotFittedError('SymbolicRegressor not fitted.')

        X = check_array(X)
        _, n_features = X.shape
        if self.n_features_ != n_features:
            raise ValueError('Number of features of the model must match the '
                             'input. Model n_features is %s and input '
                             'n_features is %s.'
                             % (self.n_features_, n_features))

        y = self._program.execute(X)

        return y 
Example #28
Source File: autoencoder.py    From muffnn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def score_samples(self, X, y=None):
        """Score the autoencoder on each element of `X`.

        Parameters
        ----------
        X : numpy array or sparse matrix of shape [n_samples, n_features]
            Data to score the autoencoder with.

        Returns
        -------
        scores : numpy array
            The score for each element of `X`.
        """

        if not self._is_fitted:
            raise NotFittedError("Call fit before transform!")

        # For sparse input, make the input a CSR matrix since it can be
        # indexed by row.
        X = check_array(X, accept_sparse=['csr'])

        # Check input data against internal data.
        # Raises an error on failure.
        self._check_data(X)

        # Make predictions in batches.
        scores = []
        start_idx = 0
        n_examples = X.shape[0]
        with self.graph_.as_default():
            while start_idx < n_examples:
                X_batch = \
                    X[start_idx:min(start_idx + self.batch_size, n_examples)]
                feed_dict = self._make_feed_dict(X_batch, training=False)
                start_idx += self.batch_size
                scores.append(self._session.run(self._scores,
                                                feed_dict=feed_dict))
        return np.concatenate(scores) 
Example #29
Source File: genetic.py    From gplearn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def predict_proba(self, X):
        """Predict probabilities on test vectors X.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            Input vectors, where n_samples is the number of samples
            and n_features is the number of features.

        Returns
        -------
        proba : array, shape = [n_samples, n_classes]
            The class probabilities of the input samples. The order of the
            classes corresponds to that in the attribute `classes_`.

        """
        if not hasattr(self, '_program'):
            raise NotFittedError('SymbolicClassifier not fitted.')

        X = check_array(X)
        _, n_features = X.shape
        if self.n_features_ != n_features:
            raise ValueError('Number of features of the model must match the '
                             'input. Model n_features is %s and input '
                             'n_features is %s.'
                             % (self.n_features_, n_features))

        scores = self._program.execute(X)
        proba = self._transformer(scores)
        proba = np.vstack([1 - proba, proba]).T
        return proba 
Example #30
Source File: common_tests.py    From kenchi with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_decision_function_notffied(self):
        self.assertRaises(
            NotFittedError, self.sut.decision_function, self.X_test
        )