1

I have a function to convert Ip address to integer value:

def convertIpToInt(ip):
    return sum([int(ipField) << 8*index for index, ipField in enumerate(reversed(ip.split('.')))])

convertIpToInt('149.170.10.1') -> 2510948865

which works fine and gives an integer value, but how to use these values in the classification model? Should I Scale these values, or what could be other options to handle this type of data?

5
  • normalization: datascience.stackexchange.com/questions/5885/… Commented Feb 27, 2020 at 4:43
  • Will that be okay to normalize the IP address values? Commented Feb 27, 2020 at 5:01
  • almost everything has already solution: stackoverflow.com/questions/9810198/normalizing-ip-addresses Commented Feb 27, 2020 at 5:03
  • Thanks but That is not the answer I'm looking for! Commented Feb 27, 2020 at 7:17
  • from ML perspective, converting ip addresses to numbers is a very bad idea. ip addresses are discrete and for any two ip addresses, ip1 > ip2 makes no sense. They can at best be represented as a set of discrete features but one hot encoding would give a very sparse representation, again overfitting the model. Try finding features that will help you represent ip addresses as discrete features without being sparse. for eg. Which country an ip belongs to from lite.ip2location.com/ip-address-ranges-by-country Commented Feb 27, 2020 at 13:13

1 Answer 1

1

You have to convert IP into identifiers for your machine learning algorithm, for this you can use Label Encoders. This will convert your IP into numbers.

from sklearn import preprocessing le = preprocessing.LabelEncoder() le.fit(["ip1", "ip2", "ip3", "ip4"]) le.transform(["ip1", "ip2", "ip1"])

Link: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html

If you think this can add weight to certain IP's(feature) then you can use One Hot encoding.

Link: https://machinelearningmastery.com/why-one-hot-encode-data-in-machine-learning/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.