Book Image

Practical Data Analysis Cookbook

By : Tomasz Drabas
Book Image

Practical Data Analysis Cookbook

By: Tomasz Drabas

Overview of this book

Data analysis is the process of systematically applying statistical and logical techniques to describe and illustrate, condense and recap, and evaluate data. Its importance has been most visible in the sector of information and communication technologies. It is an employee asset in almost all economy sectors. This book provides a rich set of independent recipes that dive into the world of data analytics and modeling using a variety of approaches, tools, and algorithms. You will learn the basics of data handling and modeling, and will build your skills gradually toward more advanced topics such as simulations, raw text processing, social interactions analysis, and more. First, you will learn some easy-to-follow practical techniques on how to read, write, clean, reformat, explore, and understand your data—arguably the most time-consuming (and the most important) tasks for any data scientist. In the second section, different independent recipes delve into intermediate topics such as classification, clustering, predicting, and more. With the help of these easy-to-follow recipes, you will also learn techniques that can easily be expanded to solve other real-life problems such as building recommendation engines or predictive models. In the third section, you will explore more advanced topics: from the field of graph theory through natural language processing, discrete choice modeling to simulations. You will also get to expand your knowledge on identifying fraud origin with the help of a graph, scrape Internet websites, and classify movies based on their reviews. By the end of this book, you will be able to efficiently use the vast array of tools that the Python environment has to offer.
Table of Contents (19 chapters)
Practical Data Analysis Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Sampling the data


Sometimes the dataset that we have is too big to be used to build a model. For practical reasons (so that the estimation of our models does not take forever), it is good to create a stratified sample from the full dataset.

In this recipe, we will read from our MongoDB database and use Python to create a sample.

Getting ready

To execute this recipe, you will need PyMongo, pandas, and NumPy. No other prerequisites are required.

How to do it…

There are two approaches that one can take: either specify the fraction of the original dataset (say, 20%) or specify the number of records one would like to retrieve from the dataset. The following code shows you how to fetch a fraction of the dataset (the data_sampling.py file):

strata_frac = 0.2

client = pymongo.MongoClient()
db = client['packt']
real_estate = db['real_estate']

# retrieve the data
sales = pd.DataFrame.from_dict(
    list( real_estate.find(
            {'beds': {'$in': [2,3,4]} },
            {'_id': 0, 'zip': 1, 'city...