How to get rid of ValueError: could not convert string to float 'Enrolled' in Google Colab?

huangapple go评论100阅读模式
英文:

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&#39'

我不断收到数值错误:无法将字符串转换为浮点数:“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?

huangapple
  • 本文由 发表于 2023年3月9日 22:47:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定