pythonfunctionmachine-learningtypeerrorprediction

How to solve machine learning error related to data science which exists in function which has been created


I'm trying to execute code which is created is about the salary prediction while cleaning the data in my machine learning model...I am unable to overcome the following code error

Error which is faced:

TypeError                                 Traceback (most recent call last)
Cell In[440], line 8
      5         return 0.5
      6     return float(x)
----> 8 df['YearsCodePro'] = df['YearsCodePro'].apply(clean_experience)

Cell In[441], line 6, in clean_experience(x)
      4 if x == 'Less than 1 year':
      5     return 0.5
----> 6 return float(x)

TypeError: float() argument must be a string or a real number, not 'NoneType'

This code is been created for executing a function for machine learning model in python

def clean_experience(x):
    if x == 'More Than 50 years':
        return 50
    if x == 'Less than 1 year':
        return 0.5
    return float(x)
    
df['YearsCodePro'] = df['YearsCodePro'].apply(clean_experience)

Expected output is execute above function without passing an error


Solution

  • It is hard to answer without seeing the actual dataset, but error says that you have 'x' which is None (probably you have some empty fields in your data).

    Try to handle this with additional 'if' block:

    def clean_experience(x):
        if x == 'More Than 50 years':
            return 50
        if x == 'Less than 1 year':
            return 0.5
        if x is None:
            # do something
        return float(x)
    

    Also you should consider other cases where x is neither 'More Than 50 years' or 'Less than 1 year' and cannot be converted to float.