Search code examples
python-3.xscikit-learnlogistic-regressionsklearn-pandas

Logistic Regression SKLEARN could not convert string to float: 'DailyReturns'


Trying to run Logistic regression but I am getting this error could not convert string to float: 'DailyReturns' I have checked my data DailyReturns is the column name.

Also: apple['DailyReturns'].dtype gives dtype('float64') and apple['_lortrain'].dtype gives dtype('float64')

Error:

ValueError: could not convert string to float: 'DailyReturns'

Code:

_lortrain = np.sign(apple['DailyReturns'])
apple['_lortrain'] = _lortrain
_data_train, _data_test,  = train_test_split(apple,test_size = 0.2)
X_train = _data_train.columns[4:9].values.reshape(-1,1)
y_train = _data_train['_lortrain'].values.reshape(-1,1)
#X_train = X_train.apply(pd.to_numeric, errors='coerce')
#y_train = y_train.apply(pd.to_numeric, errors='coerce')
X_test = _data_test.columns[4:9].values.reshape(-1,1)
y_test = _data_test['_lortrain'].values.reshape(-1,1)
_LogR = lor(C = 1e6, penalty='l2', tol=0.01, solver='saga')
_LogR.fit(X_train,y_train)
_logr = _LogR.predict(X_test)

Data:

apple.columns[4:9] :

Index(['DailyReturns', 'Lag_Returns_1', 'Lag_Returns_2', 'Lag_Returns_3',
       'Lag_Returns_4'],
      dtype='object')

Close   DailyReturns    Lag_Returns_1   Lag_Returns_2   Lag_Returns_3   Lag_Returns_4   Lag_Returns_5
Date                            
1980-12-22  0.53    0.058269    0.040822    0.042560    0.021979    -0.085158   -0.040005
1980-12-23  0.55    0.037041    0.058269    0.040822    0.042560    0.021979    -0.085158
1980-12-24  0.58    0.053110    0.037041    0.058269    0.040822    0.042560    0.021979
1980-12-26  0.63    0.082692    0.053110    0.037041    0.058269    0.040822    0.042560
1980-12-29  0.64    0.015748    0.082692    0.053110    0.037041    0.058269    0.040822

Solution

  • _data_train.columns[4:9].values is a list of column names (which are strings), not column data. You should use _data_train.iloc[:,4:9].values. Or, better yet:

    X_train = _data_train.iloc[:,4:9] # No values or reshape