1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to solve ValueError: could not convert string to float in python

Discussion in 'Programming/Internet' started by Waseem Randhawa, Sep 12, 2018.

  1. I have text file of dataset which look like this.

    2.8858451e-001 -2.0294171e-002 -1.3290514e-001 -9.9527860e-001 -9.8311061e-001

    When I had read this file and fit classifier on this data I get this error.

    ValueError: could not convert string to float: ' 2.3177399e-001 4.9371571e-003

    I'm using the following code:

    tra_x, tra_y, tes_x, tes_y = np.array([]), np.array([]), np.array([]),np.array([])

    file = open('train\X_train.txt', encoding='utf-8')
    tra_x = file.readlines()
    tra_x = [float(x).split(" ") for x in tra_x]

    file = open('train\y_train.txt', encoding='utf-8')
    tra_y = file.readlines()

    file = open('test\X_test.txt', encoding='utf-8')
    tes_x = file.readlines()

    file = open('test\y_test.txt', encoding='utf-8')
    tes_y = file.readlines()

    Login To add answer/comment

Share This Page