| Conditions | 5 |
| Total Lines | 62 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | ''' |
||
| 83 | def train_dev_test_split(data, target, dev_size=0.1, test_size=0.1, stratify=None, random_state=408): |
||
| 84 | ''' |
||
| 85 | Split a dataset and a label column into train, dev and test sets. |
||
| 86 | |||
| 87 | Parameters: |
||
| 88 | ---------- |
||
| 89 | |||
| 90 | data: 2D dataset that can be coerced into Pandas DataFrame. If a Pandas DataFrame is provided, the index/column \ |
||
| 91 | information is used to label the plots. |
||
| 92 | |||
| 93 | target: string, list, np.array or pd.Series, default None |
||
| 94 | Specify target for correlation. E.g. label column to generate only the correlations between each feature \ |
||
| 95 | and the label. |
||
| 96 | |||
| 97 | dev_size: float, default 0.1 |
||
| 98 | If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the dev \ |
||
| 99 | split. |
||
| 100 | |||
| 101 | test_size: float, default 0.1 |
||
| 102 | If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test \ |
||
| 103 | split. |
||
| 104 | |||
| 105 | stratify: target column, default None |
||
| 106 | If not None, data is split in a stratified fashion, using the input as the class labels. |
||
| 107 | |||
| 108 | random_state: integer, default 408 |
||
| 109 | Random_state is the seed used by the random number generator. |
||
| 110 | |||
| 111 | Returns |
||
| 112 | ------- |
||
| 113 | tuple: Tuple containing train-dev-test split of inputs. |
||
| 114 | ''' |
||
| 115 | |||
| 116 | # Validate Inputs |
||
| 117 | _validate_input_range(dev_size, 'dev_size', 0, 1) |
||
| 118 | _validate_input_range(test_size, 'test_size', 0, 1) |
||
| 119 | _validate_input_int(random_state, 'random_state') |
||
| 120 | _validate_input_sum(1, 'Sum of args', dev_size, test_size) |
||
| 121 | |||
| 122 | target_data = [] |
||
| 123 | if isinstance(target, str): |
||
| 124 | target_data = data[target] |
||
| 125 | data = data.drop(target, axis=1) |
||
| 126 | |||
| 127 | elif isinstance(target, (list, pd.Series, np.ndarray)): |
||
| 128 | target_data = pd.Series(target) |
||
| 129 | target = target.name |
||
| 130 | |||
| 131 | X_train, X_dev_test, y_train, y_dev_test = train_test_split(data, target_data, |
||
| 132 | test_size=dev_size+test_size, |
||
| 133 | random_state=random_state, |
||
| 134 | stratify=stratify) |
||
| 135 | |||
| 136 | if (dev_size == 0) or (test_size == 0): |
||
| 137 | return X_train, X_dev_test, y_train, y_dev_test |
||
| 138 | |||
| 139 | else: |
||
| 140 | X_dev, X_test, y_dev, y_test = train_test_split(X_dev_test, y_dev_test, |
||
| 141 | test_size=test_size/(dev_size+test_size), |
||
| 142 | random_state=random_state, |
||
| 143 | stratify=y_dev_test) |
||
| 144 | return X_train, X_dev, X_test, y_train, y_dev, y_test |
||
| 145 |