机器学习时出现 could not convert string to float:‘xxx‘解决方法

这篇具有很好参考价值的文章主要介绍了机器学习时出现 could not convert string to float:‘xxx‘解决方法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

先放结论:数据未进行One hot code

解决方法:使用这个函数pd.get_dummies()对数据进行处理

案例:

#直接对信息进行归一化、标准化或机器学习

from sklearn.neighbors import KNeighborsClassifier
knn =  KNeighborsClassifier()
#训练数据
knn.fit(x_train,y_train)

机器学习时出现 could not convert string to float:‘xxx‘解决方法

 因为:

机器学习时出现 could not convert string to float:‘xxx‘解决方法

 

 因为类型不能转换为float等数字类型,不是数字直接进行机器学习是不行的,同理直接进行归一化、标准化同样不行。报错相同。

 加入函数

features = pd.get_dummies(features)

#再进行学习或数据预处理

features_temp = StandardScaler().fit_transform(features)#去均值和方差归一化
knn.fit(x_train,y_train)#训练数据

不报错。

看看One_hot_encode化处理前后的数据:

处理前

机器学习时出现 could not convert string to float:‘xxx‘解决方法

 处理后

机器学习时出现 could not convert string to float:‘xxx‘解决方法

 

第一次写博客,很混乱,欢迎得到批评和指正文章来源地址https://www.toymoban.com/news/detail-504604.html

到了这里,关于机器学习时出现 could not convert string to float:‘xxx‘解决方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Git 拉取代码提示 unable to access ‘http://xxx.xxx.com/xxx/xxx.git/: Could not resolve host 异常解决

    在开发过程中,会遇到git拉取提交操作。最近拉取主分支代码的时候,提示拉取失败 提示如下: 出现这个问题,一般是域名对应IP发生变化,git拉取时访问不到造成的。 两种解决方法: 第一种: 本地 host文件 域名对应的IP 替换成新的。 host文件地址:C:WindowsSystem32drivers

    2024年02月05日
    浏览(43)
  • Python ERROR: Could not install packages due to an OSError:XXX解决方法

    在使用 pip 安装 python 包时,出现ERROR: Could not install packages due to an OSError: XXX 的错误,这时候主要有两种错误类型,要根据类型来解决问题 错误类型1: 特点:[WinError 5] 拒绝访问 + … + Consider using the --user option or check the permissions 错误类型2: 特点:[Errno 22] Invalid argument: + … 我原

    2024年01月25日
    浏览(49)
  • Proxy error: Could not proxy request xxx from localhost:8080 to http://localhost:3000(ECONNREFUSED)

    在 mock 数据的时候,配置 vue.config.js 里的 devServer 的 proxy 时报错,错误如下 我的配置如下: 我们可以将 target 端口 3000 改成跟起的服务一样 http://localhost:8080 ,都是 8080 端口即可。 ECONNREFUSED 的意思就是:ECONNREFUSED(连接被拒绝):无法建立连接,因为目标机器主动拒绝了它

    2024年02月16日
    浏览(36)
  • Proxy error: Could not proxy request /XXX localhost to http://localhost:8080/ (ECONNRE)报错处理

    今天在进行前后端联调时,联调不通,前端出现如下报错 通过在网上查找相关问题有很多种原因,因此博主根据资料进行相关解决方法的整理。 修改vue.config.js中的代理port 修改正确的地址或端口 博主是这个问题导致,一开始前端代理端口配置是8080,后端配置是8084,因此启

    2024年02月11日
    浏览(38)
  • Proxy error: Could not proxy request xxx from localhost:8080 to http://localhost:3000(ECONNREFUSED)关

    将代理规则从 /api 修改为 @(/api) 意味着使用前缀 @ 来标识代理规则。这种方式相对于直接使用路径的方式,可以提高代理规则的可读性和可维护性,并且它还支持更复杂的代理规则设置和管理,例如,可以设置多个代理规则,对不同的 API 接口进行不同的转发设置。 下面是将

    2024年02月04日
    浏览(38)
  • IDEA项目启动报错:Failed to execute goal on project xxx: Could not resolve dependencies for project

    [ERROR] Failed to execute goal on project xxx: Could not resolve dependencies for project Failed to collect dependencies at xxx .xxx-service:jar:dev: Failed to read artifact descriptor for xxxx/maven-snapshots/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of nexus-snaps

    2024年02月03日
    浏览(63)
  • vue3警告Extraneous non-emits event listeners (XXX) were passed to component but could not be automatic

    vue3 警告Extraneous non-emits event listeners (selectMeth) were passed to component but could not be automatically inherited because component renders fragment or text root nodes. If the listener is intended to be a component custom event listener only, declare it using the “emits” option. 之所以出现这个警告,是因为在子组件向父组件发

    2024年02月09日
    浏览(38)
  • kvm not all arguments converted during string

    kylin virt-manager 远程镜像制作问题记录(not all arguments ) 服务器端安装的OS版本:Kylin-Server-10-SP1-Release-Build20-20210518-arm64-2021-05-18 客户端安装的OS版本:Kylin-Server-10-SP1-Release-Build20-20210518-x86_64-2021-05-18 客户端通过virt-manager镜像制作工具远程连接到服务器端,进行镜像制作,生成新虚

    2024年02月13日
    浏览(27)
  • 载入qss时出现Could not parse application stylesheet

    我这里其实qss文件本身没有错误。 参考:解决Qt Creator修改qss文件后导致样式无效问题_qt qss改变但运行结果没变_风吹沙走的博客-CSDN博客 我的解决方法: (1)UTF-8 BOM:总是删除 (2) 文本重新编码为ANSI 这时候中文会变成乱码。  (3)我事先复制了一个副本,然后把副本中的内容复制

    2024年02月10日
    浏览(26)
  • 解决Failed to convert value of type ‘java.lang.String‘ to required type ‘java.lang.Integer

    项目:网上商城练习 问题:使用postman测试接口报错:类型转换异常 上代码: 改为: 直接去掉{}和@PathVariable注释,容易找不到对应的参数类型,希望对大家有用,问题已解决。

    2024年02月11日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包