首页 > 其他分享 >One Hot Encoding

One Hot Encoding

时间:2023-04-05 23:44:09浏览次数:35  
标签:town categorical OneHotEncoder Encoding Hot dfle import sklearn

One Hot Encoding

one method converting categorical variables to convenient variables (e.g. 0-1) using dummy variables

Pandas

Get dummy columns

dummies = pd.get_dummies(df.town)

merged = pd.concat([df, dummies], axis='columns')

Drop one of the variables

防止变量出现完全共线性情况使参数无法估计

final = merged.drop(['town', 'west windsor'], axis='columns')

Sklearn

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()

dfle = df
dfle.town = le.fit_transform(dfle.town)

X = dfle[['town', 'area']].values
y = dfle.price
from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder(categorical_features=[0])

"""
报错如下:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [28], line 2
      1 from sklearn.preprocessing import OneHotEncoder
----> 2 ohe = OneHotEncoder(categorical_features=[0])

TypeError: __init__() got an unexpected keyword argument 'categorical_features'

原因:新版sklearn删去了"categorical_features"参数

解决:from sklearn.compose import ColumnTransformer
"""
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
ohe = ColumnTransformer([('encoder', OneHotEncoder(), [0])], remainder='passthrough')

X = ohe.fit_transform(X)

X = X[:,1:] # Take all the rows and drop 0th column

标签:town,categorical,OneHotEncoder,Encoding,Hot,dfle,import,sklearn
From: https://www.cnblogs.com/POLAYOR/p/17291317.html

相关文章

  • mac global hotkey
    https://www.computerhope.com/issues/ch002051.htmInmacOS,therearemanywaystolaunchanapplication.YoucanclickaniconintheDockortheLaunchpad,ortypethenameoftheappintheSpotlightsearchbox.However,inmacOS,thereisnobuilt-inw......
  • Hot Start Up (easy version) CF1799
    你有两个CPU,n个程序(m个类型)要运行。在不同条件下程序运行的时间不同,但连续运行的时间满足小于等于在不连续状态下运行的时间。  #include<iostream>#include<cstring>#include<queue>usingnamespacestd;constintN=5002;#defineintlonglong#definei......
  • 【AutoHotkey】笔记本键盘没有Home键和End键的解决方案
    problem笔记本键盘没有小键盘,所以少了Home键,End键等一系列键。编辑文本的时候就十分不方便solution可以创建键盘快捷键(键盘映射)(找一组平时用不到的热键)把ctrl+[映射为Home把ctrl+]映射为Endcodes软件可以用Autohotkey,配置code.ahk如下(拖入下载的软件即可运行)^[::Home^]::End#......
  • SyntaxError: Non-UTF-8 code starting with ‘\xb2‘ in file xxx.py but no encodi
    openCV系列文章目录文章目录openCV系列文章目录前言一、问题原因二、解决办法1.点击“运行按钮”->RunPythonfile前言#coding=gbkimportcv2importnumpyasnpdefmouse_callback(event,x,y,flags,userData):print(event,x,y,flags,userData)#mouse_callb......
  • [USACO08FEB]Hotel G
    [USACO08FEB]HotelG线段树二分,最大字段和对于操作二,是很简单的区间赋值对于操作一,长度为\(len\)的,我们要找到最小的的\(x\)满足\([x,x+len-1]\)的房间为空在最大字段和的基础上,我们可以求出最长连续空房间的长度,对于要求长度为\(len\)的房间,可以按顺序判断:若左区......
  • [论文阅读] Diff-Font: Diffusion Model for Robust One-Shot Font Generation
    pretitle:Diff-Font:DiffusionModelforRobustOne-ShotFontGenerationaccepted:arxiv2022paper:https://arxiv.org/abs/2212.05895code:noneref:https://www.zhihu.com/question/545764550关键词:one-shot,字体生成,扩散模型阅读理由:扩散模型在字体这边的第一次应......
  • idea子项目打jar包错误 Could not find artifact xxx:pom:1.0-SNAPSHOT 解决办法
    idea子项目打jar包错误Couldnotfindartifactxxx:pom:1.0-SNAPSHOT解决办法原文链接:https://blog.csdn.net/a459471027/article/details/124195296项目结构如下:......
  • 【AutoHotkey】一种适合敲代码&&可以用左手完成大部分功能的组合键设计
    故事一使用键盘,需要两只手;使用鼠标,还需要一只手;总共三只手。而你,只有两只手所以,你需要第三只手(bushi)所以,通常是左手使用键盘,而右手控制鼠标。但Enter、BackSpace等......
  • Detecting novel systemic biomarkers in external eye photos
    FRIDAY,MARCH24,2023PostedbyBorisBabenko,SoftwareEngineer,andAkibUddin,ProductManager,GoogleResearch Lastyearwepresented results demo......
  • 【Visual Leak Detector】配置项 ReportEncoding
    说明使用VLD内存泄漏检测工具辅助开发时整理的学习笔记。本篇介绍VLD配置文件中配置项ReportEncoding的使用方法。同系列文章目录可见《内存泄漏检测工具》目录......