Number transformer
GeoDistanceTransformer
¶
Bases: BaseTransformer
Calculates the distance in kilometers between two places on the earth using the geographic coordinates.
Example:
import pandas as pd
from sk_transformers import GeoDistanceTransformer
X = pd.DataFrame(
{
"lat_1": [48.353802, 51.289501, 53.63040161],
"long_1": [11.7861, 6.76678, 9.988229752],
"lat_2": [51.289501, 53.63040161, 48.353802],
"long_2": [6.76678, 9.988229752, 11.7861],
}
)
transformer = GeoDistanceTransformer([("lat_1", "long_1", "lat_2", "long_2")])
transformer.fit_transform(X)
lat_1 long_1 lat_2 long_2 distance_lat_1_lat_2
0 48.353802 11.78610 51.289501 6.76678 485.975293
1 51.289501 6.76678 53.630402 9.98823 339.730537
2 53.630402 9.98823 48.353802 11.78610 600.208154
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
List[Tuple[str, str, str, str]]
|
A list of tuples containing the names of four columns, which are coordinates of the two points in the following order: - latitude of point 1 - longitude of point 1 - latitude of point 2 - longitude of point 2 |
required |
Source code in src/sk_transformers/number_transformer.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
|
__distance_function(latitude_1, longitude_1, latitude_2, longitude_2)
staticmethod
¶
Calculates the distance (in kilometer) between two points on the earth.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
latitude_1 |
NDArray
|
Latitude of the first point. |
required |
longitude_1 |
NDArray
|
Longitude of the first point. |
required |
latitude_2 |
NDArray
|
Latitude of the second point. |
required |
longitude_2 |
NDArray
|
Longitude of the second point. |
required |
Returns:
Type | Description |
---|---|
NDArray
|
NDArray[numpy.float16]: Distance between the two points in kilometer. |
Source code in src/sk_transformers/number_transformer.py
transform(X)
¶
Adds new columns containing the distances between two points on the earth based on their geographical coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
Dataframe containing the coordinates of the two points as four columns. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: Dataframe containing the new columns. |
Source code in src/sk_transformers/number_transformer.py
MathExpressionTransformer
¶
Bases: BaseTransformer
Applies an function/operation to a column and a given value or column. The operation can be a function from NumPy's mathematical functions or operator package.
Warning! Some functions/operators may not work as expected. Especially, functions that don't
belong in numpy.ufunc
are not supported.
NumPy functions with return values that don't fit the size of the source column are also not supported.
Example:
import pandas as pd
from sk_transformers import MathExpressionTransformer
X = pd.DataFrame({"foo": [1, 2, 3], "bar": [4, 5, 6]})
transformer = MathExpressionTransformer([("foo", "np.add", "bar", None)])
transformer.fit_transform(X)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
List[str, str, Union[int, float]]
|
List of tuples containing the name of the column to apply the operation on,
a string representation of the operation (see list above) and the value to apply the operation on. The value can be
an number (int or float) or the name of another column in the dataframe. If the value is |
required |
Source code in src/sk_transformers/number_transformer.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
__abbreviate_numpy_in_operation(operation)
¶
Replaces numpy
at the start of a string with np
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
operation |
str
|
The operation as a string. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The operation as a string with numpy replaced with np. |
Source code in src/sk_transformers/number_transformer.py
transform(X)
¶
Applies the operation to the column and the value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
DataFrame containing the columns to apply the operation on. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: The original dataframe with the new columns. The new columns are named as follows: |
pd.DataFrame
|
' |