Skip to content

feat!: model.predict returns all the columns #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 16, 2023
4 changes: 2 additions & 2 deletions bigframes/ml/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from __future__ import annotations

from typing import cast, Dict, List, Optional, Union
from typing import Dict, List, Optional, Union

from google.cloud import bigquery

Expand Down Expand Up @@ -92,7 +92,7 @@ def predict(

(X,) = utils.convert_to_dataframe(X)

return cast(bpd.DataFrame, self._bqml_model.predict(X)[["CENTROID_ID"]])
return self._bqml_model.predict(X)

def to_gbq(self, model_name: str, replace: bool = False) -> KMeans:
"""Save the model to BigQuery.
Expand Down
9 changes: 2 additions & 7 deletions bigframes/ml/decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from __future__ import annotations

from typing import cast, List, Optional, Union
from typing import List, Optional, Union

from google.cloud import bigquery

Expand Down Expand Up @@ -106,12 +106,7 @@ def predict(self, X: Union[bpd.DataFrame, bpd.Series]) -> bpd.DataFrame:

(X,) = utils.convert_to_dataframe(X)

return cast(
bpd.DataFrame,
self._bqml_model.predict(X)[
["principal_component_" + str(i + 1) for i in range(self.n_components)]
],
)
return self._bqml_model.predict(X)

def to_gbq(self, model_name: str, replace: bool = False) -> PCA:
"""Save the model to BigQuery.
Expand Down
49 changes: 5 additions & 44 deletions bigframes/ml/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from __future__ import annotations

from typing import cast, Dict, List, Literal, Optional, Union
from typing import Dict, List, Literal, Optional, Union

from google.cloud import bigquery

Expand Down Expand Up @@ -168,16 +168,7 @@ def predict(
raise RuntimeError("A model must be fitted before predict")
(X,) = utils.convert_to_dataframe(X)

df = self._bqml_model.predict(X)
return cast(
bpd.DataFrame,
df[
[
cast(str, field.name)
for field in self._bqml_model.model.label_columns
]
],
)
return self._bqml_model.predict(X)

def score(
self,
Expand Down Expand Up @@ -328,19 +319,9 @@ def _fit(
def predict(self, X: Union[bpd.DataFrame, bpd.Series]) -> bpd.DataFrame:
if not self._bqml_model:
raise RuntimeError("A model must be fitted before predict")

(X,) = utils.convert_to_dataframe(X)

df = self._bqml_model.predict(X)
return cast(
bpd.DataFrame,
df[
[
cast(str, field.name)
for field in self._bqml_model.model.label_columns
]
],
)
return self._bqml_model.predict(X)

def score(
self,
Expand Down Expand Up @@ -486,19 +467,9 @@ def predict(
) -> bpd.DataFrame:
if not self._bqml_model:
raise RuntimeError("A model must be fitted before predict")

(X,) = utils.convert_to_dataframe(X)

df = self._bqml_model.predict(X)
return cast(
bpd.DataFrame,
df[
[
cast(str, field.name)
for field in self._bqml_model.model.label_columns
]
],
)
return self._bqml_model.predict(X)

def score(
self,
Expand Down Expand Up @@ -661,19 +632,9 @@ def predict(
) -> bpd.DataFrame:
if not self._bqml_model:
raise RuntimeError("A model must be fitted before predict")

(X,) = utils.convert_to_dataframe(X)

df = self._bqml_model.predict(X)
return cast(
bpd.DataFrame,
df[
[
cast(str, field.name)
for field in self._bqml_model.model.label_columns
]
],
)
return self._bqml_model.predict(X)

def score(
self,
Expand Down
9 changes: 2 additions & 7 deletions bigframes/ml/forecasting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

from __future__ import annotations

from typing import cast, Dict, List, Optional, Union
from typing import Dict, List, Optional, Union

from google.cloud import bigquery

import bigframes
from bigframes.ml import base, core, globals, utils
import bigframes.pandas as bpd

_PREDICT_OUTPUT_COLUMNS = ["forecast_timestamp", "forecast_value"]


class ARIMAPlus(base.SupervisedTrainablePredictor):
"""Time Series ARIMA Plus model."""
Expand Down Expand Up @@ -100,10 +98,7 @@ def predict(self, X=None) -> bpd.DataFrame:
if not self._bqml_model:
raise RuntimeError("A model must be fitted before predict")

return cast(
bpd.DataFrame,
self._bqml_model.forecast()[_PREDICT_OUTPUT_COLUMNS],
)
return self._bqml_model.forecast()

def score(
self,
Expand Down
22 changes: 2 additions & 20 deletions bigframes/ml/imported.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,7 @@ def predict(self, X: Union[bpd.DataFrame, bpd.Series]) -> bpd.DataFrame:

(X,) = utils.convert_to_dataframe(X)

df = self._bqml_model.predict(X)
return cast(
bpd.DataFrame,
df[
[
cast(str, field.name)
for field in self._bqml_model.model.label_columns
]
],
)
return self._bqml_model.predict(X)

def to_gbq(self, model_name: str, replace: bool = False) -> TensorFlowModel:
"""Save the model to BigQuery.
Expand Down Expand Up @@ -161,16 +152,7 @@ def predict(self, X: Union[bpd.DataFrame, bpd.Series]) -> bpd.DataFrame:

(X,) = utils.convert_to_dataframe(X)

df = self._bqml_model.predict(X)
return cast(
bpd.DataFrame,
df[
[
cast(str, field.name)
for field in self._bqml_model.model.label_columns
]
],
)
return self._bqml_model.predict(X)

def to_gbq(self, model_name: str, replace: bool = False) -> ONNXModel:
"""Save the model to BigQuery.
Expand Down
24 changes: 3 additions & 21 deletions bigframes/ml/linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from __future__ import annotations

from typing import cast, Dict, List, Literal, Optional, Union
from typing import Dict, List, Literal, Optional, Union

from google.cloud import bigquery

Expand Down Expand Up @@ -145,16 +145,7 @@ def predict(self, X: Union[bpd.DataFrame, bpd.Series]) -> bpd.DataFrame:

(X,) = utils.convert_to_dataframe(X)

df = self._bqml_model.predict(X)
return cast(
bpd.DataFrame,
df[
[
cast(str, field.name)
for field in self._bqml_model.model.label_columns
]
],
)
return self._bqml_model.predict(X)

def score(
self,
Expand Down Expand Up @@ -267,16 +258,7 @@ def predict(

(X,) = utils.convert_to_dataframe(X)

df = self._bqml_model.predict(X)
return cast(
bpd.DataFrame,
df[
[
cast(str, field.name)
for field in self._bqml_model.model.label_columns
]
],
)
return self._bqml_model.predict(X)

def score(
self,
Expand Down
17 changes: 5 additions & 12 deletions bigframes/ml/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ def predict(


Returns:
bigframes.dataframe.DataFrame: Output DataFrame with only 1 column as the output text results."""
bigframes.dataframe.DataFrame: DataFrame of shape (n_samples, n_input_columns + n_prediction_columns). Returns predicted values.
"""

# Params reference: https://cloud.google.com/vertex-ai/docs/generative-ai/learn/models
if temperature < 0.0 or temperature > 1.0:
Expand Down Expand Up @@ -181,11 +182,7 @@ def predict(
"top_p": top_p,
"flatten_json_output": True,
}
df = self._bqml_model.generate_text(X, options)
return cast(
bpd.DataFrame,
df[[_TEXT_GENERATE_RESULT_COLUMN]],
)
return self._bqml_model.generate_text(X, options)


class PaLM2TextEmbeddingGenerator(base.Predictor):
Expand Down Expand Up @@ -269,7 +266,7 @@ def predict(self, X: Union[bpd.DataFrame, bpd.Series]) -> bpd.DataFrame:
Input DataFrame, which needs to contain a column with name "content". Only the column will be used as input. Content can include preamble, questions, suggestions, instructions, or examples.

Returns:
bigframes.dataframe.DataFrame: Output DataFrame with only 1 column as the output embedding results
bigframes.dataframe.DataFrame: DataFrame of shape (n_samples, n_input_columns + n_prediction_columns). Returns predicted values.
"""

# Params reference: https://cloud.google.com/vertex-ai/docs/generative-ai/learn/models
Expand All @@ -287,8 +284,4 @@ def predict(self, X: Union[bpd.DataFrame, bpd.Series]) -> bpd.DataFrame:
options = {
"flatten_json_output": True,
}
df = self._bqml_model.generate_text_embedding(X, options)
return cast(
bpd.DataFrame,
df[[_EMBED_TEXT_RESULT_COLUMN]],
)
return self._bqml_model.generate_text_embedding(X, options)
Loading