Conditions | 5 |
Total Lines | 61 |
Code Lines | 22 |
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 | ''' |
||
89 | def train_dev_test_split(data, target, dev_size=0.1, test_size=0.1, stratify=None, random_state=1234): |
||
90 | ''' |
||
91 | Split a dataset and a label column into train, dev and test sets. |
||
92 | |||
93 | Parameters: |
||
94 | ---------- |
||
95 | |||
96 | data: 2D dataset that can be coerced into Pandas DataFrame. If a Pandas DataFrame is provided, the index/column \ |
||
97 | information is used to label the plots. |
||
98 | |||
99 | target: string, list, np.array or pd.Series, default None |
||
100 | Specify target for correlation. E.g. label column to generate only the correlations between each feature \ |
||
101 | and the label. |
||
102 | |||
103 | dev_size: float, default 0.1 |
||
104 | If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the dev \ |
||
105 | split. |
||
106 | |||
107 | test_size: float, default 0.1 |
||
108 | If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test \ |
||
109 | split. |
||
110 | |||
111 | stratify: target column, default None |
||
112 | If not None, data is split in a stratified fashion, using the input as the class labels. |
||
113 | |||
114 | random_state: integer |
||
115 | Random_state is the seed used by the random number generator. |
||
116 | |||
117 | Returns |
||
118 | ------- |
||
119 | tuple: Tuple containing train-dev-test split of inputs. |
||
120 | ''' |
||
121 | |||
122 | # Validate Inputs |
||
123 | _validate_input_int(random_state, 'random_state') |
||
124 | _validate_input_range(dev_size, 'dev_size', 0, 1) |
||
125 | _validate_input_range(test_size, 'test_size', 0, 1) |
||
126 | |||
127 | target_data = [] |
||
128 | if isinstance(target, str): |
||
129 | target_data = data[target] |
||
130 | data = data.drop(target, axis=1) |
||
131 | |||
132 | elif isinstance(target, (list, pd.Series, np.ndarray)): |
||
133 | target_data = pd.Series(target) |
||
134 | target = target.name |
||
135 | |||
136 | X_train, X_dev_test, y_train, y_dev_test = train_test_split(data, target_data, |
||
137 | test_size=dev_size+test_size, |
||
138 | random_state=random_state, |
||
139 | stratify=stratify) |
||
140 | |||
141 | if (dev_size == 0) or (test_size == 0): |
||
142 | return X_train, X_dev_test, y_train, y_dev_test |
||
143 | |||
144 | else: |
||
145 | X_dev, X_test, y_dev, y_test = train_test_split(X_dev_test, y_dev_test, |
||
146 | test_size=test_size/(dev_size+test_size), |
||
147 | random_state=random_state, |
||
148 | stratify=y_dev_test) |
||
149 | return X_train, X_dev, X_test, y_train, y_dev, y_test |
||
150 |