Python
  • Introduction
  • Chapter 1.Notes from research
    • 1.Introduction of Python
    • 2. Build developer environment
      • 2.1.Sublime Text3
      • 2.2.Jupyter(IPython notebook)
        • 2.2.1.Introduction
        • 2.2.2.Basic usage
        • 2.2.3.some common operations
      • 2.3.Github
        • 2.3.1.Create Github account
        • 2.3.2.Create a new repository
        • 2.3.3.Basic operations: config, clone, push
      • 2.4.Install Python 3.4 in Windows
    • 3. Write Python code
      • 3.1.Hello Python
      • 3.2.Basic knowledges
      • 3.3.撰寫獨立python程式
      • 3.4.Arguments parser
      • 3.5.Class
      • 3.6.Sequence
    • 4. Web crawler
      • 4.1.Introduction
      • 4.2.requests
      • 4.3.beautifulSoup4
      • 3.4.a little web crawler
    • 5. Software testing
      • 5.1. Robot Framework
        • 1.1.Introduction
        • 1.2.What is test-automation framework?
        • 1.3.Robot Framework Architecture
        • 1.4.Robot Framework Library
        • 1.5.Reference
    • 6. encode/ decode
      • 6.1.編碼/解碼器的基本概念
      • 6.2.常見的編碼/ 解碼錯誤訊息與其意義
      • 6.3 .處理文字檔案
    • 7. module
      • 7.1.Write a module
      • 7.2.Common module
        • 7.2.1.sched
        • 7.2.2.threading
    • 8. Integrate IIS with django
      • 8.1.Integrate IIS with django
  • Chapter 2.Courses
    • 2.1.Python for Data Science and Machine Learning Bootcamp
      • 2.1.1.Virtual Environment
      • 2.1.2.Python crash course
      • 2.1.3.Python for Data Analysis - NumPy
        • 2.1.3.1.Numpy arrays
        • 2.1.3.2.Numpy Array Indexing
        • 2.1.3.3.Numpy Operations
      • 2.1.4.Python for Data Analysis - Pandas
        • 2.1.4.1.Introduction
        • 2.1.4.2.Series
        • 2.1.4.3.DataFrames
        • 2.1.4.4.Missing Data
        • 2.1.4.5.GroupBy
        • 2.1.4.6.Merging joining and Concatenating
        • 2.1.4.7.Data input and output
      • 2.1.5.Python for Data Visual Visualization - Pandas Built-in Data Visualization
      • 2.1.6.Python for Data Visualization - Matplotlib
        • 2.1.6.1.Introduction of Matplotlib
        • 2.1.6.2.Matplotlib
      • 2.1.7.Python for Data Visualization - Seaborn
        • 2.1.7.1.Introduction to Seaborn
        • 2.1.7.2.Distribution Plots
        • 2.1.7.3.Categorical Plots
        • 2.1.7.4.Matrix Plots
        • 2.1.7.5.Grids
        • 2.1.7.6.Regression Plots
      • 2.1.8. Python for Data Visualization - Plotly and Cufflinks
        • 2.1.8.1.Introduction to Plotly and Cufflinks
        • 2.1.8.2.Plotly and Cufflinks
      • 2.1.9. Python for Data Visualization - Geographical plotting
        • 2.1.9.1.Choropleth Maps - USA
        • 2.1.9.2.Choropleth Maps - World
      • 2.1.10.Combine data analysis and visualization to tackle real world data sets
        • 911 calls capstone project
      • 2.1.11.Linear regression
        • 2.1.11.1.Introduction to Scikit-learn
        • 2.1.11.2.Linear regression with Python
      • 2.1.12.Logistic regression
        • 2.1.12.1.Logistic regression Theory
        • 2.1.12.2.Logistic regression with Python
      • 2.1.13.K Nearest Neighbors
        • 2.1.13.1.KNN Theory
        • 2.1.13.2.KNN with Python
      • 2.1.14.Decision trees and random forests
        • 2.1.14.1.Introduction of tree methods
        • 2.1.14.2.Decision trees and Random Forests with Python
      • 2.1.15.Support Vector Machines
      • 2.1.16.K means clustering
      • 2.1.17.Principal Component Analysis
    • 2.2. Machine Learning Crash Course Jam
Powered by GitBook
On this page
  • 1.Kaggle
  • 2.Emergency - 911 Calls projects
  • 3.讀取資料並了解資料
  • 4.基本分析
  • 5.產生新的feature

Was this helpful?

  1. Chapter 2.Courses
  2. 2.1.Python for Data Science and Machine Learning Bootcamp
  3. 2.1.10.Combine data analysis and visualization to tackle real world data sets

911 calls capstone project

Previous2.1.10.Combine data analysis and visualization to tackle real world data setsNext2.1.11.Linear regression

Last updated 5 years ago

Was this helpful?

1.Kaggle

  • 是一個數據建模和數據分析競賽平台。企業和研究者可在其上發布數據

  • 有眾多策略可以用於解決幾乎所有預測建模的問題, 而研究者不可能在一開始就了解什麼方法對於特定問題是最為有效的, Kaggle的目標則是試圖通過眾包的形式來解決這一難題, 進而使數據科學成為一場運動

2.Emergency - 911 Calls projects

