我寫這篇文章的目的,是為參加數據科學社區Kaggle簡單指引。 大多數初學者無從下手,因為他們使用自己不理解的庫和算法,就像陷入黑盒。 本教程將通過提供一個框架來教您如何像數據科學家一樣思考與編碼,從而為您提供數據分析的領域優勢。
目錄:
一 、引言:數據科學家如何打敗賠率
二 、 數據科學框架綜述
三、步驟1:明確問題、步驟2:准備數據
四、步驟3:數據清洗
五、數據清理的4 C:糾正,完成,創建和轉換
六、步驟4:進行探索性分析
七、步驟5:模型數據
八、評估模型性能
九、具有超參數的調整模型
十、具有特征選擇的調整模型
十一、步驟6:驗證和實施
十二、步驟7:優化和制定戰略
一 、引言:數據科學家如何打敗賠率
預測二元事件的結果是一個經典的問題。 例如,你贏了或沒贏,你通過測試或沒有通過測試。 常見的業務應用程序是流失或客戶保留。 另一個流行的用例是醫療保健的死亡率或生存分析。 二進制事件創建了一個有趣的動態,因為我們從統計上知道,隨機猜測應該達到50%的准確率,就像投硬幣一樣,而無需創建單個算法或編寫一行代碼。 然而,就像自動更正拼寫檢查技術一樣,有時我們人類可能因為自己的利益而過於聰明,實際上表現不如硬幣翻轉。 在本文中,我使用Kaggle的入門競賽,泰坦尼克號數據,介紹如何使用數據科學框架來克服困難。
二 、 數據科學框架綜述
- 定義問題:俗話說,不要把車放在馬前。在解決問題之前,必須要明白問題是什么,而且可以應用以前的模型或者算法,而不是直接嘗試新的方法。
- 收集數據:約翰·奈斯比特在他1984年的書“大趨勢”中寫道,我們“淹沒在數據中,但仍然需要知識。”所以,數據集已經存在於某個地方,某種格式。可能是外部或內部的,結構化的或非結構化的,靜態的或流式的,客觀的或主觀的等等。俗話說,你不必重新發明輪子,你只需要知道在哪里找到它。在下一步中,我們擔心將“臟數據”轉換為“清理數據”。
- 數據清洗:是將“瘋狂”數據轉換為“可管理”數據的必需過程。數據包括實現用於存儲和處理的數據架構,開發用於質量和控制的數據治理標准,數據提取(即ETL和網絡抓取)以及用於識別異常,丟失或異常數據點的數據清理。
- 探索性分析:任何曾經使用過數據的人都知道,垃圾進入,垃圾進出(GIGO)。因此,部署描述性和圖形化統計信息以查找數據集中的潛在問題,模式,分類,相關性和比較非常重要。此外,數據分類(即定性與定量)對於理解和選擇正確的假設檢驗或數據模型也很重要。
- 模型數據:與描述性和推論性統計數據一樣,數據建模可以匯總數據或預測未來結果。算法是工具而不是魔法棒或銀子彈,你必須知道如何為工作選擇合適工具的主人。錯誤的模型最壞的情況下會導致糟糕的表現和錯誤的結論。
- 驗證和實施數據模型:在根據數據子集訓練模型后,是時候測試模型了。這有助於確保不會過度擬合模型或使其特定於所選子集,因為它不能准確地適合同一數據集中的另一個子集。在這一步中,我們確定我們的模型是否適合,概括或不適合我們的數據集。
- 優化和策略:你在這個過程中重復一遍,讓它更好......更強......比以前更快。作為數據科學家,您的策略應該是將開發人員操作和應用程序管道外包,這樣您就有更多時間專注於建議和設計。
三、步驟1:明確問題、步驟2:准備數據
步驟1:定義問題
對於這個項目,問題陳述在上述計划中已經給出,開發一種算法來預測泰坦尼克號上乘客的生存結果。
......
項目概要:RMS泰坦尼克號沉沒是歷史上最臭名昭着的沉船之一。 1912年4月15日,在她的處女航中,泰坦尼克號在與冰山相撞后沉沒,在2224名乘客和機組人員中造
成1502人死亡。這場聳人聽聞的悲劇震驚了國際社會,並導致了更好的船舶安全規定。
造成海難失事的原因之一是乘客和機組人員沒有足夠的救生艇。盡管幸存下沉有一些運氣因素,但有些人比其他人更容易生存,比如女人,孩子和上流社會。
在這個挑戰中,我們要求您完成對哪些人可能存活的分析。特別是,我們要求您運用機器學習工具來預測哪些乘客幸免於悲劇。
練習技巧
- 二進制分類
- Python知識
第2步:收集數據
Kaggle的泰坦尼克號上的測試和訓練數據在:災難中的機器學習
四、步驟3:數據清洗
步驟3:數據清洗
收集了數據之后,必須對數據進行清洗。
3.1導入所需要的包
下面的代碼是用Python 3.x編寫的。 預先編寫和導入一些庫來執行必要的任務。
# This Python 3 environment comes with many helpful analytics libraries installed # It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python #load packages import sys #access to system parameters https://docs.python.org/3/library/sys.html print("Python version: {}". format(sys.version)) import pandas as pd #collection of functions for data processing and analysis modeled after R dataframes with SQL like features print("pandas version: {}". format(pd.__version__)) import matplotlib #collection of functions for scientific and publication-ready visualization print("matplotlib version: {}". format(matplotlib.__version__)) import numpy as np #foundational package for scientific computing print("NumPy version: {}". format(np.__version__)) import scipy as sp #collection of functions for scientific computing and advance mathematics print("SciPy version: {}". format(sp.__version__)) import IPython from IPython import display #pretty printing of dataframes in Jupyter notebook print("IPython version: {}". format(IPython.__version__)) import sklearn #collection of machine learning algorithms print("scikit-learn version: {}". format(sklearn.__version__)) #misc libraries import random import time #ignore warnings import warnings warnings.filterwarnings('ignore') print('-'*25)
3.11加載數據和建模的庫
我們將使用流行的scikit-learn庫來開發我們的機器學習算法。 在sklearn中,算法稱為Estimators並在其自己的類中實現。 對於數據可視化,我們將使用matplotlib和seaborn庫。 以下是要加載的常見類。
#導入所需要的包 #Common Model Algorithms from sklearn import svm, tree, linear_model, neighbors, naive_bayes, ensemble, discriminant_analysis, gaussian_process from xgboost import XGBClassifier #Common Model Helpers from sklearn.preprocessing import OneHotEncoder, LabelEncoder from sklearn import feature_selection from sklearn import model_selection from sklearn import metrics #Visualization import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.pylab as pylab import seaborn as sns from pandas.tools.plotting import scatter_matrix #Configure Visualization Defaults #%matplotlib inline = show plots in Jupyter Notebook browser %matplotlib inline mpl.style.use('ggplot') sns.set_style('white') pylab.rcParams['figure.figsize'] = 12,8
3.2了解數據類型
通過名字了解數據,並了解它的一些信息。它是什么樣的(數據類型和值),是什么使得它(獨立/特征變量(s)),它的目標是什么(依賴/目標變量)。
要開始此步驟,我們首先導入數據。接下來,我們使用info()和sample()函數來獲得可變數據類型(即定性與定量)。單擊此處獲取源數據。
- 幸存變量(Survived)是我們的結果或因變量。對於幸存者,它是二進制標稱數據類型1,而對於死者,它是0。
- PassengerID和Ticket變量假定為隨機唯一標識符,對結果變量沒有影響。因此,他們將被排除在分析之外。
- Pclass變量是故障單類的序數數據類型,是社會經濟階層(SES):1 =上層,2 =中產階級,3 =下層。
- Name變量是標稱數據類型。它可以用於特征工程中,從標題中獲得性別,從姓氏中獲取家庭大小,從醫生或主人等標題中獲取SES。由於這些變量已經存在,我們將利用它來查看標題,如master,是否有所作為。
- Sex和Embarked變量是名義數據類型。它們將被轉換為虛擬變量以進行數學計算。
- Age和Fare變量是連續的定量數據類型。
- SibSp代表船上相關兄弟姐妹/配偶的數量,Parch代表船上相關父母/子女的數量。兩者都是離散的定量數據類型。這可以用於特征工程來創建族大小並且是單獨變量。
- Cabin變量是一種標稱數據類型,可用於特征工程中事件發生時船舶上的大致位置和甲板水平的SES。但是,由於存在許多空值,因此會從分析中排除。
#import data from file: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html data_raw = pd.read_csv(r'F:\wd.jupyter\datasets\kaggle_data\titanic\train.csv') #a dataset should be broken into 3 splits: train, test, and (final) validation #the test file provided is the validation file for competition submission #we will split the train set into train and test data in future sections data_val = pd.read_csv(r'F:\wd.jupyter\datasets\kaggle_data\titanic\test.csv') #to play with our data we'll create a copy #remember python assignment or equal passes by reference vs values, #so we use the copy function: https://stackoverflow.com/questions/46327494/python-pandas-dataframe-copydeep-false-vs-copydeep-true-vs data1 = data_raw.copy(deep = True) #however passing by reference is convenient, because we can clean both datasets at once data_cleaner = [data1, data_val] #preview data print (data_raw.info()) #https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.info.html #data_raw.head() #https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.head.html #data_raw.tail() #https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.tail.html data_raw.sample(10) #https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sample.html
五、數據清理的4 C:糾正,完成,創建和轉換
5.1 數據清理的4 C:糾正,完成,創建和轉換
在這個階段,我們將清理我們的數據:(1)糾正異常值和異常值,(2)完成缺失信息,(3)創建新的分析功能,(4)將字段轉換為正確的格式進行計算和顯示。
- 更正(correcting):查看數據時,似乎沒有任何異常或不可接受的數據輸入。此外,我們發現我們可能在年齡和票價方面存在潛在的異常值。但是,由於它們是合理的值,我們將等到我們完成探索性分析后確定是否應該包含或排除數據集。應該注意的是,如果它們是不合理的值,例如age= 800而不是80,那么現在修正它十一個正確的決定。但是,當我們從原始值修改數據時,我們要謹慎使用,因為我們需要創建一個准確的模型。
- 完成(completing):年齡,艙室和登船區域中存在空值或缺失數據。丟失的值可能很糟糕,因為某些算法不知道如何處理空值並且會失敗。而其他模型,如決策樹,可以處理空值。因此,在開始建模之前修復是很重要的,因為我們將比較和對比幾個模型。有兩種常用方法,要么刪除記錄,要么使用合理的輸入填充缺失值。建議不要刪除記錄,尤其是大部分記錄,除非它真正代表不完整的記錄。相反,最好填充缺失值。定性數據的基本方法是使用眾數。定量數據的基本方法是使用均值,中位數或均值+隨機標准差來估算。中間方法是使用基於特定標准的基本方法;比如按班級划分的平均年齡或按票價和SES登船。有更復雜的方法,但在使用之前,應將其與基礎模型進行比較,以確定復雜性是否真正增加了價值。對於此數據集,年齡將使用中位數估算,艙室屬性將被刪除,並且將使用模式估算出來。隨后的模型迭代可以修改該決策以確定它是否提高了模型的准確性。
- 創建(creating):特征工程是指我們使用現有功能創建新功能以確定它們是否提供新信號來預測結果。對於此數據集,我們將創建一個標題功能,以確定它是否在生存中發揮作用。
- 轉換(converting):最后,但同樣重要的是,我們將處理格式化。沒有日期或貨幣格式,但數據類型格式。我們的分類數據作為對象導入,這使得數學計算變得困難。對於此數據集,我們將對象數據類型轉換為分類虛擬變量。
print('Train columns with null values:\n', data1.isnull().sum()) print("-"*10) print('Test/Validation columns with null values:\n', data_val.isnull().sum()) print("-"*10) data_raw.describe(include = 'all')
5.2 清洗數據
現在我們知道要清理什么,讓我們執行我們的代碼。
開發者文檔:
- pandas.DataFrame
- pandas.DataFrame.info
- pandas.DataFrame.describe
- Indexing and Selecting Data
- pandas.isnull
- pandas.DataFrame.sum
- pandas.DataFrame.mode
- pandas.DataFrame.copy
- pandas.DataFrame.fillna
- pandas.DataFrame.drop
- pandas.Series.value_counts
- pandas.DataFrame.loc
for dataset in data_cleaner: #complete missing age with median dataset['Age'].fillna(dataset['Age'].median(), inplace = True) #complete embarked with mode dataset['Embarked'].fillna(dataset['Embarked'].mode()[0], inplace = True) #complete missing fare with median dataset['Fare'].fillna(dataset['Fare'].median(), inplace = True) #delete the cabin feature/column and others previously stated to exclude in train dataset drop_column = ['PassengerId','Cabin', 'Ticket'] data1.drop(drop_column, axis=1, inplace = True) print(data1.isnull().sum()) print("-"*10) print(data_val.isnull().sum())
for dataset in data_cleaner: #Discrete variables dataset['FamilySize'] = dataset ['SibSp'] + dataset['Parch'] + 1 dataset['IsAlone'] = 1 #initialize to yes/1 is alone dataset['IsAlone'].loc[dataset['FamilySize'] > 1] = 0 # now update to no/0 if family size is greater than 1 #quick and dirty code split title from name: http://www.pythonforbeginners.com/dictionary/python-split dataset['Title'] = dataset['Name'].str.split(", ", expand=True)[1].str.split(".", expand=True)[0] #Continuous variable bins; qcut vs cut: https://stackoverflow.com/questions/30211923/what-is-the-difference-between-pandas-qcut-and-pandas-cut #Fare Bins/Buckets using qcut or frequency bins: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.qcut.html dataset['FareBin'] = pd.qcut(dataset['Fare'], 4) #Age Bins/Buckets using cut or value bins: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.cut.html dataset['AgeBin'] = pd.cut(dataset['Age'].astype(int), 5) #cleanup rare title names #print(data1['Title'].value_counts()) stat_min = 10 #while small is arbitrary, we'll use the common minimum in statistics: http://nicholasjjackson.com/2012/03/08/sample-size-is-10-a-magic-number/ title_names = (data1['Title'].value_counts() < stat_min) #this will create a true false series with title name as index #apply and lambda functions are quick and dirty code to find and replace with fewer lines of code: https://community.modeanalytics.com/python/tutorial/pandas-groupby-and-python-lambda-functions/ data1['Title'] = data1['Title'].apply(lambda x: 'Misc' if title_names.loc[x] == True else x) print(data1['Title'].value_counts()) print("-"*10) #preview data again data1.info() data_val.info() data1.sample(10)
5.3 轉換格式
我們將分類數據轉換為虛擬變量以進行數學分析。 有多種方法可以對分類變量進行編碼; 我們將使用sklearn和pandas函數。
在此步驟中,我們還將為數據建模定義x(獨立/特征/解釋/預測器/等)和y(依賴/目標/結果/響應/等)變量。
開發者文檔:
- Categorical Encoding
- Sklearn LabelEncoder
- Sklearn OneHotEncoder
- Pandas Categorical dtype
- pandas.get_dummies
label = LabelEncoder() for dataset in data_cleaner: dataset['Sex_Code'] = label.fit_transform(dataset['Sex']) dataset['Embarked_Code'] = label.fit_transform(dataset['Embarked']) dataset['Title_Code'] = label.fit_transform(dataset['Title']) dataset['AgeBin_Code'] = label.fit_transform(dataset['AgeBin']) dataset['FareBin_Code'] = label.fit_transform(dataset['FareBin']) #define y variable aka target/outcome Target = ['Survived'] #define x variables for original features aka feature selection data1_x = ['Sex','Pclass', 'Embarked', 'Title','SibSp', 'Parch', 'Age', 'Fare', 'FamilySize', 'IsAlone'] #pretty name/values for charts data1_x_calc = ['Sex_Code','Pclass', 'Embarked_Code', 'Title_Code','SibSp', 'Parch', 'Age', 'Fare'] #coded for algorithm calculation data1_xy = Target + data1_x print('Original X Y: ', data1_xy, '\n') #define x variables for original w/bin features to remove continuous variables data1_x_bin = ['Sex_Code','Pclass', 'Embarked_Code', 'Title_Code', 'FamilySize', 'AgeBin_Code', 'FareBin_Code'] data1_xy_bin = Target + data1_x_bin print('Bin X Y: ', data1_xy_bin, '\n') #define x and y variables for dummy features original data1_dummy = pd.get_dummies(data1[data1_x]) data1_x_dummy = data1_dummy.columns.tolist() data1_xy_dummy = Target + data1_x_dummy print('Dummy X Y: ', data1_xy_dummy, '\n') data1_dummy.head()
5.4 再次檢查清理數據
現在我們已經清理了我們的數據,讓我們做再次檢查!
print('Train columns with null values: \n', data1.isnull().sum()) print("-"*10) print (data1.info()) print("-"*10) print('Test/Validation columns with null values: \n', data_val.isnull().sum()) print("-"*10) print (data_val.info()) print("-"*10) data_raw.describe(include = 'all')
5.5 拆分訓練和測試數據
如前所述,提供測試的文件實際上是競賽提交的驗證數據。 因此,我們將使用sklearn函數將訓練數據分成兩個數據集; 75/25分裂。 這很重要,所以我們不會過度擬合(overfitting)我們的模型。 意思是,該算法對於給定子集是如此特定,它不能從同一數據集中准確地推廣另一個子集。 重要的是我們的算法沒有看到我們將用於測試的子集,因此它不會通過記憶答案來“欺騙”。 我們將使用sklearn的train_test_split函數。 在后面的部分中,我們還將使用sklearn的交叉驗證函數(cross validation functions,),將我們的數據集拆分為訓練和測試數據建模比較。
#split train and test data with function defaults #random_state -> seed or control random number generator: https://www.quora.com/What-is-seed-in-random-number-generation train1_x, test1_x, train1_y, test1_y = model_selection.train_test_split(data1[data1_x_calc], data1[Target], random_state = 0) train1_x_bin, test1_x_bin, train1_y_bin, test1_y_bin = model_selection.train_test_split(data1[data1_x_bin], data1[Target] , random_state = 0) train1_x_dummy, test1_x_dummy, train1_y_dummy, test1_y_dummy = model_selection.train_test_split(data1_dummy[data1_x_dummy], data1[Target], random_state = 0) print("Data1 Shape: {}".format(data1.shape)) print("Train1 Shape: {}".format(train1_x.shape)) print("Test1 Shape: {}".format(test1_x.shape)) train1_x_bin.head()
六、步驟4:進行探索性分析
現在我們的數據已經清理完畢,我們將使用描述性統計和圖形化統計數據來探索我們的數據。 在這個階段,你會發現自己對特征進行分類並確定它們與目標變量和彼此之間的相關性。
#Discrete Variable Correlation by Survival using #group by aka pivot table: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html for x in data1_x: if data1[x].dtype != 'float64' : print('Survival Correlation by:', x) print(data1[[x, Target[0]]].groupby(x, as_index=False).mean()) print('-'*10, '\n') #using crosstabs: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.crosstab.html print(pd.crosstab(data1['Title'],data1[Target[0]]))
#IMPORTANT: Intentionally plotted different ways for learning purposes only. #optional plotting w/pandas: https://pandas.pydata.org/pandas-docs/stable/visualization.html #we will use matplotlib.pyplot: https://matplotlib.org/api/pyplot_api.html #to organize our graphics will use figure: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.figure.html#matplotlib.pyplot.figure #subplot: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot.html#matplotlib.pyplot.subplot #and subplotS: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html?highlight=matplotlib%20pyplot%20subplots#matplotlib.pyplot.subplots #graph distribution of quantitative data plt.figure(figsize=[16,12]) plt.subplot(231) plt.boxplot(x=data1['Fare'], showmeans = True, meanline = True) plt.title('Fare Boxplot') plt.ylabel('Fare ($)') plt.subplot(232) plt.boxplot(data1['Age'], showmeans = True, meanline = True) plt.title('Age Boxplot') plt.ylabel('Age (Years)') plt.subplot(233) plt.boxplot(data1['FamilySize'], showmeans = True, meanline = True) plt.title('Family Size Boxplot') plt.ylabel('Family Size (#)') plt.subplot(234) plt.hist(x = [data1[data1['Survived']==1]['Fare'], data1[data1['Survived']==0]['Fare']], stacked=True, color = ['g','r'],label = ['Survived','Dead']) plt.title('Fare Histogram by Survival') plt.xlabel('Fare ($)') plt.ylabel('# of Passengers') plt.legend() plt.subplot(235) plt.hist(x = [data1[data1['Survived']==1]['Age'], data1[data1['Survived']==0]['Age']], stacked=True, color = ['g','r'],label = ['Survived','Dead']) plt.title('Age Histogram by Survival') plt.xlabel('Age (Years)') plt.ylabel('# of Passengers') plt.legend() plt.subplot(236) plt.hist(x = [data1[data1['Survived']==1]['FamilySize'], data1[data1['Survived']==0]['FamilySize']], stacked=True, color = ['g','r'],label = ['Survived','Dead']) plt.title('Family Size Histogram by Survival') plt.xlabel('Family Size (#)') plt.ylabel('# of Passengers') plt.legend()
#we will use seaborn graphics for multi-variable comparison: https://seaborn.pydata.org/api.html
#graph individual features by survival fig, saxis = plt.subplots(2, 3,figsize=(16,12)) sns.barplot(x = 'Embarked', y = 'Survived', data=data1, ax = saxis[0,0]) sns.barplot(x = 'Pclass', y = 'Survived', order=[1,2,3], data=data1, ax = saxis[0,1]) sns.barplot(x = 'IsAlone', y = 'Survived', order=[1,0], data=data1, ax = saxis[0,2]) sns.pointplot(x = 'FareBin', y = 'Survived', data=data1, ax = saxis[1,0]) sns.pointplot(x = 'AgeBin', y = 'Survived', data=data1, ax = saxis[1,1]) sns.pointplot(x = 'FamilySize', y = 'Survived', data=data1, ax = saxis[1,2])
#graph distribution of qualitative data: Pclass
#we know class mattered in survival, now let's compare class and a 2nd feature fig, (axis1,axis2,axis3) = plt.subplots(1,3,figsize=(14,12)) sns.boxplot(x = 'Pclass', y = 'Fare', hue = 'Survived', data = data1, ax = axis1) axis1.set_title('Pclass vs Fare Survival Comparison') sns.violinplot(x = 'Pclass', y = 'Age', hue = 'Survived', data = data1, split = True, ax = axis2) axis2.set_title('Pclass vs Age Survival Comparison') sns.boxplot(x = 'Pclass', y ='FamilySize', hue = 'Survived', data = data1, ax = axis3) axis3.set_title('Pclass vs Family Size Survival Comparison')
#graph distribution of qualitative data: Sex
#we know sex mattered in survival, now let's compare sex and a 2nd feature fig, qaxis = plt.subplots(1,3,figsize=(14,12)) sns.barplot(x = 'Sex', y = 'Survived', hue = 'Embarked', data=data1, ax = qaxis[0]) axis1.set_title('Sex vs Embarked Survival Comparison') sns.barplot(x = 'Sex', y = 'Survived', hue = 'Pclass', data=data1, ax = qaxis[1]) axis1.set_title('Sex vs Pclass Survival Comparison') sns.barplot(x = 'Sex', y = 'Survived', hue = 'IsAlone', data=data1, ax = qaxis[2]) axis1.set_title('Sex vs IsAlone Survival Comparison')
#more side-by-side comparisons
fig, (maxis1, maxis2) = plt.subplots(1, 2,figsize=(14,12)) #how does family size factor with sex & survival compare sns.pointplot(x="FamilySize", y="Survived", hue="Sex", data=data1, palette={"male": "blue", "female": "pink"}, markers=["*", "o"], linestyles=["-", "--"], ax = maxis1) #how does class factor with sex & survival compare sns.pointplot(x="Pclass", y="Survived", hue="Sex", data=data1, palette={"male": "blue", "female": "pink"}, markers=["*", "o"], linestyles=["-", "--"], ax = maxis2)
#how does embark port factor with class, sex, and survival compare
#facetgrid: https://seaborn.pydata.org/generated/seaborn.FacetGrid.html e = sns.FacetGrid(data1, col = 'Embarked') e.map(sns.pointplot, 'Pclass', 'Survived', 'Sex', ci=95.0, palette = 'deep') e.add_legend()
#how does embark port factor with class, sex, and survival compare
#facetgrid: https://seaborn.pydata.org/generated/seaborn.FacetGrid.html
a = sns.FacetGrid( data1, hue = 'Survived', aspect=4 ) a.map(sns.kdeplot, 'Age', shade= True ) a.set(xlim=(0 , data1['Age'].max())) a.add_legend()
#histogram comparison of sex, class, and age by survival
h = sns.FacetGrid(data1, row = 'Sex', col = 'Pclass', hue = 'Survived') h.map(plt.hist, 'Age', alpha = .75) h.add_legend()
#pair plots of entire dataset pp = sns.pairplot(data1, hue = 'Survived', palette = 'deep', size=1.2, diag_kind = 'kde', diag_kws=dict(shade=True), plot_kws=dict(s=10) ) pp.set(xticklabels=[])
#correlation heatmap of dataset def correlation_heatmap(df): _ , ax = plt.subplots(figsize =(14, 12)) colormap = sns.diverging_palette(220, 10, as_cmap = True) _ = sns.heatmap( df.corr(), cmap = colormap, square=True, cbar_kws={'shrink':.9 }, ax=ax, annot=True, linewidths=0.1,vmax=1.0, linecolor='white', annot_kws={'fontsize':12 } ) plt.title('Pearson Correlation of Features', y=1.05, size=15) correlation_heatmap(data1)
七、步驟5:模型數據
數據科學是數學、統計學、計算機科學、和商業管理之間的多學科領域。大多數數據科學家來自三個領域之一,因此他們傾向於該學科。然而,數據科學就像一個三腳凳,沒有一條腿比另一條腿更重要。因此,這一步將需要先進的數學知識。但不要擔心,我們只需要一個高級概述,我們將在文章中介紹。此外,由於計算機科學的發展,很多繁重的工作都用計算機完成。因此,曾經需要數學或統計學研究生學位的問題,現在只需要幾行代碼。最后,我們需要一些商業頭腦來思考問題。畢竟,就像訓練寵物一樣,它是向我們學習,需要我們一點一點引導。
機器學習(ML),顧名思義,就是教機器如何思考而不是思考什么。雖然這個話題和大數據已經存在了幾十年,但它正變得比以往任何時候都更受歡迎,因為對於企業和專業人士而言,進入門檻較低。這既好又壞。這很好,因為這些算法現在可供更多人使用,可以解決現實世界中的更多問題。這很糟糕,因為進入門檻較低意味着,更多的人不會知道他們使用的工具,並且會得出錯誤的結論。以前,我曾經比喻過要求別人給你一把螺絲刀,他們會給你一把平頭螺絲刀或最差的錘子。充其量,它表明完全缺乏理解。在最壞的情況下,它使項目變得很差;甚至最糟糕的是,得出錯誤的結論。所以,現在,我會告訴你該怎么做,最重要的是,為什么你這樣做。
首先,你必須明白,機器學習的目的是解決人類的問題。機器學習可分為:監督學習,無監督學習和強化學習。監督學習是指通過向訓練數據集提供包含正確答案的訓練模型來訓練模型的地方。無監督學習是使用不包含正確答案的訓練數據集訓練模型的地方。強化學習是前兩者的混合體,其中模型沒有立即給出正確的答案,但是在一系列事件之后加強學習。我們正在進行有監督的機器學習,因為我們通過提供一組特征及其相應的目標來訓練我們的算法。然后,我們希望從同一數據集中為它提供一個新的子集,並且在預測精度方面具有相似的結果。
有許多機器學習算法,但它們可以簡化為四類:分類,回歸,聚類或降維,具體取決於您的目標變量和數據建模目標。本文中專注於分類和回歸。我們可以概括出連續目標變量需要回歸算法而離散目標變量需要分類算法。一方面注意,邏輯回歸雖然名稱中有回歸,但實際上是一種分類算法。由於我們的問題是預測乘客是否幸存或未幸存,因此這是一個離散的目標變量。我們將使用sklearn庫中的分類算法開始我們的分析。我們將使用交叉驗證和評分指標,在后面的章節中討論,以排名和比較我們的算法的性能。
機器學習模型選擇:
- Sklearn Estimator Overview
- Sklearn Estimator Detail
- Choosing Estimator Mind Map
- Choosing Estimator Cheat Sheet
機器學習常見的分類算法:
- Ensemble Methods
- Generalized Linear Models (GLM)
- Naive Bayes
- Nearest Neighbors
- Support Vector Machines (SVM)
- Decision Trees
- Discriminant Analysis
那么,如何選擇機器學習算法(MLA)?
重要提示:在數據建模方面,初學者的問題始終是“什么是最好的機器學習算法?”對此,初學者必須學習機器學習的免費午餐定理(No Free Lunch Theorem (NFLT))。簡而言之,NFLT指出,對於所有數據集,沒有超級算法在所有情況下都能發揮最佳作用。因此,最好的方法是嘗試多個工作量,調整它們,並根據您的具體情況進行比較。話雖如此,已經進行了一些很好的研究來比較算法,例如 Caruana & Niculescu-Mizil 2006, Ogutu et al. 2011NIH完成基因組選擇, Fernandez-Delgado et al. 2014比較了來自17個家庭的179個分類器,並且還有一種思想流派認為,Thoma 2016 sklearn comparison, 更多的數據打敗了更好的算法。
所有這些信息,初學者在哪里開始?我建議從樹木,套袋,隨機森林和提升(Trees, Bagging, Random Forests, and Boosting)開始。它們基本上是決策樹的不同實現,這是最容易學習和理解的概念。與SVC相比,它們在下一節中也更容易調整。下面,我將概述如何運行和比較幾個MLA,但這個內核的其余部分將側重於通過決策樹及其衍生物學習數據建模。
#Machine Learning Algorithm (MLA) Selection and Initialization MLA = [ #Ensemble Methods ensemble.AdaBoostClassifier(), ensemble.BaggingClassifier(), ensemble.ExtraTreesClassifier(), ensemble.GradientBoostingClassifier(), ensemble.RandomForestClassifier(), #Gaussian Processes gaussian_process.GaussianProcessClassifier(), #GLM linear_model.LogisticRegressionCV(), linear_model.PassiveAggressiveClassifier(), linear_model.RidgeClassifierCV(), linear_model.SGDClassifier(), linear_model.Perceptron(), #Navies Bayes naive_bayes.BernoulliNB(), naive_bayes.GaussianNB(), #Nearest Neighbor neighbors.KNeighborsClassifier(), #SVM svm.SVC(probability=True), svm.NuSVC(probability=True), svm.LinearSVC(), #Trees tree.DecisionTreeClassifier(), tree.ExtraTreeClassifier(), #Discriminant Analysis discriminant_analysis.LinearDiscriminantAnalysis(), discriminant_analysis.QuadraticDiscriminantAnalysis(), #xgboost: http://xgboost.readthedocs.io/en/latest/model.html XGBClassifier() ] #split dataset in cross-validation with this splitter class: http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.ShuffleSplit.html#sklearn.model_selection.ShuffleSplit #note: this is an alternative to train_test_split cv_split = model_selection.ShuffleSplit(n_splits = 10, test_size = .3, train_size = .6, random_state = 0 ) # run model 10x with 60/30 split intentionally leaving out 10% #create table to compare MLA metrics MLA_columns = ['MLA Name', 'MLA Parameters','MLA Train Accuracy Mean', 'MLA Test Accuracy Mean', 'MLA Test Accuracy 3*STD' ,'MLA Time'] MLA_compare = pd.DataFrame(columns = MLA_columns) #create table to compare MLA predictions MLA_predict = data1[Target] #index through MLA and save performance to table row_index = 0 for alg in MLA: #set name and parameters MLA_name = alg.__class__.__name__ MLA_compare.loc[row_index, 'MLA Name'] = MLA_name MLA_compare.loc[row_index, 'MLA Parameters'] = str(alg.get_params()) #score model with cross validation: http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.cross_validate.html#sklearn.model_selection.cross_validate cv_results = model_selection.cross_validate(alg, data1[data1_x_bin], data1[Target], cv = cv_split) MLA_compare.loc[row_index, 'MLA Time'] = cv_results['fit_time'].mean() MLA_compare.loc[row_index, 'MLA Train Accuracy Mean'] = cv_results['train_score'].mean() MLA_compare.loc[row_index, 'MLA Test Accuracy Mean'] = cv_results['test_score'].mean() #if this is a non-bias random sample, then +/-3 standard deviations (std) from the mean, should statistically capture 99.7% of the subsets MLA_compare.loc[row_index, 'MLA Test Accuracy 3*STD'] = cv_results['test_score'].std()*3 #let's know the worst that can happen! #save MLA predictions - see section 6 for usage alg.fit(data1[data1_x_bin], data1[Target]) MLA_predict[MLA_name] = alg.predict(data1[data1_x_bin]) row_index+=1 #print and sort table: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html MLA_compare.sort_values(by = ['MLA Test Accuracy Mean'], ascending = False, inplace = True) MLA_compare #MLA_predict
#barplot using https://seaborn.pydata.org/generated/seaborn.barplot.html sns.barplot(x='MLA Test Accuracy Mean', y = 'MLA Name', data = MLA_compare, color = 'm') #prettify using pyplot: https://matplotlib.org/api/pyplot_api.html plt.title('Machine Learning Algorithm Accuracy Score \n') plt.xlabel('Accuracy Score (%)') plt.ylabel('Algorithm')
八、評估模型性能
讓我們回顧一下,通過一些基本的數據清理,分析和機器學習算法(MLA),我們能夠以大約82%的准確度預測乘客的生存。 幾行代碼也不錯。 但我們一直在問的問題是,我們能投入更多時間,更重要的是獲得更多的投資回報率嗎? 例如,如果我們只是將准確度提高0.1,那么開發真的值3個月。 如果你在研究中工作,答案是肯定的,但如果你在商業中工作,答案肯定是否定的。 因此,在改進模型時請記住這一點。
NOTE:確定基線准確度
在我們決定如何使我們的模型更好之前,讓我們確定我們的模型是否值得保留。要做到這一點,我們必須回到數據科學的基礎.我們知道這是一個二元問題,因為只有兩種可能的結果;乘客幸存或死亡。所以,把它想象成硬幣翻轉問題。如果你有一個公平的硬幣並且猜到了頭或尾,那么你有50%的機會猜測正確。所以,讓我們將50%設為最差的模特表現;因為低於那個,那么為什么我只需要翻硬幣就需要你?
所以,沒有關於數據集的信息,我們總是可以得到50%的二進制問題。但是我們有關於數據集的信息,所以我們應該能夠做得更好。我們知道1,502 / 2,224或67.5%的人死亡。因此,如果我們只是預測最常見的事件,那100%的人死亡,那么我們使用67.5%的概率。所以,讓我們將68%設置為糟糕的模型性能,因為再次,低於此值,那么為什么我需要你,當我可以預測使用最頻繁的事件。
NOTE:如何創建自己的模型
我們的准確性正在提高,但我們可以做得更好嗎?我們的數據中是否有任何信號?為了說明這一點,我們將構建自己的決策樹模型,因為它最容易構思並需要簡單的加法和乘法計算。創建決策樹時,您需要提出分段目標響應的問題,將幸存的1和死亡0放入同類子組中。這是科學和藝術的一部分,所以讓我們玩21個問題的游戲,告訴你它是如何工作的。如果您想自己動手,請下載火車數據集並導入Excel。創建一個數據透視表,其中包含列中的生存,值的行數和計數,以及行中下面描述的功能。
請記住,游戲的名稱是使用決策樹模型創建子組,以便在一個存儲桶中存活1,在另一個存儲桶中存在死0。我們的經驗法則是多數規則。意思是,如果大部分或50%或更多存活,那么我們亞組中的每個人都存活 1,但如果50%或更少存活,那么如果我們子組中的每個人都死亡/0。此外,如果子組小於10或我們的模型精度平穩或降低,我們將停止。
問題1:你是泰坦尼克號嗎?如果是,則大部分(62%)死亡。請注意,我們的樣本存活率與我們68%的人口不同。盡管如此,如果我們假設每個人都死了,我們的樣本准確率為62%。
問題2:你是男性還是女性?男性,大多數(81%)死亡。女性,大多數(74%)幸免於難。給我們79%的准確率。
問題3A(沿着女性分支進行計數= 314):您是第1,2或3級? 1級,大多數(97%)存活,2級,大部分(92%)存活。由於死子組小於10,我們將停止進入該分支。 3級,甚至是50-50分。沒有獲得改進我們模型的新信息。
問題4A(沿着女性3級分支,計數= 144):您是從C,Q還是S出發?我們獲得了一些信息。 C和Q,大部分仍然存活,所以沒有變化。此外,死子組小於10,所以我們將停止。 S,大多數人(63%)死亡。所以,我們將改變女性,第3類,讓S從假設幸存下來,假設他們死了。我們的模型精度提高到81%。
問題5A(從女性班級3開始走向S分支,計數= 88):到目前為止,看起來我們做出了很好的決定。添加另一個級別似乎沒有獲得更多信息。該小組55死亡,33人幸存,因為多數人死亡,我們需要找到一個信號來識別33或一個小組,將他們從死亡變為幸存,並提高我們的模型准確性。我們可以玩我們的功能。我找到的一個是0-8的票價,大多數幸存下來。這是一個11-9的小樣本,但經常用於統計。我們略微提高了准確度,但沒有太多讓我們超過82%。所以,我們會在這里停下來。
問題3B(沿着男性分支進行計數= 577):回到問題2,我們知道大多數男性死亡。因此,我們正在尋找一種能夠識別大多數幸存下來的子群的功能。令人驚訝的是,上課或甚至開始並沒有像女性那樣重要,但是頭銜確實讓我們達到了82%。猜測並檢查其他功能,似乎沒有一個讓我們超過82%。所以,我們暫時停在這里。
你做到了,信息很少,我們的准確率達到了82%。在最糟糕的,壞的,好的,更好的和最好的規模上,我們將82%設置為好,因為它是一個簡單的模型,可以產生不錯的結果。但問題仍然存在,我們能比手工制作的模型更好嗎?
在我們開始之前,讓我們編寫上面剛寫的內容。請注意,這是由“手”創建的手動過程。您不必這樣做,但在開始使用MLA之前了解它非常重要。在微積分考試中將MLA想象成TI-89計算器。它非常強大,可以幫助您完成大量繁重的工作。但如果你不知道你在考試中做了什么,計算器,甚至是TI-89,都不會幫助你通過。所以,明智地研究下一節。
參考:: Cross-Validation and Decision Tree Tutorial
#le/generated/pandas.DataFrame.iterrows.html for index, row in data1.iterrows(): #random number generator: https://docs.python.org/2/library/random.html if random.random() > .5: # Random float x, 0.0 <= x < 1.0 data1.set_value(index, 'Random_Predict', 1) #predict survived/1 else: data1.set_value(index, 'Random_Predict', 0) #predict died/0 #score random guess of survival. Use shortcut 1 = Right Guess and 0 = Wrong Guess #the mean of the column will then equal the accuracy data1['Random_Score'] = 0 #assume prediction wrong data1.loc[(data1['Survived'] == data1['Random_Predict']), 'Random_Score'] = 1 #set to 1 for correct prediction print('Coin Flip Model Accuracy: {:.2f}%'.format(data1['Random_Score'].mean()*100)) #we can also use scikit's accuracy_score function to save us a few lines of code #http://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html#sklearn.metrics.accuracy_score print('Coin Flip Model Accuracy w/SciKit: {:.2f}%'.format(metrics.accuracy_score(data1['Survived'], data1['Random_Predict'])*100))
#e.groupby.html pivot_female = data1[data1.Sex=='female'].groupby(['Sex','Pclass', 'Embarked','FareBin'])['Survived'].mean() print('Survival Decision Tree w/Female Node: \n',pivot_female) pivot_male = data1[data1.Sex=='male'].groupby(['Sex','Title'])['Survived'].mean() print('\n\nSurvival Decision Tree w/Male Node: \n',pivot_male)
def mytree(df): #initialize table to store predictions Model = pd.DataFrame(data = {'Predict':[]}) male_title = ['Master'] #survived titles for index, row in df.iterrows(): #Question 1: Were you on the Titanic; majority died Model.loc[index, 'Predict'] = 0 #Question 2: Are you female; majority survived if (df.loc[index, 'Sex'] == 'female'): Model.loc[index, 'Predict'] = 1 #Question 3A Female - Class and Question 4 Embarked gain minimum information #Question 5B Female - FareBin; set anything less than .5 in female node decision tree back to 0 if ((df.loc[index, 'Sex'] == 'female') & (df.loc[index, 'Pclass'] == 3) & (df.loc[index, 'Embarked'] == 'S') & (df.loc[index, 'Fare'] > 8) ): Model.loc[index, 'Predict'] = 0 #Question 3B Male: Title; set anything greater than .5 to 1 for majority survived if ((df.loc[index, 'Sex'] == 'male') & (df.loc[index, 'Title'] in male_title) ): Model.loc[index, 'Predict'] = 1 return Model #model data Tree_Predict = mytree(data1) print('Decision Tree Model Accuracy/Precision Score: {:.2f}%\n'.format(metrics.accuracy_score(data1['Survived'], Tree_Predict)*100))
#model data Tree_Predict = mytree(data1) print('Decision Tree Model Accuracy/Precision Score: {:.2f}%\n'.format(metrics.accuracy_score(data1['Survived'], Tree_Predict)*100)) #assification_report.html#sklearn.metrics.classification_report #Where recall score = (true positives)/(true positive + false negative) w/1 being best:http://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html#sklearn.metrics.recall_score #And F1 score = weighted average of precision and recall w/1 being best: http://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html#sklearn.metrics.f1_score print(metrics.classification_report(data1['Survived'], Tree_Predict))
import itertools def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') print(cm) plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') # Compute confusion matrix cnf_matrix = metrics.confusion_matrix(data1['Survived'], Tree_Predict) np.set_printoptions(precision=2) class_names = ['Dead', 'Survived'] # Plot non-normalized confusion matrix plt.figure() plot_confusion_matrix(cnf_matrix, classes=class_names, title='Confusion matrix, without normalization') # Plot normalized confusion matrix plt.figure() plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True, title='Normalized confusion matrix')
8.1 交叉驗證(CV)的模型性能
在步驟5.0中,我們使用sklearn cross_validate函數來訓練,測試和評分我們的模型性能。
請記住,重要的是我們使用不同的火車數據子集來構建我們的模型和測試數據來評估我們的模型。否則,我們的模型將被過度裝配。意思是它已經看到了“預測”數據的好處,但是在預測未見到的數據方面很糟糕;這根本不是預測。這就像在學校測驗中作弊獲得100%,但是當你參加考試時,你失敗了,因為你從未真正學到任何東西。機器學習也是如此。
CV基本上是一種快捷方式,可以多次拆分和評分我們的模型,因此我們可以了解它對未見數據的執行情況。它在計算機處理方面要貴一些,但它很重要,所以我們不會獲得虛假的信心。這在Kaggle比賽或任何需要避免一致性和意外的用例中很有用。
除了CV之外,我們還使用了定制的 sklearn train test splitter,,以便在我們的測試評分中獲得更多隨機性。下面是默認CV拆分的圖像。
九、具有超參數的調整模型
當我們使用sklearn決策樹(DT)分類器時(sklearn Decision Tree (DT) Classifier),我們接受了所有函數默認值。 這樣就有機會了解各種超參數設置如何改變模型的准確性。 ( (Click here to learn more about parameters vs hyper-parameters.)。)
但是,為了調整模型,我們需要真正理解它。 這就是為什么我花時間在前幾節中向您展示預測是如何工作的。 現在讓我們更多地了解我們的DT算法。
參考:sklearn
關於決策樹的優缺點,參見我寫的關於決策樹的文章。
以下是可用的超參數和定義:
class sklearn.tree.DecisionTreeClassifier(criterion ='gini',splitter ='best',max_depth = None,min_samples_split = 2,min_samples_leaf = 1,min_weight_fraction_leaf = 0.0,max_features = None,random_state = None,max_leaf_nodes = None,min_impurity_decrease = 0.0 ,min_impurity_split = None,class_weight = None,presort = False)
我們將使用ParameterGrid, GridSearchCV,和自定義的sklearn評分來調整我們的模型; 單擊此處以了解有關ROC_AUC分數的更多信息( sklearn scoring; click here to learn more about ROC_AUC scores)。 然后我們將使用graphviz.可視化我們的樹。 單擊此處以了解有關ROC_AUC分數的更多信息 (Click here to learn more about ROC_AUC scores.)。
#base model dtree = tree.DecisionTreeClassifier(random_state = 0) base_results = model_selection.cross_validate(dtree, data1[data1_x_bin], data1[Target], cv = cv_split) dtree.fit(data1[data1_x_bin], data1[Target]) print('BEFORE DT Parameters: ', dtree.get_params()) print("BEFORE DT Training w/bin score mean: {:.2f}". format(base_results['train_score'].mean()*100)) print("BEFORE DT Test w/bin score mean: {:.2f}". format(base_results['test_score'].mean()*100)) print("BEFORE DT Test w/bin score 3*std: +/- {:.2f}". format(base_results['test_score'].std()*100*3)) #print("BEFORE DT Test w/bin set score min: {:.2f}". format(base_results['test_score'].min()*100)) print('-'*10) #tune hyper-parameters: http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier param_grid = {'criterion': ['gini', 'entropy'], #scoring methodology; two supported formulas for calculating information gain - default is gini #'splitter': ['best', 'random'], #splitting methodology; two supported strategies - default is best 'max_depth': [2,4,6,8,10,None], #max depth tree can grow; default is none #'min_samples_split': [2,5,10,.03,.05], #minimum subset size BEFORE new split (fraction is % of total); default is 2 #'min_samples_leaf': [1,5,10,.03,.05], #minimum subset size AFTER new split split (fraction is % of total); default is 1 #'max_features': [None, 'auto'], #max features to consider when performing split; default none or all 'random_state': [0] #seed or control random number generator: https://www.quora.com/What-is-seed-in-random-number-generation } #print(list(model_selection.ParameterGrid(param_grid))) #choose best model with grid_search: #http://scikit-learn.org/stable/modules/grid_search.html#grid-search #http://scikit-learn.org/stable/auto_examples/model_selection/plot_grid_search_digits.html tune_model = model_selection.GridSearchCV(tree.DecisionTreeClassifier(), param_grid=param_grid, scoring = 'roc_auc', cv = cv_split) tune_model.fit(data1[data1_x_bin], data1[Target]) #print(tune_model.cv_results_.keys()) #print(tune_model.cv_results_['params']) print('AFTER DT Parameters: ', tune_model.best_params_) #print(tune_model.cv_results_['mean_train_score']) print("AFTER DT Training w/bin score mean: {:.2f}". format(tune_model.cv_results_['mean_train_score'][tune_model.best_index_]*100)) #print(tune_model.cv_results_['mean_test_score']) print("AFTER DT Test w/bin score mean: {:.2f}". format(tune_model.cv_results_['mean_test_score'][tune_model.best_index_]*100)) print("AFTER DT Test w/bin score 3*std: +/- {:.2f}". format(tune_model.cv_results_['std_test_score'][tune_model.best_index_]*100*3)) print('-'*10) #duplicates gridsearchcv #tune_results = model_selection.cross_validate(tune_model, data1[data1_x_bin], data1[Target], cv = cv_split) #print('AFTER DT Parameters: ', tune_model.best_params_) #print("AFTER DT Training w/bin set score mean: {:.2f}". format(tune_results['train_score'].mean()*100)) #print("AFTER DT Test w/bin set score mean: {:.2f}". format(tune_results['test_score'].mean()*100)) #print("AFTER DT Test w/bin set score min: {:.2f}". format(tune_results['test_score'].min()*100)) #print('-'*10)
十、具有特征選擇的調整模型
如開頭所述,變量越多,並不意味着模型越好,但正確的預測變量確實如此。 因此,數據建模的另一個步驟是特征選擇。 Sklearn有幾個選項,我們將使用帶有交叉驗證(CV)的遞歸特征消除(RFE)( recursive feature elimination (RFE) with cross validation (CV).)。
#base model print('BEFORE DT RFE Training Shape Old: ', data1[data1_x_bin].shape) print('BEFORE DT RFE Training Columns Old: ', data1[data1_x_bin].columns.values) print("BEFORE DT RFE Training w/bin score mean: {:.2f}". format(base_results['train_score'].mean()*100)) print("BEFORE DT RFE Test w/bin score mean: {:.2f}". format(base_results['test_score'].mean()*100)) print("BEFORE DT RFE Test w/bin score 3*std: +/- {:.2f}". format(base_results['test_score'].std()*100*3)) print('-'*10) #feature selection dtree_rfe = feature_selection.RFECV(dtree, step = 1, scoring = 'accuracy', cv = cv_split) dtree_rfe.fit(data1[data1_x_bin], data1[Target]) #transform x&y to reduced features and fit new model #alternative: can use pipeline to reduce fit and transform steps: http://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html X_rfe = data1[data1_x_bin].columns.values[dtree_rfe.get_support()] rfe_results = model_selection.cross_validate(dtree, data1[X_rfe], data1[Target], cv = cv_split) #print(dtree_rfe.grid_scores_) print('AFTER DT RFE Training Shape New: ', data1[X_rfe].shape) print('AFTER DT RFE Training Columns New: ', X_rfe) print("AFTER DT RFE Training w/bin score mean: {:.2f}". format(rfe_results['train_score'].mean()*100)) print("AFTER DT RFE Test w/bin score mean: {:.2f}". format(rfe_results['test_score'].mean()*100)) print("AFTER DT RFE Test w/bin score 3*std: +/- {:.2f}". format(rfe_results['test_score'].std()*100*3)) print('-'*10) #tune rfe model rfe_tune_model = model_selection.GridSearchCV(tree.DecisionTreeClassifier(), param_grid=param_grid, scoring = 'roc_auc', cv = cv_split) rfe_tune_model.fit(data1[X_rfe], data1[Target]) #print(rfe_tune_model.cv_results_.keys()) #print(rfe_tune_model.cv_results_['params']) print('AFTER DT RFE Tuned Parameters: ', rfe_tune_model.best_params_) #print(rfe_tune_model.cv_results_['mean_train_score']) print("AFTER DT RFE Tuned Training w/bin score mean: {:.2f}". format(rfe_tune_model.cv_results_['mean_train_score'][tune_model.best_index_]*100)) #print(rfe_tune_model.cv_results_['mean_test_score']) print("AFTER DT RFE Tuned Test w/bin score mean: {:.2f}". format(rfe_tune_model.cv_results_['mean_test_score'][tune_model.best_index_]*100)) print("AFTER DT RFE Tuned Test w/bin score 3*std: +/- {:.2f}". format(rfe_tune_model.cv_results_['std_test_score'][tune_model.best_index_]*100*3)) print('-'*10)
#Graph MLA version of Decision Tree: http://scikit-learn.org/stable/modules/generated/sklearn.tree.export_graphviz.html import graphviz dot_data = tree.export_graphviz(dtree, out_file=None, feature_names = data1_x_bin, class_names = True, filled = True, rounded = True) graph = graphviz.Source(dot_data) graph
十一、步驟6:驗證和實施
下一步是使用驗證數據准備提交。
#compare algorithm predictions with each other, where 1 = exactly similar and 0 = exactly opposite #there are some 1's, but enough blues and light reds to create a "super algorithm" by combining them correlation_heatmap(MLA_predict)
#why choose one model, when you can pick them all with voting classifier #http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.VotingClassifier.html #removed models w/o attribute 'predict_proba' required for vote classifier and models with a 1.0 correlation to another model vote_est = [ #Ensemble Methods: http://scikit-learn.org/stable/modules/ensemble.html ('ada', ensemble.AdaBoostClassifier()), ('bc', ensemble.BaggingClassifier()), ('etc',ensemble.ExtraTreesClassifier()), ('gbc', ensemble.GradientBoostingClassifier()), ('rfc', ensemble.RandomForestClassifier()), #Gaussian Processes: http://scikit-learn.org/stable/modules/gaussian_process.html#gaussian-process-classification-gpc ('gpc', gaussian_process.GaussianProcessClassifier()), #GLM: http://scikit-learn.org/stable/modules/linear_model.html#logistic-regression ('lr', linear_model.LogisticRegressionCV()), #Navies Bayes: http://scikit-learn.org/stable/modules/naive_bayes.html ('bnb', naive_bayes.BernoulliNB()), ('gnb', naive_bayes.GaussianNB()), #Nearest Neighbor: http://scikit-learn.org/stable/modules/neighbors.html ('knn', neighbors.KNeighborsClassifier()), #SVM: http://scikit-learn.org/stable/modules/svm.html ('svc', svm.SVC(probability=True)), #xgboost: http://xgboost.readthedocs.io/en/latest/model.html ('xgb', XGBClassifier()) ] #Hard Vote or majority rules vote_hard = ensemble.VotingClassifier(estimators = vote_est , voting = 'hard') vote_hard_cv = model_selection.cross_validate(vote_hard, data1[data1_x_bin], data1[Target], cv = cv_split) vote_hard.fit(data1[data1_x_bin], data1[Target]) print("Hard Voting Training w/bin score mean: {:.2f}". format(vote_hard_cv['train_score'].mean()*100)) print("Hard Voting Test w/bin score mean: {:.2f}". format(vote_hard_cv['test_score'].mean()*100)) print("Hard Voting Test w/bin score 3*std: +/- {:.2f}". format(vote_hard_cv['test_score'].std()*100*3)) print('-'*10) #Soft Vote or weighted probabilities vote_soft = ensemble.VotingClassifier(estimators = vote_est , voting = 'soft') vote_soft_cv = model_selection.cross_validate(vote_soft, data1[data1_x_bin], data1[Target], cv = cv_split) vote_soft.fit(data1[data1_x_bin], data1[Target]) print("Soft Voting Training w/bin score mean: {:.2f}". format(vote_soft_cv['train_score'].mean()*100)) print("Soft Voting Test w/bin score mean: {:.2f}". format(vote_soft_cv['test_score'].mean()*100)) print("Soft Voting Test w/bin score 3*std: +/- {:.2f}". format(vote_soft_cv['test_score'].std()*100*3)) print('-'*10)
#WARNING: Running is very computational intensive and time expensive. #Code is written for experimental/developmental purposes and not production ready! #Hyperparameter Tune with GridSearchCV: http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html grid_n_estimator = [10, 50, 100, 300] grid_ratio = [.1, .25, .5, .75, 1.0] grid_learn = [.01, .03, .05, .1, .25] grid_max_depth = [2, 4, 6, 8, 10, None] grid_min_samples = [5, 10, .03, .05, .10] grid_criterion = ['gini', 'entropy'] grid_bool = [True, False] grid_seed = [0] grid_param = [ [{ #AdaBoostClassifier - http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.AdaBoostClassifier.html 'n_estimators': grid_n_estimator, #default=50 'learning_rate': grid_learn, #default=1 #'algorithm': ['SAMME', 'SAMME.R'], #default=’SAMME.R 'random_state': grid_seed }], [{ #BaggingClassifier - http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.BaggingClassifier.html#sklearn.ensemble.BaggingClassifier 'n_estimators': grid_n_estimator, #default=10 'max_samples': grid_ratio, #default=1.0 'random_state': grid_seed }], [{ #ExtraTreesClassifier - http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesClassifier.html#sklearn.ensemble.ExtraTreesClassifier 'n_estimators': grid_n_estimator, #default=10 'criterion': grid_criterion, #default=”gini” 'max_depth': grid_max_depth, #default=None 'random_state': grid_seed }], [{ #GradientBoostingClassifier - http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingClassifier.html#sklearn.ensemble.GradientBoostingClassifier #'loss': ['deviance', 'exponential'], #default=’deviance’ 'learning_rate': [.05], #default=0.1 -- 12/31/17 set to reduce runtime -- The best parameter for GradientBoostingClassifier is {'learning_rate': 0.05, 'max_depth': 2, 'n_estimators': 300, 'random_state': 0} with a runtime of 264.45 seconds. 'n_estimators': [300], #default=100 -- 12/31/17 set to reduce runtime -- The best parameter for GradientBoostingClassifier is {'learning_rate': 0.05, 'max_depth': 2, 'n_estimators': 300, 'random_state': 0} with a runtime of 264.45 seconds. #'criterion': ['friedman_mse', 'mse', 'mae'], #default=”friedman_mse” 'max_depth': grid_max_depth, #default=3 'random_state': grid_seed }], [{ #RandomForestClassifier - http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier 'n_estimators': grid_n_estimator, #default=10 'criterion': grid_criterion, #default=”gini” 'max_depth': grid_max_depth, #default=None 'oob_score': [True], #default=False -- 12/31/17 set to reduce runtime -- The best parameter for RandomForestClassifier is {'criterion': 'entropy', 'max_depth': 6, 'n_estimators': 100, 'oob_score': True, 'random_state': 0} with a runtime of 146.35 seconds. 'random_state': grid_seed }], [{ #GaussianProcessClassifier 'max_iter_predict': grid_n_estimator, #default: 100 'random_state': grid_seed }], [{ #LogisticRegressionCV - http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegressionCV.html#sklearn.linear_model.LogisticRegressionCV 'fit_intercept': grid_bool, #default: True #'penalty': ['l1','l2'], 'solver': ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'], #default: lbfgs 'random_state': grid_seed }], [{ #BernoulliNB - http://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.BernoulliNB.html#sklearn.naive_bayes.BernoulliNB 'alpha': grid_ratio, #default: 1.0 }], #GaussianNB - [{}], [{ #KNeighborsClassifier - http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html#sklearn.neighbors.KNeighborsClassifier 'n_neighbors': [1,2,3,4,5,6,7], #default: 5 'weights': ['uniform', 'distance'], #default = ‘uniform’ 'algorithm': ['auto', 'ball_tree', 'kd_tree', 'brute'] }], [{ #SVC - http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC #http://blog.hackerearth.com/simple-tutorial-svm-parameter-tuning-python-r #'kernel': ['linear', 'poly', 'rbf', 'sigmoid'], 'C': [1,2,3,4,5], #default=1.0 'gamma': grid_ratio, #edfault: auto 'decision_function_shape': ['ovo', 'ovr'], #default:ovr 'probability': [True], 'random_state': grid_seed }], [{ #XGBClassifier - http://xgboost.readthedocs.io/en/latest/parameter.html 'learning_rate': grid_learn, #default: .3 'max_depth': [1,2,4,6,8,10], #default 2 'n_estimators': grid_n_estimator, 'seed': grid_seed }] ] start_total = time.perf_counter() #https://docs.python.org/3/library/time.html#time.perf_counter for clf, param in zip (vote_est, grid_param): #https://docs.python.org/3/library/functions.html#zip #print(clf[1]) #vote_est is a list of tuples, index 0 is the name and index 1 is the algorithm #print(param) start = time.perf_counter() best_search = model_selection.GridSearchCV(estimator = clf[1], param_grid = param, cv = cv_split, scoring = 'roc_auc') best_search.fit(data1[data1_x_bin], data1[Target]) run = time.perf_counter() - start best_param = best_search.best_params_ print('The best parameter for {} is {} with a runtime of {:.2f} seconds.'.format(clf[1].__class__.__name__, best_param, run)) clf[1].set_params(**best_param) run_total = time.perf_counter() - start_total print('Total optimization time was {:.2f} minutes.'.format(run_total/60)) print('-'*10)
#Hard Vote or majority rules w/Tuned Hyperparameters grid_hard = ensemble.VotingClassifier(estimators = vote_est , voting = 'hard') grid_hard_cv = model_selection.cross_validate(grid_hard, data1[data1_x_bin], data1[Target], cv = cv_split) grid_hard.fit(data1[data1_x_bin], data1[Target]) print("Hard Voting w/Tuned Hyperparameters Training w/bin score mean: {:.2f}". format(grid_hard_cv['train_score'].mean()*100)) print("Hard Voting w/Tuned Hyperparameters Test w/bin score mean: {:.2f}". format(grid_hard_cv['test_score'].mean()*100)) print("Hard Voting w/Tuned Hyperparameters Test w/bin score 3*std: +/- {:.2f}". format(grid_hard_cv['test_score'].std()*100*3)) print('-'*10) #Soft Vote or weighted probabilities w/Tuned Hyperparameters grid_soft = ensemble.VotingClassifier(estimators = vote_est , voting = 'soft') grid_soft_cv = model_selection.cross_validate(grid_soft, data1[data1_x_bin], data1[Target], cv = cv_split) grid_soft.fit(data1[data1_x_bin], data1[Target]) print("Soft Voting w/Tuned Hyperparameters Training w/bin score mean: {:.2f}". format(grid_soft_cv['train_score'].mean()*100)) print("Soft Voting w/Tuned Hyperparameters Test w/bin score mean: {:.2f}". format(grid_soft_cv['test_score'].mean()*100)) print("Soft Voting w/Tuned Hyperparameters Test w/bin score 3*std: +/- {:.2f}". format(grid_soft_cv['test_score'].std()*100*3)) print('-'*10)
#prepare data for modeling
print(data_val.info())
print("-"*10)
#data_val.sample(10)
#handmade decision tree - submission score = 0.77990
data_val['Survived'] = mytree(data_val).astype(int)
#decision tree w/full dataset modeling submission score: defaults= 0.76555, tuned= 0.77990
#submit_dt = tree.DecisionTreeClassifier()
#submit_dt = model_selection.GridSearchCV(tree.DecisionTreeClassifier(), param_grid=param_grid, scoring = 'roc_auc', cv = cv_split)
#submit_dt.fit(data1[data1_x_bin], data1[Target])
#print('Best Parameters: ', submit_dt.best_params_) #Best Parameters: {'criterion': 'gini', 'max_depth': 4, 'random_state': 0}
#data_val['Survived'] = submit_dt.predict(data_val[data1_x_bin])
#bagging w/full dataset modeling submission score: defaults= 0.75119, tuned= 0.77990
#submit_bc = ensemble.BaggingClassifier()
#submit_bc = model_selection.GridSearchCV(ensemble.BaggingClassifier(), param_grid= {'n_estimators':grid_n_estimator, 'max_samples': grid_ratio, 'oob_score': grid_bool, 'random_state': grid_seed}, scoring = 'roc_auc', cv = cv_split)
#submit_bc.fit(data1[data1_x_bin], data1[Target])
#print('Best Parameters: ', submit_bc.best_params_) #Best Parameters: {'max_samples': 0.25, 'n_estimators': 500, 'oob_score': True, 'random_state': 0}
#data_val['Survived'] = submit_bc.predict(data_val[data1_x_bin])
#extra tree w/full dataset modeling submission score: defaults= 0.76555, tuned= 0.77990
#submit_etc = ensemble.ExtraTreesClassifier()
#submit_etc = model_selection.GridSearchCV(ensemble.ExtraTreesClassifier(), param_grid={'n_estimators': grid_n_estimator, 'criterion': grid_criterion, 'max_depth': grid_max_depth, 'random_state': grid_seed}, scoring = 'roc_auc', cv = cv_split)
#submit_etc.fit(data1[data1_x_bin], data1[Target])
#print('Best Parameters: ', submit_etc.best_params_) #Best Parameters: {'criterion': 'entropy', 'max_depth': 6, 'n_estimators': 100, 'random_state': 0}
#data_val['Survived'] = submit_etc.predict(data_val[data1_x_bin])
#random foreset w/full dataset modeling submission score: defaults= 0.71291, tuned= 0.73205
#submit_rfc = ensemble.RandomForestClassifier()
#submit_rfc = model_selection.GridSearchCV(ensemble.RandomForestClassifier(), param_grid={'n_estimators': grid_n_estimator, 'criterion': grid_criterion, 'max_depth': grid_max_depth, 'random_state': grid_seed}, scoring = 'roc_auc', cv = cv_split)
#submit_rfc.fit(data1[data1_x_bin], data1[Target])
#print('Best Parameters: ', submit_rfc.best_params_) #Best Parameters: {'criterion': 'entropy', 'max_depth': 6, 'n_estimators': 100, 'random_state': 0}
#data_val['Survived'] = submit_rfc.predict(data_val[data1_x_bin])
#ada boosting w/full dataset modeling submission score: defaults= 0.74162, tuned= 0.75119
#submit_abc = ensemble.AdaBoostClassifier()
#submit_abc = model_selection.GridSearchCV(ensemble.AdaBoostClassifier(), param_grid={'n_estimators': grid_n_estimator, 'learning_rate': grid_ratio, 'algorithm': ['SAMME', 'SAMME.R'], 'random_state': grid_seed}, scoring = 'roc_auc', cv = cv_split)
#submit_abc.fit(data1[data1_x_bin], data1[Target])
#print('Best Parameters: ', submit_abc.best_params_) #Best Parameters: {'algorithm': 'SAMME.R', 'learning_rate': 0.1, 'n_estimators': 300, 'random_state': 0}
#data_val['Survived'] = submit_abc.predict(data_val[data1_x_bin])
#gradient boosting w/full dataset modeling submission score: defaults= 0.75119, tuned= 0.77033
#submit_gbc = ensemble.GradientBoostingClassifier()
#submit_gbc = model_selection.GridSearchCV(ensemble.GradientBoostingClassifier(), param_grid={'learning_rate': grid_ratio, 'n_estimators': grid_n_estimator, 'max_depth': grid_max_depth, 'random_state':grid_seed}, scoring = 'roc_auc', cv = cv_split)
#submit_gbc.fit(data1[data1_x_bin], data1[Target])
#print('Best Parameters: ', submit_gbc.best_params_) #Best Parameters: {'learning_rate': 0.25, 'max_depth': 2, 'n_estimators': 50, 'random_state': 0}
#data_val['Survived'] = submit_gbc.predict(data_val[data1_x_bin])
#extreme boosting w/full dataset modeling submission score: defaults= 0.73684, tuned= 0.77990
#submit_xgb = XGBClassifier()
#submit_xgb = model_selection.GridSearchCV(XGBClassifier(), param_grid= {'learning_rate': grid_learn, 'max_depth': [0,2,4,6,8,10], 'n_estimators': grid_n_estimator, 'seed': grid_seed}, scoring = 'roc_auc', cv = cv_split)
#submit_xgb.fit(data1[data1_x_bin], data1[Target])
#print('Best Parameters: ', submit_xgb.best_params_) #Best Parameters: {'learning_rate': 0.01, 'max_depth': 4, 'n_estimators': 300, 'seed': 0}
#data_val['Survived'] = submit_xgb.predict(data_val[data1_x_bin])
#hard voting classifier w/full dataset modeling submission score: defaults= 0.75598, tuned = 0.77990
#data_val['Survived'] = vote_hard.predict(data_val[data1_x_bin])
data_val['Survived'] = grid_hard.predict(data_val[data1_x_bin])
#soft voting classifier w/full dataset modeling submission score: defaults= 0.73684, tuned = 0.74162
#data_val['Survived'] = vote_soft.predict(data_val[data1_x_bin])
#data_val['Survived'] = grid_soft.predict(data_val[data1_x_bin])
#submit file
submit = data_val[['PassengerId','Survived']]
submit.to_csv("F:/wd.jupyter/datasets/kaggle_data/titanic/submit.csv", index=False)
print('Validation Data Distribution: \n', data_val['Survived'].value_counts(normalize = True))
submit.sample(10)
十二、步驟7:優化和制定戰略
結論
我們的模型收斂於0.77990提交准確性。使用相同的數據集和決策樹(adaboost,隨機森林,梯度增強,xgboost等)的不同實現與調整不超過0.77990提交准確性。有趣的是,對此數據集,簡單決策樹算法具有最佳默認提交分數,並且調整獲得了相同的最佳准確度分數。
雖然在單個數據集上測試少量算法無法得出一般結論,但對所提到的數據集有幾個觀察結果。
- 訓練數據集具有與測試/驗證數據集和群體不同的分布。這在交叉驗證(CV)准確度分數和Kaggle提交准確度分數之間創造了廣泛的差距。
- 給定相同的數據集,基於決策樹的算法在適當調整后似乎收斂於相同的准確度分數。盡管進行調整,
對於迭代二,我會花更多的時間在預處理和特征工程上。為了更好地調整CV分數和Kaggle分數並提高整體准確性。