| @@ 47-63 (lines=17) @@ | ||
| 44 | yaml.add_representer(literal_str, represent_literal_str) |
|
| 45 | ||
| 46 | ||
| 47 | def normalize(metric_type, X): |
|
| 48 | """ |
|
| 49 | Normalize the vectors. |
|
| 50 | ||
| 51 | If type equals ip, using sklearn.preprocessing.normalize to convert it |
|
| 52 | """ |
|
| 53 | if metric_type == "ip": |
|
| 54 | logger.info("Set normalize for metric_type: %s" % metric_type) |
|
| 55 | X = sklearn.preprocessing.normalize(X, axis=1, norm='l2') |
|
| 56 | X = X.tolist() |
|
| 57 | elif metric_type in ["jaccard", "hamming", "sub", "super"]: |
|
| 58 | tmp = [] |
|
| 59 | for _, item in enumerate(X): |
|
| 60 | new_vector = bytes(np.packbits(item, axis=-1).tolist()) |
|
| 61 | tmp.append(new_vector) |
|
| 62 | X = tmp |
|
| 63 | return X |
|
| 64 | ||
| 65 | ||
| 66 | def get_unique_name(prefix=None): |
|
| @@ 97-110 (lines=14) @@ | ||
| 94 | """ |
|
| 95 | pass |
|
| 96 | ||
| 97 | def normalize(self, metric_type, X): |
|
| 98 | if metric_type == "ip": |
|
| 99 | logger.info("Set normalize for metric_type: %s" % metric_type) |
|
| 100 | X = sklearn.preprocessing.normalize(X, axis=1, norm='l2') |
|
| 101 | X = X.astype(np.float32) |
|
| 102 | elif metric_type == "l2": |
|
| 103 | X = X.astype(np.float32) |
|
| 104 | elif metric_type in ["jaccard", "hamming", "sub", "super"]: |
|
| 105 | tmp = [] |
|
| 106 | for _, item in enumerate(X): |
|
| 107 | new_vector = bytes(np.packbits(item, axis=-1).tolist()) |
|
| 108 | tmp.append(new_vector) |
|
| 109 | X = tmp |
|
| 110 | return X |
|
| 111 | ||
| 112 | def generate_combinations(self, args): |
|
| 113 | if isinstance(args, list): |
|