Search code examples
pythonarrayspandasnumpyvalueerror

TypeError: float() argument must be a string or a number, not 'tuple' ,ValueError: setting an array element with a sequence


I'm calling this function to get predicted price for a car

def predict_price(car_model,condition,body,transmission,fuel,year,capacity,mileage):
    car_model_index = np.where(X.columns==car_model)[0][0]
    condtion_index = np.where(X.columns==condition)[0][0]
    body_index = np.where(X.columns==body)[0][0]
    trans_index = np.where(X.columns==transmission)[0][0]
    fuel_index = np.where(X.columns==fuel)[0][0]
    
    x = np.zeros(len(X.columns))
    x[0] = year,
    x[1] = capacity,
    x[2] = mileage,
    if car_model_index>0:
        x[car_model_index] = 1
    if condtion_index>0:
        x[condtion_index] = 1
    if body_index>0:
        x[body_index] = 1
    if trans_index>0:
        x[trans_index] = 1
    if fuel_index>0:
        x[fuel_index] = 1
    
    return lr.predict([x])[0]

predict_price('Toyota CHR','Used','SUV','Automatic','Petrol',2018,1200,26400)

Expecting to get the predicted price but getting an error like this, TypeError: float() argument must be a string or a number, not 'tuple' ,ValueError: setting an array element with a sequence.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: float() argument must be a string or a number, not 'tuple'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
<ipython-input-536-2e08ba5e0e1e> in <module>
----> 1 predict_price('Toyota CHR','Used','SUV','Automatic','Petrol',2018,1200,26400)

<ipython-input-525-e4e4a4d425fb> in predict_price(car_model, condition, body, transmission, fuel, year, capacity, mileage)
      7 
      8     x = np.zeros(len(X.columns))
----> 9     x[0] = year,
     10     x[1] = capacity,
     11     x[2] = mileage,

ValueError: setting an array element with a sequence.

Solution

  • AS @Priya suggested, you need to remove the , after year, capacity and mileage:

    def predict_price(car_model,condition,body,transmission,fuel,year,capacity,mileage):
        car_model_index = np.where(X.columns==car_model)[0][0]
        condtion_index = np.where(X.columns==condition)[0][0]
        body_index = np.where(X.columns==body)[0][0]
        trans_index = np.where(X.columns==transmission)[0][0]
        fuel_index = np.where(X.columns==fuel)[0][0]
        
        x = np.zeros(len(X.columns))
        x[0] = year  # here
        x[1] = capacity  # here
        x[2] = mileage  # and here
        if car_model_index>0:
            x[car_model_index] = 1
        if condtion_index>0:
            x[condtion_index] = 1
        if body_index>0:
            x[body_index] = 1
        if trans_index>0:
            x[trans_index] = 1
        if fuel_index>0:
            x[fuel_index] = 1
        
        return lr.predict([x])[0]
    
    predict_price('Toyota CHR','Used','SUV','Automatic','Petrol',2018,1200,26400)
    

    Indeed, the comma , is the symbol used to create a tuple:

    >>> year = 2018
    >>> foo = year
    >>> foo
    2018
    >>> type(foo)
    <class 'int'>
    >>> bar = year,
    >>> bar
    (2018,)
    >>> type(bar)
    <class 'tuple'>