简体   繁体   English

无法修复我在编写代码以分析 O'Reilly 书中的“California Housing”数据集时遇到的错误

[英]Not able to fix the error I get while writing code for analyzing "California Housing" data set from O'Reilly book

while executing code:(from book page 69 of "Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems Book by Aurelien Geron")执行代码时:(摘自《使用 Scikit-Learn、Keras 和 TensorFlow:构建智能系统的概念、工具和技术》一书的第 69 页)

housing_cat_encoded = ordinal_encoder.fit_transform(housing_cat)

housing_cat_encoded[:10]

I get error:-我收到错误:-

ValueError: Expected 2D array, got 1D array instead:
array=['<1H OCEAN' '<1H OCEAN' 'NEAR OCEAN' ... 'INLAND' '<1H OCEAN' 'NEAR BAY'].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

How do I fix it?我如何解决它?

In this case the error describes the issue and a way to solve it.在这种情况下,错误描述了问题和解决方法。 The function .fit_transform() is expecting a 2D array, not a 1D one. function .fit_transform()需要一个二维数组,而不是一维数组。 A way of achieving this, is using .reshape() .实现这一点的一种方法是使用.reshape() Since we are passing a single column (feature) then we should use -1,1 .由于我们传递的是单列(特征),所以我们应该使用-1,1

housing_cat_encoded = ordinal_encoder.fit_transform(housing_cat.reshape(-1,1))

If housing_cat is a pandas series, then you might have to use:如果housing_cat是 pandas 系列,那么您可能必须使用:

housing_cat_encoded = ordinal_encoder.fit_transform(housing_cat.values.reshape(-1,1))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 这是一个 function 可以反转加州住房数据集中的坐标,从而得到具体地址。 但是我有一个问题, - This is a function to be able to reverse a coordinate in the california housing dataset so as to get the specific address. But I have a problem, 加州住房数据的神经网络 - Neural Network with California Housing Data 住房数据集无法从“动手机器学习”中加载 - Housing Data Set Not Able to Load From 'Hands-On Machine Learning' NameError:名称“x1”未在加利福尼亚住房数据中定义 - NameError: name 'x1' is not defined in California housing data 写入 SQL 数据库时出现 I/O 错误 - I/O error while writing to an SQL database ImportError:Toby Segaram第4章“编程集体智慧O Reilly”中没有名为pysqlite2代码的模块 - ImportError: No module named pysqlite2 code from Programming Collective intelligence O Reilly by Toby Segaram chapter 4 如何使用Selenium获取房屋数据 - How to use Selenium to get the housing data 获取 IOError:[Errno 121] 尝试通过 I2C 从 Arduino 获取数据时,python(覆盆子)上的 smbus 出现远程 I/O 错误 - Getting IOError: [Errno 121] Remote I/O error with smbus on python (raspberry) while trying to get data over I2C from Arduino 我在编写关于运动规划的代码时遇到了这个错误。 如何摆脱它? - I had this error while writing a code on motion planning. How to get rif of it? 使用此代码,我可以从第一个 url 获得作者和书名列表?! 如何使用beautifulsoup 抓取多个url 数据? - with this code i could get list of author and book title from first url!! how to crawl multiple urls data using beautifulsoup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM