Data Mining with Python (Working draft) - DTU
Data Mining with Python (Working draft)
Finn ?Arup Nielsen November 29, 2017
Contents
Contents
i
List of Figures
vii
List of Tables
ix
1 Introduction
1
1.1 Other introductions to Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Why Python for data mining? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Why not Python for data mining? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Components of the Python language and software . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 Developing and running Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5.1 Python, pypy, IPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5.2 Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5.3 Python 2 vs. Python 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5.4 Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5.5 Python in the cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5.6 Running Python in the browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 Python
9
2.1 Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2.1 Booleans (bool) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2.2 Numbers (int, float, complex and Decimal) . . . . . . . . . . . . . . . . . . . . . . . 10
2.2.3 Strings (str) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2.4 Dictionaries (dict) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2.5 Dates and times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.2.6 Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2.7 Other containers classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 Functions and arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.1 Anonymous functions with lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.2 Optional function arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4 Object-oriented programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.1 Objects as functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.5 Modules and import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.5.1 Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5.2 Globbing import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.5.3 Coping with Python 2/3 incompatibility . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.6 Persistency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.6.1 Pickle and JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.6.2 SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
i
2.6.3 NoSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.7 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.8 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.8.1 Testing for type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.8.2 Zero-one-some testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.8.3 Test layout and test discovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.8.4 Test coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.8.5 Testing in different environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.9 Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.10 Coding style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.10.1 Where is private and public? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.11 Command-line interface scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.11.1 Distinguishing between module and script . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.11.2 Argument parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.11.3 Exit status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.12 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2.12.1 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.13 Advices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3 Python for data mining
33
3.1 Numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.2 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.2.1 3D plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.2.2 Real-time plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.2.3 Plotting for the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.3 Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.3.1 Pandas data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.3.2 Pandas indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.3.3 Pandas joining, merging and concatenations . . . . . . . . . . . . . . . . . . . . . . . . 42
3.3.4 Simple statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.4 SciPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.4.1 scipy.linalg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.4.2 Fourier transform with scipy.fftpack . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.5 Statsmodels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.6 Sympy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.7 Machine learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.7.1 Scikit-learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.8 Text mining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.8.1 Regular expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.8.2 Extracting from webpages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.8.3 NLTK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.8.4 Tokenization and part-of-speech tagging . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.8.5 Language detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.8.6 Sentiment analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.9 Network mining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.10 Miscellaneous issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.10.1 Lazy computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.11 Testing data mining code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4 Case: Pure Python matrix library
59
4.1 Code listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
ii
5 Case: Pima data set
65
5.1 Problem description and objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.2 Descriptive statistics and plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
5.3 Statistical tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
5.4 Predicting diabetes type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6 Case: Data mining a database
71
6.1 Problem description and objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
6.2 Reading the data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
6.3 Graphical overview on the connections between the tables . . . . . . . . . . . . . . . . . . . . 72
6.4 Statistics on the number of tracks sold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7 Case: Twitter information diffusion
75
7.1 Problem description and objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
7.2 Building a news classifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
8 Case: Big data
77
8.1 Problem description and objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.2 Stream processing of JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.2.1 Stream processing of JSON Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Bibliography
81
Index
85
iii
iv
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- pyqgis developer cookbook documentation
- python data science handbook interplanetary file system
- data mining with python working draft dtu
- introduction to python harvard university
- python for computational finance github pages
- python for finance
- biopython tutorial and cookbook biopython biopython
- introduction to python pandas for data virginia tech
- a python book beginning python advanced python and
- manipulating and analyzing data with pandas
Related searches
- statistics with python pdf
- data analysis with excel examples
- data analytics with excel pdf
- python working with dataframes
- python working with csv files
- data analytics with excel
- create empty data frame with column names
- r create data frame with column names
- data science with python
- data analysis with excel pdf
- data analysis with excel
- data analytics with excel example