Python Forum
Error: ValueError: could not convert string to float: 'L200 1.6 D/C'
Poll: Since When Do you use Python?
You do not have permission to vote in this poll.
I am new on Python
100.00%
1 100.00%
More than 2 years
0%
0 0%
Total 1 vote(s) 100%
* You voted for this item. [Show Results]

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error: ValueError: could not convert string to float: 'L200 1.6 D/C'
#1
Hi guys, I need help trying to use Python to predict if a customer is going to leave the company.

When I try to fit the Random Forest, it says a ValueError.

I let here the csv and the code. (the csv file only has 5 rows but the real BD has 425.000 rows)

https://www.dropbox.com/s/n2y8m131uv3kkt...a.csv?dl=0 (Download CSV from Dropbox)

Please help me.

Regards

'Importa libreria
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.cross_validation import train_test_split


'Importa libreria de graficos
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
matplotlib.rcParams['savefig.dpi'] = 144

'Importa archivo para modelo
data_fuga= pd.read_csv(r"\\MANQUEHUE\Qlikview\CSV\BD_Fuga.csv", encoding='utf-8-sig')
print(data_fuga.head())
'CREACION DE NUEVO CAMPO CON 0: PERMANECE 1: FUGA
data_fuga['Clase']=(data_fuga['ESTADO']=='Fuga')
print(data_fuga.head())
data_fuga['Clase']=data_fuga['Clase'].astype(int)
print(data_fuga.head())

'ELIMINA VARIABLE ESTADO PARA DEJAR SOLO NUMERICO
data_fuga = data_fuga.drop('ESTADO',1)
data_fuga = data_fuga.drop(['ESTADO','Comuna','VEHMARK','VEHMODEL','FECHA_INI_VIGENCIA','FECHA_FIN_VIGENCIA','FECHA_ANULACION','FECHA_HOY','PRIMERA_CUOTA','EDAD_GROUP'],1)
print(data_fuga.head())

'Nombre de la variable a predecir
Clasificacion_Estado = 'Clase'

'Asignacion de nombres del campo a variable headers
headers = data_fuga.columns.values.tolist()
print(headers)

'Elimina el label a predecir
headers.remove(Clasificacion_Estado)
print(headers)

'Carga en variable Clasificacion la lista completa de datos que posee ESTADO
Clasificacion = data_fuga[Clasificacion_Estado].tolist() 
print(Clasificacion)
'Conversion a Array
Clasificacion = np.array(Clasificacion) 
print(Clasificacion)

'Entrega el numero de filas y variables de la base de datos
print('Numero de filas: %d \n Numero de variables: %d' % data_fuga.shape) 
'Muestra los datos
data_fuga.head() 
'Analisis de datos, entrega informacion como min o media.
data_fuga.describe() 
'Cantidad de fugas vs permanece
print('Cantidad por cada clase (0: Permanece, 1: Fuga):')
print(data_fuga['Clase'].value_counts())

'Porcentaje por cada clase
print(data_fuga['Clase'].value_counts('Fuga')*100)
'Cantidad de campos sin informacion
print('Numero de valores perdidos por columna:')
print(pd.isnull(data_fuga).sum(axis=0))




'agrupa por mes de vigencia la cantidad de Fugas
data_fuga.groupby('MES_INI_VIGENCIA').Clase.sum() 
'agrupa por deducible la cantidad de Fugas
data_fuga.groupby('DEDUCIBLE').Clase.sum()
'agrupa por comuna la cantidad de fugas
data_fuga.groupby('Comuna').Clase.sum()
'Cuenta la cantidad exacta de Fuga por comuna
data_fuga[data_fuga.Comuna=='SANTIAGO'].Clase.sum()
'Cuenta la cantidad de Fuga total
data_fuga['Clase'].sum()


'Conversion de tabla Dataframe(pandas) a Matrix (numpy) 
data_fuga = data_fuga.drop(Clasificacion_Estado,1)
data_fuga = data_fuga.as_matrix()
data_fuga = np.matrix(data_fuga)
print(np.matrix(data_fuga))

'ENTRENAMIENTO DE DATOS
x_data_fuga = data_fuga
y_data_fuga = Clasificacion
print(data_fuga)
print(Clasificacion)

'DIVISION DE LA DATA 80% ENTRENAMIENTO 20% TEST
x_train, x_test, y_train, y_test = train_test_split(x_data_fuga, y_data_fuga, test_size = 0.2)

'Random Forest
rf=RandomForestClassifier(n_estimators=100)
rf.fit(x_train,y_train)

y_test

pred = rf.predict(x_test)
S= y_test.values

for i in range(len(pred)):
    if pred[i]=S[i]:
        count = count + 1
count
len(pred)
Acc = len(pred)/count*100
print('Precision: %' + Acc)


var_imp = pd.DataFrame({'feature':headers,'v_importance':modelo.feature_importances_.tolist()})
print var_imp.sort_values(by = 'v_importance', ascending = False)
Reply
#2
Not sure what the purpose of lines like 1, 8, 15, etc (those that start with a single quote " ' "), if they are meant to be comments, that is not the proper way to create a comment in Python.

Please post the error code, in it's entirety (between the error code tags).
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Sparkz_alot thanks for the correction. You right I was using the "'" to comment like VBA, but It has to be #. I fix it.

Posted the error code.

Thanks for your time

#Random Forest

rf=RandomForestClassifier(n_estimators=100)

rf.fit(x_train,y_train)
Traceback (most recent call last):

  File "<ipython-input-77-12afa76ba01c>", line 1, in <module>
    rf.fit(x_train,y_train)

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py", line 247, in fit
    X = check_array(X, accept_sparse="csc", dtype=DTYPE)

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 433, in check_array
    array = np.array(array, dtype=dtype, order=order, copy=copy)

ValueError: could not convert string to float: 'YARIS SPORT'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Indirectlty convert string to float in JSON file WBPYTHON 6 600 May-06-2020, 12:09 PM
Last Post: WBPYTHON
  ValueError: could not convert string to float RahulSingh 3 932 Apr-09-2020, 02:59 PM
Last Post: dinesh
  convert a list of string+bytes into a list of strings (python 3) pacscaloupsu 4 3,406 Mar-17-2020, 07:21 AM
Last Post: markfilan
  Convert dataframe string column to numeric in Python darpInd 1 540 Mar-14-2020, 10:07 AM
Last Post: ndc85430
  convert 'A B C' to numpy float matrix rezabma 4 458 Feb-27-2020, 09:48 AM
Last Post: rezabma
  ValueError: could not convert string to float: '4 AVENUE' Kudzo 4 1,442 Jan-26-2020, 10:47 PM
Last Post: Kudzo
  Convert 'object' to 'string' AdWill97 1 39,743 May-06-2019, 08:22 AM
Last Post: Yoriz
  ValueError: could not convert the string to float Grin 3 7,469 Jun-14-2018, 08:17 PM
Last Post: killerrex
  Problema with convert image to string karlo123 1 1,092 May-16-2018, 10:44 PM
Last Post: karlo123
  Error for convert image PIL to CV2 karlo123 2 2,428 May-07-2018, 03:14 AM
Last Post: karlo123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020