AnswerBun.com

Pandas :TypeError: float() argument must be a string or a number, not 'pandas._libs.interval.Interval'

I am trying to do the machine learning practice problem of Heart disease , dataset from kaggle.
Then i tried to split data into train set and test set and after that combing models into single function and predicting,this error shows up in jupyter notebook .

Here’s my code:

# Split data into X and y
X = df.drop("target", axis=1)
y = df["target"]

Spliting

# Split data into train and test sets
np.random.seed(42)

# Split into train & test set
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)

Prediction function

    # Put models in a dictionary
models = {"Logistic Regression": LogisticRegression(),
          "KNN": KNeighborsClassifier(),
          "Random Forest": RandomForestClassifier()}

# Create a function to fit and score models
def fit_and_score(models, X_train, X_test, y_train, y_test):
    """
    Fits and evaluates given machine learning models.
    models : a dict of differetn Scikit-Learn machine learning models
    X_train : training data (no labels)
    X_test : testing data (no labels)
    y_train : training labels
    y_test : test labels
    """
    # Set random seed
    np.random.seed(42)
    # Make a dictionary to keep model scores
    model_scores = {}
    # Loop through models
    for name, model in models.items():
        # Fit the model to the data
        model.fit(X_train, y_train)
        # Evaluate the model and append its score to model_scores
        model_scores[name] = model.score(X_test, y_test)
    return model_scores

And when i run this code , that error shows up

    model_scores = fit_and_score(models=models,
                             X_train=X_train,
                             X_test=X_test,
                             y_train=y_train,
                             y_test=y_test)

model_scores

This is error
enter image description here

Stack Overflow Asked by himanshu040 on February 19, 2021

1 Answers

One Answer

Your X_train, y_train, or both, seem to have entries that are not float numbers.

At some point in the code, try using

X_train = X_train.astype(float)
y_train = y_train.astype(float)
X_test = X_test.astype(float)
y_test = y_test.astype(float)

Either this will work and the error will go away, or one of the conversions will fail, at which point you will need to decide how (or if) you want to encode the data as a float.

Answered by Ami Tavory on February 19, 2021

Add your own answers!

Related Questions

Keycloak permission to restrict account based resources

0  Asked on December 26, 2020 by james-lin

 

CSS flex, full height sidebar inside a modal?

1  Asked on December 25, 2020 by ddulla

   

Cant loop through List and display in DataTable

1  Asked on December 25, 2020 by finchy70

 

Automate and looping through batch script

2  Asked on December 25, 2020 by nck_505

       

issue connecting Heroku PHP stack to Redis using Predis

0  Asked on December 25, 2020 by rob-edlin

       

Splitting C linked list without making a copy

2  Asked on December 24, 2020

 

Notice Messages Do Not Appear On The Page

1  Asked on December 24, 2020 by cifhai

 

Making comparisons in a HTTPS GET request in node js

0  Asked on December 24, 2020 by thor_

     

SwiftUI Horizontal ScrollView does not scroll

1  Asked on December 23, 2020 by hillinharwich

     

Calling a tab fragment method inside the parent acitivity

0  Asked on December 23, 2020 by aaron-waller

       

Hiding table rows works, but fading out does not

1  Asked on December 23, 2020 by diamondjoe12

       

How to list kubernetes services in k9s?

1  Asked on December 23, 2020 by matthias-m

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.