Data Analysis Libraries
Python's native data analysis capabilities are limited, requiring third-party libraries to enhance its functionality. The key libraries we'll explore include NumPy, Matplotlib, and Pandas. Below is a brief introduction to these three libraries, with detailed usage examples covered in later sections.
NumPy
Python lacks built-in array support. While lists can handle basic array operations, they aren't true arrays and become inefficient with large datasets. NumPy addresses this by providing genuine array functionality along with fast, efficient data processing functions. NumPy also serves as a foundation for more advanced libraries, including Matplotlib and Pandas. It's worth noting that NumPy's built-in functions operate at C-language speeds, so developers should prioritize these functions to avoid performance bottlenecks.
Matplotlib
Both data mining and mathematical modeling require data visualization. Matplotlib stands as Python's most prominent plotting library, specializing in two-dimensional graphics. It enables rapid and effective data visualization in Python.
Pandas
Pandas represents Python's most powerful data analysis toolkit. It incorporates advanced data structures and sophisticated tools that make data processing in Python both fast and straightforward. Built on NumPy, Pandas facilitates easy integration with NumPy-centric applications. Originally deeveloped by AQR Capital Management in April 2008 as a financial data enalysis tool, it was open-sourced in 2009.
Pandas offers robust functionality, including SQL-like data manipulation (create, read, update, delete) and numerous data processing functions. It also provides flexible handling of missing data.
Anaconda Distribution
Anaconda is a Python distribution tailored for scientific computing, available for Linux, Mac, and Windows. It provides package management and Python environment management capabilities, simplifying multi-version Python scenarios and package installations. Anaconda uses the conda command for both package and virtual environment management.
The distinction between Anaconda and conda: conda is a tool primarily for package and virtual environment management, while Anaconda is a comprehensive collection of packages and scientific computing tools, making it a Python distribution.
# Create a Python 3.4 environment named analysis-env
conda create --name analysis-env python=3.4
# Activate the environment
# Windows command doesn't use source
# activate analysis-env
# Linux & Mac activation command
source activate analysis-env
# Check version
python --version
# For Windows
deactivate analysis-env
# For Linux & Mac
source deactivate analysis-env
# Remove an existing environment
conda remove --name analysis-env --all
Package Management:
# Install third-party packages
conda install django=1.8.2
# Uninstall third-party packages
conda uninstall django
Why Use Jupyter Notebook?
Data analysis requires communicating findings and reproducing the entire analysis process. We need to organize explanatory text, code, charts, formulas, and conclusions in a single document. The data analysis workflow involves continuous computation and visualization, wich traditional text editors cannot adequately support. This is where Jupyter Notebook becomes valuable.
When writing conventional code, our audience is the machine, so we structure programs following computational logic. In data analysis, however, our audience is human. We transition from writing machine-readable code to explaining how to implement our ideas, incorporating narrative text and visualizations alongside code. A data analyst must be both a skilled programmer and an effective communicator, requiring a tool that supports both programming and writing.
Jupyter evolved from the IPython project, which was initially focused on Python. As the project expanded, it grew beyond Python to support multiple programming languages. The name Jupyter represents a combination of Julia, Python, and R, and it now supports many additional languages beyond these three.
Installing Jupyter
- Create a virtual environment
pip install virtualenv
pip install virtualenvwrapper-win
mkvirtualenv data-analysis-env
- Install required packages
pip install numpy
pip install matplotlib
pip install pandas # Windows users can install whl packages directly
pip install jupyter
- Launch Jupyter Notebook
workon data-analysis-env
jupyter notebook
Basic Jupyter Operations
Shift + Enter — Executes current cell and moves to the next cell
Ctrl + Enter — Executes current cell without moving to the next cell
Example code:
10+20
Example with visualization:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4],[2,4,6,8])
plt.show()
You can select Markdown syntax in this area to format text.
Note: Include a space after the # symbol when using Markdown headers
Click here to rename the current file.