Hướng dẫn data analysis with python ppt - phân tích dữ liệu với ppt python

Presentation on theme: "Python for Data Analysis"— Presentation transcript:

1 Python for Data Analysis
Taken from Slides at Boston University

2 Contents What Is Data Analytics?
Overview of Python Libraries for Data Scientists Contents Reading Data; Selecting and Filtering the Data; Data manipulation, sorting, grouping, rearranging Plotting the data Descriptive statistics Inferential statistics

3 What Is Data Analytics? & Why Is It So Popular?
Data analytics is the process and methodology of analyzing data to draw meaningful insight from the data We now see the limitless potential for gaining critical insight by applying data analytics

4 Types of Data Analytics

5 Confusion – Data Analysis vs. Data Analytics
They’re often used interchangeably, but technically speaking…

6 Confusion – Big Data vs. Data Analytics
What they have in common is that both refer to data, but technically speaking…

7 Confusion – Machine Learning vs. Data Analytics

8 Confusion –AI vs. Data Analytics

9 Advanced Python Programming Features
Web development Networking Scientific computing Data analytics etc.

10 Python as a Data Analytics Tool
The nature of Python makes it a perfect-fit for data analytics Easy to learn Readable Scalable Extensive set of libraries Easy integration with other apps Active community & ecosystem

11 Popular Python Data Analytics Libraries

12 Comparison – R vs. Python
Comparison between R and Python has been absolutely one of the hottest topics in data science communities R came from the statisticians community, whereas Python came from the computer scientists community Python is said to be a challenger against R, but in general it’s a tie It’s up to you to choose the one that best fits your needs For detailed comparison, refer to analysis

13 Python Libraries for Data Science
Many popular Python toolboxes/libraries: NumPy SciPy Pandas SciKit-Learn Visualization libraries matplotlib Seaborn and many more … You may need to install some of these libraries

14 Python Libraries for Data Science
NumPy: introduces objects for multidimensional arrays and matrices, as well as functions that allow to easily perform advanced mathematical and statistical operations on those objects provides vectorization of mathematical operations on arrays and matrices which significantly improves the performance many other python libraries are built on NumPy Link: Find, install and publish Python package with the Python Package Index:

15 Python Libraries for Data Science
SciPy: collection of algorithms for linear algebra, differential equations, numerical integration, optimization, statistics and more part of SciPy Stack built on NumPy Link:

16 Python Libraries for Data Science
Pandas: adds data structures and tools designed to work with table-like data (similar to Series and Data Frames in R) provides tools for data manipulation: reshaping, merging, sorting, slicing, aggregation etc. allows handling missing data Link:

17 Python Libraries for Data Science
SciKit-Learn: provides machine learning algorithms: classification, regression, clustering, model validation etc. built on NumPy, SciPy and matplotlib Link:

18 Python Libraries for Data Science
matplotlib: python 2D plotting library which produces publication quality figures in a variety of hardcopy formats  a set of functionalities similar to those of MATLAB line plots, scatter plots, barcharts, histograms, pie charts etc. relatively low-level; some effort needed to create advanced visualization Link:

19 Python Libraries for Data Science
Seaborn: based on matplotlib  provides high level interface for drawing attractive statistical graphics Similar (in style) to the popular ggplot2 library in R Link:

20 Loading Python Libraries
#Import Python Libraries import numpy as np import scipy as sp import pandas as pd import matplotlib as mpl import seaborn as sns Press Shift+Enter to execute the jupyter cell

21 Reading data using pandas
#Read csv file df = pd.read_csv(" Note: The above command has many optional arguments to fine-tune the data import process. There is a number of pandas commands to read other data formats: pd.read_excel('myfile.xlsx',sheet_name='Sheet1', index_col=None, na_values=['NA']) pd.read_stata('myfile.dta') pd.read_sas('myfile.sas7bdat') pd.read_hdf('myfile.h5','df')

22 Exploring data frames In [3]: #List first 5 records df.head() Out[3]:

23 Hands-on exercises Try to read the first 10, 20, 50 records;
Can you guess how to view the last few records; Hint:

24 Data Frame data types Pandas Type Native Python Type Description
object string The most general dtype. Will be assigned to your column if column has mixed types (numbers and strings). int64 int Numeric characters. 64 refers to the memory allocated to hold this character. float64 float Numeric characters with decimals. If a column contains numbers and NaNs(see below), pandas will default to float64, in case your missing value has a decimal. datetime64, timedelta[ns] N/A (but see the datetime module in Python’s standard library) Values meant to hold time data. Look into these for time series experiments.

25 Kiểu dữ liệu khung dữ liệu trong [4]: ​​#check một cột cụ thể đánh máy ['Mức lương']. : RankDisciplinePhdservicesexsalDype: ObjectObjectInT64 Data Frame data types In [4]: #Check a particular column type
df['salary'].dtype Out[4]: dtype('int64') In [5]: #Check types for all the columns df.dtypes Out[4]: rank discipline phd service sex salary dtype: object object int64

26 Khung dữ liệu Attributespython Đối tượng có các thuộc tính và phương thức.DF.AttributionScripdTyPesList Các loại của cột Data Frames attributes
Python objects have attributes and methods. df.attribute description dtypes list the types of the columns columns list the column names axes list the row labels and column names ndim number of dimensions size number of elements shape return a tuple representing the dimensionality values numpy representation of the data

27 Bài tập thực hành tìm thấy có bao nhiêu bản ghi mà khung dữ liệu này có bao nhiêu phần tử? Tên cột là gì? Những loại cột chúng ta có trong khung dữ liệu này là gì? Hands-on exercises Find how many records this data frame has;
How many elements are there? What are the column names? What types of columns we have in this data frame?

28 Khung dữ liệu Phương thức DF.METHOD () Các thuộc tính mô tả, Phương thức Python có dấu ngoặc đơn. Tất cả các thuộc tính và phương thức có thể được liệt kê với hàm dir () n]) đầu tiên/cuối n rowsDescrib () tạo số liệu thống kê mô tả (chỉ cho các cột số) tối đa . Data Frames methods df.method() description
Unlike attributes, python methods have parenthesis. All attributes and methods can be listed with a dir() function: dir(df) df.method() description head( [n] ), tail( [n] ) first/last n rows describe() generate descriptive statistics (for numeric columns only) max(), min() return max/min values for all numeric columns mean(), median() return mean/median values for all numeric columns std() standard deviation sample([n]) returns a random sample of the data frame dropna() drop all the records with missing values

29 Bài tập thực hành Tóm tắt cho các cột số trong DatasetCalculation Độ lệch chuẩn cho tất cả các cột số; Giá trị trung bình của 50 bản ghi đầu tiên trong bộ dữ liệu là gì? Gợi ý: Sử dụng phương thức Head () để tập hợp 50 bản ghi đầu tiên và sau đó tính toán giá trị trung bình Hands-on exercises Give the summary for the numeric columns in the dataset Calculate standard deviation for all numeric columns; What are the mean values of the first 50 records in the dataset? Hint: use head() method to subset the first 50 records and then calculate the mean

30 Chọn một cột trong dữ liệu FRAMEMETHOD 1: HOMTET Khung dữ liệu bằng tên cột: DF ['SEX'] Phương pháp 2: Sử dụng tên cột làm thuộc tính: DF.SEX Lưu ý: Có một thứ hạng thuộc tính cho khung dữ liệu Pandas, Vì vậy, để chọn một cột có tên "Xếp hạng", chúng ta nên sử dụng Phương pháp 1. Selecting a column in a Data Frame
Method 1: Subset the data frame using column name: df['sex'] Method 2: Use the column name as an attribute: df.sex Note: there is an attribute rank for pandas data frames, so to select a column with a name "rank" we should use method 1.

31 bài tập thực hành thực hành các số liệu thống kê cơ bản cho cột lương; tìm số lượng giá trị trong cột lương (sử dụng phương pháp đếm); tính lương trung bình; Hands-on exercises Calculate the basic statistics for the salary column; Find how many values in the salary column (use count method); Calculate the average salary;

32 Khung dữ liệu Nhóm Phương pháp sử dụng phương pháp "Nhóm BY" Chúng ta có thể: Chia dữ liệu thành các nhóm dựa trên một số thống kê tiêu chí (hoặc áp dụng hàm) cho mỗi nhóm tương ứng với hàm dplyr () trong RIN []:#Dữ liệu nhóm bằng cách sử dụng RankDF_RANK = DF. GroupBy (['xếp hạng']) trong []:#Tính giá trị trung bình cho mỗi cột số trên mỗi nhómDF_RANK.MEAN () Data Frames groupby method
Using "group by" method we can: Split the data into groups based on some criteria Calculate statistics (or apply a function) to each group Similar to dplyr() function in R In [ ]: #Group data using rank df_rank = df.groupby(['rank']) In [ ]: #Calculate mean value for each numeric column per each group df_rank.mean()