3.讀取資料並了解資料

  1. import numpy as np
    import pandas as pd
  2. import matplotlib.pyplot as plt
    import seaborn as sns
    sns.set_style('whitegrid')
    %matplotlib inline
  3. 讀取資料並分析資料

    df = pd.read_csv('911.csv')
    • 先了解資料欄位的型別以及變數的型態, 由pd.info()可以知道這份資料有9個欄位: 有3筆屬於float, 1筆屬於int64, 5筆屬於object (oject可能是屬於Series)

    df.info()
    • 可以確認前幾筆資料的內容長怎樣, 例如確認前10筆

    df.head(10)
    • 取得資料的基本統計數值: 如數量, 平均值, 標準差, 四分位數等數值

    pf.describe()

4.基本分析

  1. 列出出現次數前幾名的某欄位? 例如列出出現次數前5名的zipcode

    • value_counts會返回一個Series, 預設為由降冪排序, 想改成升冪排序也可以使用ascending=True

    df['zip'].value_counts().head(5)
  2. 某欄位有多少個唯一值? 例如有多少個title

df['title'].nunique()

5.產生新的feature

  1. pf['Reason'] = pf['title'].apply(lambda title: title.split(':')[0])
  2. 911中最常被呼叫的幾個原因為何?

    pf['Reason'].value_counts()
    sns.set_style('whitegrid')
    sns.countplot(x='Reason', data=pf)
  3. 取得timeStamp中的hour

    • 由於在DataFrame中所儲存的timeStamp Series為字串, 需要先轉成datetime

    pf['timeStamp'] = pd.to_datetime(pf['timeStamp'])
    pf['timeStamp'].iloc[0].hour
  4. 將timeStamp中的hour, month, dayofweek存在新欄位中

    • 對pf['timeStamp']中的每一個元素來說, 直接呼叫hour, month, dayofweek就可以取得小時, 月, 日資料

    pf['Hour'] = pf['timeStamp'].apply(lambda time: time.hour)
    pf['Month'] = pf['timeStamp'].apply(lambda time: time.month)
    pf['Day of Week'] = pf['timeStamp'].apply(lambda time: time.dayofweek)
    • 將Day of Week從number轉為string

    dmap = {0:'Mon',1:'Tue',2:'Wed',3:'Thu',4:'Fri',5:'Sat',6:'Sun'}
    pf['Day of Week'] = pf['Day of Week'].map(dmap)
    • 畫出Day of Week的countplot: x軸指定'Day of Week', hue指定'Reason'

    sns.countplot(x='Day of Week', hue = 'Reason', data = pf)
    
    # To relocate the legend
    plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    byMonth = pf.groupby('Month').count()
    • 畫出每個月求救的次數的曲線圖

      byMonth['twp'].plot()
    sns.lmplot(x='Month',y='twp',data=byMonth.reset_index())
  5. 將timeStamp中的date存在新欄位中

    pf['timeStamp'].iloc[0].date()
    pf['Date'] = pf['timeStamp'].apply(lambda t: t.date())
    • 畫出每日的求救次數的曲線圖

    byDate = pf.groupby('Date').count()['twp'].plot()
    plt.tight_layout()
    • Taffic的每日的求救次數曲線圖, 並加上title

    pf[pf['Reason']=='Traffic'].groupby('Date').count()['twp'].plot()
    plt.title('Traffic')
    plt.tight_layout()
    • Fire的每日的求救次數曲線圖, 並加上title

    pf[pf['Fire']=='Traffic'].groupby('Date').count()['twp'].plot()
    plt.title('Fire')
    plt.tight_layout()
    • EMS的每日的求救次數曲線圖, 並加上title

    pf[pf['EMS']=='Traffic'].groupby('Date').count()['twp'].plot()
    plt.title('EMS')
    plt.tight_layout()
    • 先取得一週中每天每小時的各欄位的累積數量

      pf.groupby(by=['Day of Week','Hour']).count()
    • 取出Reason欄位並畫出熱力圖

    dayHour = pf.groupby(by=['Day of Week','Hour']).count()['Reason'].unstack()
    plt.figure(figsize=(12,6))
    sns.heatmap(dayHour,cmap='viridis')
plt.figure(figsize=(12,6))
sns.clustermap(dayHour,cmap='viridis')

在按下Download, 即可下載CSV檔

第一步必須先匯入,

第二步記得將matplotlib的圖表直接嵌入到Notebook之中 (如果是使用jupyter notebook), 並匯入, 等視覺化套件

title的格式是Reason/Department, 例如以EMS: BACK PAINS/INJURY來說, Reason就是EMS, 使用來創造新的Reason欄位

用產生countplot

: x軸指定想要計數的欄位名稱, 指定資料來源

用找出個月份中每個欄位分別的數量

用seaborn來做每個月求救次數的線性回歸圖 (linear fit), 可以使用

x軸資料指定為Month, y軸資料指定為twp, data指定為後的資料

對pf['timeStamp']中的每一個元素來說, 必須呼叫以取得date資料

對每一種撥打求救電話原因, 畫出其每日的求救次數的曲線圖: 先對原始資料進行再plot

資料頁面
pandas
numpy
seaborn
matplotlib
lambda表達式及apply()
seaborn
countplot
groupby
可以使用pandas內建的plot
sns.lmplot
reset_index()
date()
條件過濾
Heatmap分析
Clustermap分析
Kaggle
2017年被google收購