小编dd.*_*dd.的帖子

将 Scikit-Learn OneHotEncoder 与 Pandas DataFrame 结合使用

我正在尝试使用 Scikit-Learn 的 OneHotEncoder 将 Pandas DataFrame 中包含字符串的列替换为单热编码的等效项。我下面的代码不起作用:

from sklearn.preprocessing import OneHotEncoder
# data is a Pandas DataFrame

jobs_encoder = OneHotEncoder()
jobs_encoder.fit(data['Profession'].unique().reshape(1, -1))
data['Profession'] = jobs_encoder.transform(data['Profession'].to_numpy().reshape(-1, 1))
Run Code Online (Sandbox Code Playgroud)

它产生以下错误(列表中的字符串被省略):

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-91-3a1f568322f5> in <module>()
      3 jobs_encoder = OneHotEncoder()
      4 jobs_encoder.fit(data['Profession'].unique().reshape(1, -1))
----> 5 data['Profession'] = jobs_encoder.transform(data['Profession'].to_numpy().reshape(-1, 1))

/usr/local/anaconda3/envs/ml/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py in transform(self, X)
    730                                        copy=True)
    731         else:
--> 732             return self._transform_new(X)
    733 
    734     def inverse_transform(self, X):

/usr/local/anaconda3/envs/ml/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py in _transform_new(self, X)
    678         """New implementation assuming categorical input"""
    679         # …
Run Code Online (Sandbox Code Playgroud)

python machine-learning pandas scikit-learn one-hot-encoding

12
推荐指数
3
解决办法
3万
查看次数

geopy.exc.GeocoderAuthenticationFailure:HTTP 错误 401:未经授权

我正在尝试使用 GeoPy 和 Python 来获取某个位置的纬度和经度。我尝试实现这个问题,如下:

from geopy.geocoders import GeoNames
gn = GeoNames(username='[my username]')
gn.geocode("Cleveland, OH 44106")
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

geopy.exc.GeocoderAuthenticationFailure:HTTP 错误 401:未经授权

我在http://www.geonames.org上注册,我也尝试查看这个问题,但我得到同样的错误,除非必须在http://www.geonames.orguser_agent的某个地方指定?

我究竟做错了什么?

python geopy geonames

3
推荐指数
1
解决办法
1521
查看次数