33 Khung dữ liệu Groupby Methodonce Đối tượng Nhóm là Tạo Chúng tôi có thể tính toán các số liệu thống kê khác nhau cho từng nhóm: trong []:#Tính lương trung bình cho mỗi cấp bậc giáo sư: df.groupby ('xếp hạng') [['Mức lương']]. Lưu ý: Nếu các dấu ngoặc đơn được sử dụng để chỉ định cột (ví dụ: tiền lương), thì đầu ra là đối tượng chuỗi Pandas. Khi sử dụng dấu ngoặc kép, đầu ra là khung dữ liệu Data Frames groupby method
Once groupby object is create we can calculate various statistics for each group: In [ ]: #Calculate mean salary for each professor rank: df.groupby('rank')[['salary']].mean() Note: If single brackets are used to specify the column (e.g. salary), then the output is Pandas Series object. When double brackets are used the output is a Data Frame

34 Khung dữ liệu Groupby Methodgroup Ghi chú hiệu suất:- Không có nhóm/phân tách xảy ra cho đến khi cần thiết. Tạo đối tượng GroupBy chỉ xác minh rằng bạn đã vượt qua ánh xạ hợp lệ- theo mặc định, các khóa nhóm được sắp xếp trong hoạt động nhóm. Bạn có thể muốn vượt qua Sắp xếp = Sai cho SpeedUp tiềm năng: Trong []:#Tính lương trung bình cho mỗi cấp bậc giáo sư: df.groupby (['xếp hạng'], sort = false) ['[' Mức lương ']. Data Frames groupby method
groupby performance notes: - no grouping/splitting occurs until it's needed. Creating the groupby object only verifies that you have passed a valid mapping - by default the group keys are sorted during the groupby operation. You may want to pass sort=False for potential speedup: In [ ]: #Calculate mean salary for each professor rank: df.groupby(['rank'], sort=False)[['salary']].mean()

35 Khung dữ liệu: Lọc tập hợp con dữ liệu chúng tôi có thể áp dụng lập chỉ mục Boolean. Lập chỉ mục này thường được gọi là bộ lọc. Ví dụ: nếu chúng ta muốn tập hợp các hàng trong đó giá trị lương lớn hơn $ 120K: trong []:#Tính lương trung bình cho mỗi cấp bậc giáo sư: DF_SUB = DF [DF ['Mức lương']] được sử dụng để tập hợp dữ liệu:> lớn hơn; > = lớn hơn hoặc bằng nhau; <ít hơn; Data Frame: filtering To subset the data we can apply Boolean indexing. This indexing is commonly known as a filter. For example if we want to subset the rows in which the salary value is greater than $120K: In [ ]: #Calculate mean salary for each professor rank: df_sub = df[ df['salary'] > ] Any Boolean operator can be used to subset the data: > greater; >= greater or equal; < less; <= less or equal; == equal; != not equal; In [ ]: #Select only those rows that contain female professors: df_f = df[ df['sex'] == 'Female' ]

36 Khung dữ liệu: SLICINGHERE là một số cách để tập hợp lại khung dữ liệu: một hoặc nhiều cột hoặc nhiều tập hợp con hàng của hàng và cột và cột có thể được chọn bởi vị trí hoặc nhãn của chúng Data Frames: Slicing There are a number of ways to subset the Data Frame: one or more columns one or more rows a subset of rows and columns Rows and columns can be selected by their position or label

37 khung dữ liệu: SLATION Khi chọn một cột, có thể sử dụng một bộ dấu ngoặc đơn, nhưng đối tượng kết quả sẽ là một chuỗi (không phải là DataFrame): Trong []:#Chọn Mức lương: Cần chọn nhiều hơn một cột và/hoặc tạo đầu ra thành DataFrame, chúng ta nên sử dụng dấu ngoặc kép: Trong []:#Chọn Mức lương cột: DF [['xếp hạng', 'Mức lương']]]]] Data Frames: Slicing When selecting one column, it is possible to use single set of brackets, but the resulting object will be a Series (not a DataFrame): In [ ]: #Select column salary: df['salary'] When we need to select more than one column and/or make the output to be a DataFrame, we should use double brackets: In [ ]: #Select column salary: df[['rank','salary']]

38 khung dữ liệu: Chọn hàng Chúng ta cần chọn một phạm vi hàng, chúng ta có thể chỉ định phạm vi bằng cách sử dụng ":" Trong []:#Chọn hàng theo vị trí của chúng: DF [10:20] Lưu ý rằng hàng đầu tiên có vị trí 0 và giá trị cuối cùng trong phạm vi bị bỏ qua: Vì vậy, trong khoảng 0:10, 10 hàng đầu tiên được trả về với các vị trí bắt đầu bằng 0 và kết thúc với 9 Data Frames: Selecting rows
If we need to select a range of rows, we can specify the range using ":" In [ ]: #Select rows by their position: df[10:20] Notice that the first row has a position 0, and the last value in the range is omitted: So for 0:10 range the first 10 rows are returned with the positions starting with 0 and ending with 9

39 khung dữ liệu: Phương thức locif Chúng ta cần chọn một loạt các hàng, sử dụng nhãn của chúng, chúng ta có thể sử dụng phương thức LỘC ',' tiền lương ']] ra []: Data Frames: method loc
If we need to select a range of rows, using their labels we can use method loc: In [ ]: #Select rows by their labels: df_sub.loc[10:20,['rank','sex','salary']] Out[ ]:

40 Khung dữ liệu: Phương pháp ILOCIF Chúng ta cần chọn một phạm vi hàng và/hoặc cột, sử dụng vị trí của chúng, chúng ta có thể sử dụng phương thức ILOC: Trong []:#Chọn hàng theo nhãn của chúng: df_sub.iloc [10:20, [0, 3, 4, 5]] ra []: Data Frames: method iloc
If we need to select a range of rows and/or columns, using their positions we can use method iloc: In [ ]: #Select rows by their labels: df_sub.iloc[10:20,[0, 3, 4, 5]] Out[ ]:

41 Khung dữ liệu: Phương thức ILOC (Tóm tắt) DF.ILOC [0] # Hàng đầu tiên của dữ liệu đóng khung ] # Cột đầu tiênDf.iloc [:, -1] # Cột cuối cùng.iloc [0: 7] #First 7 RowsDf.iloc [:, 0: 2] #Second đến các hàng thứ ba và 2 cột đầu tiênSdf.iloc [[0,5], [1,3]] #1 và 6 hàng và cột thứ 2 và thứ 4 Data Frames: method iloc (summary)
df.iloc[0] # First row of a data frame df.iloc[i] #(i+1)th row df.iloc[-1] # Last row df.iloc[:, 0] # First column df.iloc[:, -1] # Last column df.iloc[0:7] #First 7 rows df.iloc[:, 0:2] #First 2 columns df.iloc[1:3, 0:2] #Second through third rows and first 2 columns df.iloc[[0,5], [1,3]] #1st and 6th rows and 2nd and 4th columns

42 khung dữ liệu: Sắp xếp chúng tôi có thể sắp xếp dữ liệu theo giá trị trong cột. Theo mặc định, việc sắp xếp sẽ xảy ra theo thứ tự tăng dần và khung dữ liệu mới là return.in []:# Tạo khung dữ liệu mới từ bản gốc được sắp xếp bởi cột malydf_sorted = df.sort_values ​​(by = 'service') df_sorted.head ( )Ngoài[ ]: Data Frames: Sorting We can sort the data by a value in the column. By default the sorting will occur in ascending order and a new data frame is return. In [ ]: # Create a new data frame from the original sorted by the column Salary df_sorted = df.sort_values( by ='service') df_sorted.head() Out[ ]:

43 khung dữ liệu: Sắp xếp chúng tôi có thể sắp xếp dữ liệu bằng 2 cột trở lên: df_sorted = df.sort_values ​​(by = ['service', 'laled'], tăng dần = [true, false]) df_sorted.head (10) ]: Data Frames: Sorting We can sort the data using 2 or more columns:
df_sorted = df.sort_values( by =['service', 'salary'], ascending = [True, False]) df_sorted.head(10) Out[ ]:

44 Các giá trị bị thiếu các giá trị bị thiếu được đánh dấu là nan trong []: trong []:# đọc một bộ dữ liệu có giá trị bị thiếu flightflight = pd.read_csv ("trong []:# chọn các hàng có ít nhất một giá trị bị thiếu [flays.isnull ( ) .any (trục = 1)]. head () out []: Missing Values Missing values are marked as NaN In [ ]: In [ ]:
# Read a dataset with missing values flights = pd.read_csv(" In [ ]: # Select the rows that have at least one missing value flights[flights.isnull().any(axis=1)].head() Out[ ]:

45 Thiếu giá trị là một số phương pháp để đối phó với các giá trị bị thiếu trong khung dữ liệu: df.method () Mô tảDropna () Thả các quan sát bị thiếu 'Tất cả') Cột thả nếu tất cả các giá trị bị thiếu (ngưỡng = 5) thả các hàng có chứa ít hơn 5 giá trị không bỏ lỡ giá trị không bỏ lỡ Missing Values There are a number of methods to deal with missing values in the data frame: df.method() description dropna() Drop missing observations dropna(how='all') Drop observations where all cells is NA dropna(axis=1, how='all') Drop column if all the values are missing dropna(thresh = 5) Drop rows that contain less than 5 non-missing values fillna(0) Replace missing values with zeros isnull() returns True if the value is missing notnull() Returns True for non-missing values

46 các giá trị bị thiếu khi tính tổng dữ liệu, các giá trị bị thiếu sẽ được coi là không có tất cả các giá trị bị thiếu, tổng sẽ bằng các phương thức nancumsum () và cumprod () bỏ qua các giá trị bị thiếu nhưng bảo tồn chúng trong các giá trị mảng kết quả trong phương thức nhóm được loại trừ ( Giống như trong r) Nhiều phương thức thống kê mô tả có tùy chọn SKIPNA để kiểm soát nếu có nên loại trừ dữ liệu. Giá trị này được đặt thành true theo mặc định (không giống như r) Missing Values When summing the data, missing values will be treated as zero If all values are missing, the sum will be equal to NaN cumsum() and cumprod() methods ignore missing values but preserve them in the resulting arrays Missing values in GroupBy method are excluded (just like in R) Many descriptive statistics methods have skipna option to control if missing data should be excluded . This value is set to True by default (unlike R)

47 Các hàm tổng hợp trong phân chia pandasaging - tính toán một thống kê tóm tắt về từng nhóm, i.e.compute Sums hoặc các chức năng tổng hợp nhóm/đồng tính Aggregation Functions in Pandas
Aggregation - computing a summary statistic about each group, i.e. compute group sums or means compute group sizes/counts Common aggregation functions: min, max count, sum, prod mean, median, mode, mad std, var

48 Hàm tổng hợp trong phương pháp pandasagg () rất hữu ích khi nhiều số liệu thống kê được tính cho mỗi cột: trong []: các chuyến bay [['dep_delay', 'arr_delay']]. )Ngoài[ ]: Aggregation Functions in Pandas
agg() method are useful when multiple statistics are computed per column: In [ ]: flights[['dep_delay','arr_delay']].agg(['min','mean','max']) Out[ ]:

49 Thống kê mô tả cơ bảnDF.Method () Mô tả Thống kê thống kê (Đếm, Trung bình, STD, Min, Quantiles, Max) Min, MaxMinimum và Maximum ValueMean, Median, Modearithmetic, Median và Modevar Basic Descriptive Statistics
df.method() description describe Basic statistics (count, mean, std, min, quantiles, max) min, max Minimum and maximum values mean, median, mode Arithmetic average, median and mode var, std Variance and standard deviation sem Standard error of mean skew Sample skewness kurt kurtosis

50 Đồ họa để khám phá gói Dataseborn được xây dựng trên matplotlib nhưng cung cấp giao diện cấp cao để vẽ đồ họa thống kê hấp dẫn, tương tự như thư viện GGPLOT2 trong R. nội tuyến Graphics to explore the data
Seaborn package is built on matplotlib but provides high level interface for drawing attractive statistical graphics, similar to ggplot2 library in R. It specifically targets statistical data visualization To show graphs within Python notebook include inline directive: In [ ]: %matplotlib inline

51 Đồ họa Mô tả DISTPLOT Biểu đồ Barplotestimate của xu hướng trung tâm đối với một biến sốViolInplot & nbsp; tương tự như BoxPlot, cũng cho thấy mật độ xác suất của DataJointplotsCatterplotRegplotRess Graphics description distplot histogram barplot
estimate of central tendency for a numeric variable violinplot  similar to boxplot, also shows the probability density of the data jointplot Scatterplot regplot Regression plot pairplot Pairplot boxplot swarmplot categorical scatterplot factorplot General categorical plot

52 Phân tích thống kê cơ bảnStatsModel và Scikit-learn-Cả hai đều có một số chức năng để phân tích thống kê cái đầu tiên được sử dụng để phân tích thường xuyên bằng cách sử dụng các công thức kiểu R, trong khi Scikit-Learn phù hợp hơn với máy học. . Basic statistical Analysis
statsmodel and scikit-learn - both have a number of function for statistical analysis The first one is mostly used for regular analysis using R style formulas, while scikit-learn is more tailored for Machine Learning. statsmodels: linear regressions ANOVA tests hypothesis testings many more ... scikit-learn: kmeans support vector machines random forests See examples in the Tutorial Notebook