英文:
How to get rid of ValueError: could not convert string to float 'Enrolled' in Google Colab?
问题
I keep on getting ValueError: could not convert string to float: 'Enrolled''
我不断收到数值错误:无法将字符串转换为浮点数:“Enrolled”
I was given a dataset and was asked to perform 5 machine algorithms. As per my previous question, only 2 of them successfully worked. (Thank you for the help!)
我得到了一个数据集,并被要求执行5个机器算法。根据我之前的问题,只有其中2个成功运行了。(感谢您的帮助!)
Here is my code:
以下是我的代码:
from sklearn.ensemble import RandomForestRegressor
random_forest = RandomForestRegressor(n_estimators=100,random_state=0)
random_forest.fit(X_train, y_train)
try:
Y_prediction = random_forest.predict(X_test)
random_forest.score(X_train, y_train)
acc_random_forest = round(random_forest.score(X_train, y_train) * 100, 2)
print(acc_random_forest)
except ValueError:
pass
英文:
I keep on getting ValueError: could not convert string to float: 'Enrolled'
I was given a dataset and was asked to perform 5 machine algorithms. As per my previous question, only 2 of them successfully worked. (Thank you for the help!)
Here is my code:
from sklearn.ensemble import RandomForestRegressor
random_forest = RandomForestRegressor(n_estimators=100,random_state=0)
random_forest.fit(X_train, y_train)
try:
Y_prediction = random_forest.predict(X_test)
random_forest.score(X_train, y_train)
acc_random_forest = round(random_forest.score(X_train, y_train) * 100, 2)
print(acc_random_forest)
except ValueError:
pass
答案1
得分: 0
Your error states that you are trying to cast the string "Enrolled"
to a float. Here's a reproduction:
>>> float("Enrolled")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'Enrolled'
I do not know the lib you're using but the doc for predict()
says
>Parameters:
> X{array-like, sparse matrix} of shape (n_samples, n_features)
> The input samples. Internally, its dtype will be converted to dtype=np.float32. If a sparse matrix is provided, it will be converted into a sparse csr_matrix.
Emphasis mine
So, given the fact that you seem to be working with train-related data, and that this functions tries to cast stuff to float, and the string being Enrolled I guess you sort of passed a table header string with your data to your model or something like that?
英文:
Your error states that you are trying to cast the string "Enrolled"
to a float. Here's a reproduction:
>>> float("Enrolled")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'Enrolled'
I do not know the lib you're using but the doc for predict()
says
>Parameters:
> X{array-like, sparse matrix} of shape (n_samples, n_features)
> The input samples. Internally, its dtype will be converted to dtype=np.float32. If a sparse matrix is provided, it will be converted into a sparse csr_matrix.
Emphasis mine
So, given the fact that you seem to be working with train-related data, and that this functions tries to cast stuff to float, and the string being Enrolled I guess you sort of passed a table header string with your data to your model or something like that?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论