GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.w3schools.com — Python Machine Learning - Preprocessing - Categorical Data
❮
❯
Python TutorialPython HOME Python Intro Python Get Started Python Syntax Python Output Python Comments Python Variables
Python Variables
Variable Names
Assign Multiple Values
Output Variables
Global Variables
Variable Exercises
Code Challenge
Python Data Types
Python Numbers
Python Casting
Python Strings
Python Strings
Slicing Strings
Modify Strings
Concatenate Strings
Format Strings
Escape Characters
String Methods
String Exercises
Code Challenge
Python Booleans
Python Operators
Python Operators
Arithmetic Operators
Assignment Operators
Ternary Operator
Comparison Operators
Logical Operators
Identity Operators
Membership Operators
Bitwise Operators
Operator Precedence
Code Challenge
Python Lists
Python Lists
Access List Items
Change List Items
Add List Items
Remove List Items
Loop Lists
List Comprehension
Sort Lists
Copy Lists
Join Lists
List Methods
List Exercises
Code Challenge
Python Tuples
Python Tuples
Access Tuples
Update Tuples
Unpack Tuples
Loop Tuples
Join Tuples
Tuple Methods
Tuple Exercises
Code Challenge
Python Sets
Python Sets
Access Set Items
Add Set Items
Remove Set Items
Loop Sets
Join Sets
Frozenset
Set Methods
Set Exercises
Code Challenge
Python Dictionaries
Python Dictionaries
Access Items
Change Items
Add Items
Remove Items
Loop Dictionaries
Copy Dictionaries
Nested Dictionaries
Dictionary Methods
Dictionary Exercises
Code Challenge
Python If...Else
Python If
Python Elif
Python Else
Shorthand If
Logical Operators
Nested If
Pass Statement
Code Challenge
Python Match
Python While Loops
Python For Loops
Python Functions
Python Functions
Python Arguments
Python *args / **kwargs
Python Scope
Python Decorators
Python Lambda
Python Recursion
Python Generators
Code Challenge
Python Range
Python Arrays
Python Iterators
Python Modules
Python Dates
Python Math
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python None
Python User Input
Python VirtualEnv
Python ClassesPython OOP Python Classes/Objects Python __init__ Method Python self Parameter Python Class Properties Python Class Methods Python Magic Methods Python Inheritance Python Polymorphism Python Encapsulation Python Inner ClassesFile HandlingPython File Handling Python Read Files Python Write/Create Files Python Delete FilesPython ModulesNumPy Tutorial Pandas Tutorial SciPy Tutorial Django TutorialPython MatplotlibMatplotlib Intro Matplotlib Get Started Matplotlib Pyplot Matplotlib Plotting Matplotlib Markers Matplotlib Line Matplotlib Labels Matplotlib Grid Matplotlib Subplot Matplotlib Scatter Matplotlib Bars Matplotlib Histograms Matplotlib Pie ChartsMachine LearningGetting Started Mean Median Mode Standard Deviation Percentile Data Distribution Normal Data Distribution Scatter Plot Linear Regression Polynomial Regression Multiple Regression Scale Train/Test Decision Tree Confusion Matrix Hierarchical Clustering Logistic Regression Grid Search Categorical Data K-means Bootstrap Aggregation Cross Validation AUC - ROC Curve K-nearest neighborsPython DSAPython DSA Lists and Arrays Stacks Queues Linked Lists Hash Tables Trees Binary Trees Binary Search Trees AVL Trees Graphs Linear Search Binary Search Bubble Sort Selection Sort Insertion Sort Quick Sort Counting Sort Radix Sort Merge SortPython MySQLMySQL Get Started MySQL Create Database MySQL Create Table MySQL Insert MySQL Select MySQL Where MySQL Order By MySQL Delete MySQL Drop Table MySQL Update MySQL Limit MySQL JoinPython MongoDBMongoDB Get Started MongoDB Create DB MongoDB Collection MongoDB Insert MongoDB Find MongoDB Query MongoDB Sort MongoDB Delete MongoDB Drop Collection MongoDB Update MongoDB LimitPython CertPython CertificatePython How ToRemove List Duplicates Reverse a String Add Two NumbersPython ExamplesPython Examples Python Videos Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python TrainingPython ReferencePython Overview Python Built-in Functions
abs()
all()
any()
ascii()
bin()
bool()
bytearray()
bytes()
callable()
chr()
compile()
complex()
delattr()
dict()
dir()
divmod()
enumerate()
eval()
exec()
filter()
float()
format()
frozenset()
getattr()
globals()
hasattr()
hex()
id()
input()
int()
isinstance()
issubclass()
iter()
len()
list()
locals()
map()
max()
memoryview()
min()
next()
object()
oct()
open()
ord()
pow()
print()
range()
reversed()
round()
set()
setattr()
slice()
sorted()
str()
sum()
super()
tuple()
type()
vars()
zip()
Python String Methods
capitalize()
casefold()
center()
count()
encode()
endswith()
expandtabs()
find()
format()
format_map()
index()
isalnum()
isalpha()
isascii()
isdecimal()
isdigit()
isidentifier()
islower()
isnumeric()
isprintable()
isspace()
istitle()
isupper()
join()
ljust()
lower()
lstrip()
maketrans()
partition()
replace()
rfind()
rindex()
rjust()
rpartition()
rsplit()
rstrip()
split()
splitlines()
startswith()
strip()
swapcase()
title()
translate()
upper()
zfill()
Python List Methods
Python Dictionary Methods
Python Tuple Methods
Python Set Methods
add()
clear()
copy()
difference()
difference_update()
discard()
intersection()
intersection_update()
isdisjoint()
issubset()
<
issuperset()
>
pop()
remove()
symmetric_difference()
symmetric_difference_update()
union()
update()
Python File Methods
close()
fileno()
flush()
isatty()
read()
readable()
readline()
readlines()
seek()
seekable()
tell()
truncate()
writable()
write()
writelines()
Python Keywords
and
as
assert
async
await
break
case
class
continue
def
del
elif
else
except
False
finally
for
from
global
if
import
in
is
lambda
match
None
nonlocal
not
or
pass
raise
return
True
try
while
with
yield
Python Exceptions
ArithmeticError
AssertionError
AttributeError
Exception
EOFError
FloatingPointError
GeneratorExit
ImportError
IndentationError
IndexError
KeyError
KeyboardInterrupt
LookupError
MemoryError
NameError
NotImplementedError
OSError
OverflowError
ReferenceError
RuntimeError
StopIteration
SyntaxError
TabError
SystemError
SystemExit
TypeError
UnboundLocalError
UnicodeError
UnicodeEncodeError
UnicodeDecodeError
UnicodeTranslateError
ValueError
ZeroDivisionError
Python Glossary
Module ReferenceBuilt-in Modules
abc
aifc
argparse
array
ast
asyncio
atexit
base64
bdb
binascii
bisect
builtins
bz2
calendar
cgi
cgitb
cmd
code
codecs
codeop
collections
colorsys
concurrent
configparser
contextlib
contextvars
copy
copyreg
csv
ctypes
curses
dataclasses
datetime
dbm
decimal
difflib
dis
doctest
email
encodings
ensurepip
enum
errno
faulthandler
filecmp
fileinput
fnmatch
fractions
ftplib
functools
gc
getopt
getpass
gettext
glob
graphlib
gzip
hashlib
heapq
hmac
html
http
idlelib
imaplib
imghdr
imp
importlib
inspect
io
json
keyword
linecache
locale
logging
lzma
mailbox
mailcap
marshal
math
mimetypes
mmap
modulefinder
msilib
msvcrt
multiprocessing
netrc
nntplib
numbers
nis
ntpath
operator
optparse
os
pathlib
pdb
pickle
pickletools
pipes
pkgutil
platform
plistlib
poplib
posix
pprint
profile
pstats
pty
pyclbr
pydoc
py_compile
queue
quopri
random
re
reprlib
resource
rlcompleter
runpy
sched
secrets
select
selectors
shelve
shlex
shutil
signal
site
smtplib
socket
socketserver
sqlite3
ssl
stat
statistics
string
stringprep
struct
subprocess
sunau
symtable
sys
sysconfig
tabnanny
tarfile
telnetlib
tempfile
termios
test
textwrap
threading
time
timeit
tkinter
token
tokenize
tomllib
trace
traceback
tracemalloc
tty
turtle
types
typing
unicodedata
unittest
urllib
uuid
venv
warnings
wave
weakref
webbrowser
wsgiref
xdrlib
xml
xmlrpc
zipapp
zipfile
zipimport
zlib
zoneinfo
Random Module
seed()
getstate()
setstate()
getrandbits()
randrange()
randint()
choice()
choices()
shuffle()
sample()
random()
uniform()
triangular()
Requests Module
Statistics Module
statistics.harmonic_mean()
statistics.mean()
statistics.median()
statistics.median_grouped()
statistics.median_high()
statistics.median_low()
statistics.mode()
statistics.pstdev()
statistics.stdev()
statistics.pvariance()
statistics.variance()
Math Module
math.acos()
math.acosh()
math.asin()
math.asinh()
math.atan()
math.atan2()
math.atanh()
math.ceil()
math.comb()
math.copysign()
math.cos()
math.cosh()
math.degrees()
math.dist()
math.erf()
math.erfc()
math.exp()
math.expm1()
math.fabs()
math.factorial()
math.floor()
math.fmod()
math.frexp()
math.fsum()
math.gamma()
math.gcd()
math.hypot()
math.isclose()
math.isfinite()
math.isinf()
math.isnan()
math.isqrt()
math.ldexp()
math.lgamma()
math.log()
math.log10()
math.log1p()
math.log2()
math.perm()
math.pow()
math.prod()
math.radians()
math.remainder()
math.sin()
math.sinh()
math.sqrt()
math.tan()
math.tanh()
math.trunc()
math.e
math.inf
math.nan
math.pi
math.tau
cMath Module
cmath.acos(x)
cmath.acosh(x)
cmath.asin(x)
cmath.asinh(x)
cmath.atan(x)
cmath.atanh(x)
cmath.cos(x)
cmath.cosh(x)
cmath.exp(x)
cmath.isclose()
cmath.isfinite(x)
cmath.isinf(x)
cmath.isnan(x)
cmath.log(x[, base])
cmath.log10(x)
cmath.phase()
cmath.polar()
cmath.rect()
cmath.sin(x)
cmath.sinh(x)
cmath.sqrt(x)
cmath.tan(x)
cmath.tanh(x)
cmath.e
cmath.inf
cmath.infj
cmath.nan
cmath.nanj
cmath.pi
cmath.tau
Preprocessing - Categorical Data
Share
Categorical DataWhen your data has categories represented by strings, it will be difficult to use them to train machine learning models which often only accepts numeric data. Instead of ignoring the categorical data and excluding the information from our model, you can tranform the data so it can be used in your models. Take a look at the table below, it is the same data set that we used in the multiple regression chapter. Example
import pandas as pd
cars = pd.read_csv('data.csv') print(cars.to_string()) ResultCar Model Volume Weight CO2 0 Toyoty Aygo 1000 790 99 1 Mitsubishi Space Star 1200 1160 95 2 Skoda Citigo 1000 929 95 3 Fiat 500 900 865 90 4 Mini Cooper 1500 1140 105 5 VW Up! 1000 929 105 6 Skoda Fabia 1400 1109 90 7 Mercedes A-Class 1500 1365 92 8 Ford Fiesta 1500 1112 98 9 Audi A1 1600 1150 99 10 Hyundai I20 1100 980 99 11 Suzuki Swift 1300 990 101 12 Ford Fiesta 1000 1112 99 13 Honda Civic 1600 1252 94 14 Hundai I30 1600 1326 97 15 Opel Astra 1600 1330 97 16 BMW 1 1600 1365 99 17 Mazda 3 2200 1280 104 18 Skoda Rapid 1600 1119 104 19 Ford Focus 2000 1328 105 20 Ford Mondeo 1600 1584 94 21 Opel Insignia 2000 1428 99 22 Mercedes C-Class 2100 1365 99 23 Skoda Octavia 1600 1415 99 24 Volvo S60 2000 1415 99 25 Mercedes CLA 1500 1465 102 26 Audi A4 2000 1490 104 27 Audi A6 2000 1725 114 28 Volvo V70 1600 1523 109 29 BMW 5 2000 1705 114 30 Mercedes E-Class 2100 1605 115 31 Volvo XC70 2000 1746 117 32 Ford B-Max 1600 1235 104 33 BMW 216 1600 1390 108 34 Opel Zafira 1600 1405 109 35 Mercedes SLK 2500 1395 120 Run example » In the multiple regression chapter, we tried to predict the CO2 emitted based on the volume of the engine and the weight of the car but we excluded information about the car brand and model. The information about the car brand or the car model might help us make a better prediction of the CO2 emitted. One Hot EncodingWe cannot make use of the Car or Model column in our data since they are not numeric. A linear relationship between a categorical variable, Car or Model, and a numeric variable, CO2, cannot be determined. To fix this issue, we must have a numeric representation of the categorical variable. One way to do this is to have a column representing each group in the category. For each column, the values will be 1 or 0 where 1 represents the inclusion of the group and 0 represents the exclusion. This transformation is called one hot encoding. You do not have to do this manually, the Python Pandas module has a function that called
Learn about the Pandas module in our Pandas Tutorial. ExampleOne Hot Encode the Car column:
import pandas as pd
cars = pd.read_csv('data.csv') ohe_cars = pd.get_dummies(cars[['Car']]) print(ohe_cars.to_string()) ResultCar_Audi Car_BMW Car_Fiat Car_Ford Car_Honda Car_Hundai Car_Hyundai Car_Mazda Car_Mercedes Car_Mini Car_Mitsubishi Car_Opel Car_Skoda Car_Suzuki Car_Toyoty Car_VW Car_Volvo 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 3 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 6 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 7 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 8 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 9 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 12 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 13 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 14 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 15 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 16 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 18 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 19 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 22 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 25 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 26 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 27 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 29 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 31 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 32 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 33 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 34 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 35 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 Run example » ResultsA column was created for every car brand in the Car column. Predict CO2We can use this additional information alongside the volume and weight to predict CO2 To combine the information, we can use the First we will need to import a couple modules. We will start with importing the Pandas.
The pandas module allows us to read csv files and manipulate DataFrame objects:
It also allows us to create the dummy variables:
Then we must select the independent variables (X) and add the dummy variables columnwise. Also store the dependent variable in y.
We also need to import a method from sklearn to create a linear model Learn about linear regression.
Now we can fit the data to a linear regression:
Finally we can predict the CO2 emissions based on the car's weight, volume, and manufacturer.
Example
import pandas
from sklearn import linear_model cars = pandas.read_csv("data.csv") ohe_cars = pandas.get_dummies(cars[['Car']]) X = pandas.concat([cars[['Volume', 'Weight']], ohe_cars], axis=1) y = cars['CO2'] regr = linear_model.LinearRegression() regr.fit(X,y) ##predict the CO2 emission of a VW where the weight is 2300kg, and the volume is 1300cm3: predictedCO2 = regr.predict([[2300, 1300,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0]]) print(predictedCO2) Result[122.45153299] Run example » We now have a coefficient for the volume, the weight, and each car brand in the data set DummifyingIt is not necessary to create one column for each group in your category. The information can be retained using 1 column less than the number of groups you have. For example, you have a column representing colors and in that column, you have two colors, red and blue. Example
import pandas as pd
colors = pd.DataFrame({'color': ['blue', 'red']}) print(colors) Resultcolor 0 blue 1 red Run example » You can create 1 column called red where 1 represents red and 0 represents not red, which means it is blue. To do this, we can use the same function that we used for one hot encoding, get_dummies, and then drop one of the columns. There is an argument, drop_first, which allows us to exclude the first column from the resulting table. Example
import pandas as pd
colors = pd.DataFrame({'color': ['blue', 'red']}) dummies = pd.get_dummies(colors, drop_first=True) print(dummies) Resultcolor_red 0 0 1 1 Run example » What if you have more than 2 groups? How can the multiple groups be represented by 1 less column? Let's say we have three colors this time, red, blue and green. When we get_dummies while dropping the first column, we get the following table. Example
import pandas as pd
colors = pd.DataFrame({'color': ['blue', 'red', 'green']}) dummies = pd.get_dummies(colors, drop_first=True) dummies['color'] = colors['color'] print(dummies) Resultcolor_green color_red color 0 0 0 blue 1 0 1 red 2 1 0 green Run example » Free account Track your progress
XP earned 0
Day streak 0
Spaces 0
League —
New Learn in Adventure Mode W3Schools Adventure AppCoding fundamentals as bite-sized lessons and challenges.
×
Contact SalesIf you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: Report ErrorIf you want to report an error, or if you want to make a suggestion, send us an e-mail: Top TutorialsHTML TutorialCSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial Top ReferencesHTML ReferenceCSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference AngularJS Reference jQuery Reference
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy. Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |