|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*-- |
| 3 | + |
| 4 | +# Copyright (c) 2023 Oracle and/or its affiliates. |
| 5 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 6 | + |
| 7 | +from typing import Tuple |
| 8 | +import pandas as pd |
| 9 | +from ads.dataset import progress |
| 10 | + |
| 11 | +from ads.type_discovery.typed_feature import TypedFeature |
| 12 | + |
| 13 | +class ADSDatasetAccessMixin: |
| 14 | + |
| 15 | + def dataset( |
| 16 | + self, |
| 17 | + sampled_df=None, |
| 18 | + shape=None, |
| 19 | + name="", |
| 20 | + description=None, |
| 21 | + type_discovery=True, |
| 22 | + types={}, |
| 23 | + metadata=None, |
| 24 | + progress=progress.DummyProgressBar(), |
| 25 | + transformer_pipeline=None, |
| 26 | + interactive=False, |
| 27 | + **kwargs, |
| 28 | + ): |
| 29 | + """Converts pandas DataFrame into ADS Dataset. |
| 30 | +
|
| 31 | + Parameters |
| 32 | + ---------- |
| 33 | + sampled_df: pandas.DataFrame, optional |
| 34 | + The sampled pandas DataFrame. Defaults to None. |
| 35 | + shape: Tuple, optional |
| 36 | + The shape of pandas DataFrame. Defaults to None. |
| 37 | + name: str, optional |
| 38 | + The name of ADS Dataset. Defaults to "". |
| 39 | + description: str, optional |
| 40 | + Text describing the dataset. Defaults to "". |
| 41 | + type_discovery: bool. optional |
| 42 | + If false, the data types of the dataframe are used as such. |
| 43 | + By default, the dataframe columns are associated with the best suited data types. Associating the features |
| 44 | + with the disovered datatypes would impact visualizations and model prediction. Defaults to True. |
| 45 | + types: dict, optional |
| 46 | + Dictionary of <feature_name> : <data_type> to override the data type of features. Defaults to {}. |
| 47 | + metadata: dict, optional |
| 48 | + The metadata of ADS Dataset. Defaults to None. |
| 49 | + progress: dataset.progress.ProgressBar, optional |
| 50 | + The progress bar for ADS Dataset. Defaults to progress.DummyProgressBar() |
| 51 | + transformer_pipeline: datasets.pipeline.TransformerPipeline, optional |
| 52 | + A pipeline of transformations done outside the sdk and need to be applied at the time of scoring |
| 53 | + kwargs: additional keyword arguments that would be passed to underlying dataframe read API |
| 54 | + based on the format of the dataset |
| 55 | +
|
| 56 | + Returns |
| 57 | + ------- |
| 58 | + ADSDataset: |
| 59 | + An instance of ADSDataset |
| 60 | +
|
| 61 | + Examples |
| 62 | + -------- |
| 63 | + >>> import pandas as pd |
| 64 | + >>> df = pd.read_csv(<path_to_csv>) |
| 65 | + >>> ds = df.ads.dataset() |
| 66 | + """ |
| 67 | + from ads.dataset.dataset import ADSDataset |
| 68 | + |
| 69 | + return ADSDataset.from_dataframe( |
| 70 | + df=self._obj, |
| 71 | + sampled_df=sampled_df, |
| 72 | + shape=shape, |
| 73 | + name=name, |
| 74 | + description=description, |
| 75 | + type_discovery=type_discovery, |
| 76 | + types=types, |
| 77 | + metadata=metadata, |
| 78 | + progress=progress, |
| 79 | + transformer_pipeline=transformer_pipeline, |
| 80 | + interactive=interactive, |
| 81 | + **kwargs |
| 82 | + ) |
| 83 | + |
| 84 | + def dataset_with_target( |
| 85 | + self, |
| 86 | + target: str, |
| 87 | + sampled_df: pd.DataFrame = None, |
| 88 | + shape: Tuple[int, int] = None, |
| 89 | + target_type: TypedFeature = None, |
| 90 | + positive_class=None, |
| 91 | + **kwargs, |
| 92 | + ): |
| 93 | + """Converts pandas DataFrame into ADS Dataset with target. |
| 94 | +
|
| 95 | + Parameters |
| 96 | + ---------- |
| 97 | + target: str, optional |
| 98 | + Name of the target in dataset. |
| 99 | + If set an ADSDatasetWithTarget object is returned, otherwise an ADSDataset object is returned which can be |
| 100 | + used to understand the dataset through visualizations |
| 101 | + sampled_df: pandas.DataFrame, optional |
| 102 | + The sampled pandas DataFrame. Defaults to None. |
| 103 | + shape: Tuple, optional |
| 104 | + The shape of pandas DataFrame. Defaults to None. |
| 105 | + target_type: TypedFeature, optional |
| 106 | + The target type of ADS Dataset. Defaults to None. |
| 107 | + positive_class: Any, optional |
| 108 | + Label in target for binary classification problems which should be identified as positive for modeling. |
| 109 | + By default, the first unique value is considered as the positive label. |
| 110 | + kwargs: additional keyword arguments that would be passed to underlying dataframe read API |
| 111 | + based on the format of the dataset |
| 112 | +
|
| 113 | + Returns |
| 114 | + ------- |
| 115 | + ADSDatasetWithTarget: |
| 116 | + An instance of ADSDatasetWithTarget |
| 117 | +
|
| 118 | + Examples |
| 119 | + -------- |
| 120 | + >>> import pandas as pd |
| 121 | + >>> df = pd.read_csv(<path_to_csv>) |
| 122 | + >>> ds = df.ads.dataset_with_target(target="target") |
| 123 | + """ |
| 124 | + from ads.dataset.dataset_with_target import ADSDatasetWithTarget |
| 125 | + |
| 126 | + return ADSDatasetWithTarget.from_dataframe( |
| 127 | + df=self._obj, |
| 128 | + target=target, |
| 129 | + sampled_df=sampled_df, |
| 130 | + shape=shape, |
| 131 | + target_type=target_type, |
| 132 | + positive_class=positive_class, |
| 133 | + **kwargs |
| 134 | + ) |
0 commit comments