Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Machine Learning Fundamentals

You're reading from   Machine Learning Fundamentals Use Python and scikit-learn to get up and running with the hottest developments in machine learning

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher
ISBN-13 9781789803556
Length 240 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Hyatt Saleh Hyatt Saleh
Author Profile Icon Hyatt Saleh
Hyatt Saleh
Arrow right icon
View More author details
Toc

Chapter 4: Supervised Learning Algorithms: Predict Annual Income

Activity 11: Training a Naïve Bayes Model for our Census Income Dataset

Before working on step 1, make sure that the data has been preprocessed, as follows:

import pandas as pd
data = pd.read_csv("datasets/census_income_dataset.csv")
data = data.drop(["fnlwgt","education","relationship","sex", "race"], axis=1)

After reading the dataset, the three variables considered irrelevant for the study are removed.

Next, the remaining qualitative variables are converted into their numerical form via the following code:

from sklearn.preprocessing import LabelEncoder
enc = LabelEncoder()
features_to_convert = ["workclass","marital-status","occupation","native-country","target"]
for i in features_to_convert:
  data[i] = enc.fit_transform(data[i].astype('str'))

Once this is complete...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image