Chapter 13. Capstone: Python for Data Analytics
At the end of Chapter 8 you extended what you learned about R to explore and test relationships in the mpg dataset. We’ll do the same in this chapter, using Python. We’ve conducted the same work in Excel and R, so I’ll focus less on the whys of our analysis in favor of the hows of doing it in Python.
To get started, let’s call in all the necessary modules. Some of these are new: from scipy, we’ll import the stats submodule. To do this, we’ll use the from keyword to tell Python what module to look for, then the usual import keyword to choose a sub-module. As the name suggests, we’ll use the stats submodule of scipy to conduct our statistical analysis. We’ll also be using a new package called sklearn, or scikit-learn, to validate our model on a train/test split. This package has become a dominant resource for machine learning and also comes installed with Anaconda.
In[1]:importpandasaspdimportseabornassnsimportmatplotlib.pyplotaspltfromscipyimportstatsfromsklearnimportlinear_modelfromsklearnimportmodel_selectionfromsklearnimportmetrics
With the usecols argument of read_csv() we can specify which columns to read into the DataFrame:
In[2]:mpg=pd.read_csv('datasets/mpg/mpg.csv',usecols=['mpg','weight','horsepower','origin','cylinders'])mpg.head()Out[2]:mpgcylindershorsepowerweightorigin018.081303504USA115.081653693USA218.081503436USA316.081503433USA417.081403449USA ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access