決策樹、隨機森林整合演算法(Titanic例項)
阿新 • • 發佈:2018-12-10
#coding:utf-8 import pandas #ipython notebook titanic = pandas.read_csv("titanic_train.csv") titanic.head(5) #print (titanic.describe()) titanic["Age"] = titanic["Age"].fillna(titanic["Age"].median()) print(titanic.describe()) print(titanic["Sex"].unique())#看一下Sex裡有幾種可能性 #將字元轉換成數字 # Replace all the occurences of male with the number 0. titanic.loc[titanic["Sex"] == "male", "Sex"] = 0 titanic.loc[titanic["Sex"] == "female", "Sex"] = 1 print(titanic["Embarked"].unique()) titanic["Embarked"] = titanic["Embarked"].fillna('S') titanic.loc[titanic["Embarked"] == "S", "Embarked"] = 0 titanic.loc[titanic["Embarked"] == "C", "Embarked"] = 1 titanic.loc[titanic["Embarked"] == "Q", "Embarked"] = 2 from sklearn.linear_model import LinearRegression # Sklearn also has a helper that makes it easy to do cross validation from sklearn.cross_validation import KFold # The columns we'll use to predict the target我們用來預測目標的列 predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"] # Initialize our algorithm class初始化我們的演算法類 alg = LinearRegression() # Generate cross validation folds for the titanic dataset. It return the row indices corresponding to train and test. #為泰坦尼克資料集生成交叉驗證摺疊。它返回對應於訓練和測試的行索引 # We set random_state to ensure we get the same splits every time we run this. #我們設定隨機狀態以確保每次執行時都得到相同的分割。 kf = KFold(titanic.shape[0], n_folds=3, random_state=1) predictions = [] for train, test in kf:#把kf裡的前幾份當成train,最後一份當成test # The predictors we're using the train the algorithm. Note how we only take the rows in the train folds. #我們使用訓練的預測器演算法,注意我們如何只在訓練摺疊中取行 train_predictors = (titanic[predictors].iloc[train,:]) # The target we're using to train the algorithm.我們用來訓練演算法的目標 train_target = titanic["Survived"].iloc[train] # Training the algorithm using the predictors and target.使用預測器和目標訓練演算法。 alg.fit(train_predictors, train_target) # We can now make predictions on the test fold我們現在可以對測試摺疊做出預測 test_predictions = alg.predict(titanic[predictors].iloc[test, :]) # 這些預測是在三個獨立的數字陣列中進行的。連線成一個。 predictions.append(test_predictions) import numpy as np #這些預測是在三個獨立的數字陣列中進行的。連線成一個。 # The predictions are in three separate numpy arrays. Concatenate them into one. # We concatenate them on axis 0, as they only have one axis. #我們將它們連線在軸0上,因為它們只有一個軸。 predictions = np.concatenate(predictions, axis=0) # Map predictions to outcomes (only possible outcomes are 1 and 0) #將預測對映到結果(只有可能的結果是1和0) predictions[predictions > .5] = 1 predictions[predictions <=.5] = 0 #準確率,當[predictions == titanic["Survived"]survived與預測值相等時即預測正確 accuracy = sum(predictions[predictions == titanic["Survived"]]) / len(predictions) print(accuracy) #邏輯迴歸 from sklearn import cross_validation from sklearn.linear_model import LogisticRegression # Initialize our algorithm alg = LogisticRegression(random_state=1) # Compute the accuracy score for all the cross validation folds. (much simpler than what we did before!) scores = cross_validation.cross_val_score(alg, titanic[predictors], titanic["Survived"], cv=3) # Take the mean of the scores (because we have one for each fold) print(scores.mean()) #用測試集測試 titanic_test = pandas.read_csv("test.csv") titanic_test["Age"] = titanic_test["Age"].fillna(titanic["Age"].median()) titanic_test["Fare"] = titanic_test["Fare"].fillna(titanic_test["Fare"].median()) titanic_test.loc[titanic_test["Sex"] == "male", "Sex"] = 0 titanic_test.loc[titanic_test["Sex"] == "female", "Sex"] = 1 titanic_test["Embarked"] = titanic_test["Embarked"].fillna("S") titanic_test.loc[titanic_test["Embarked"] == "S", "Embarked"] = 0 titanic_test.loc[titanic_test["Embarked"] == "C", "Embarked"] = 1 titanic_test.loc[titanic_test["Embarked"] == "Q", "Embarked"] = 2 #隨機森林 from sklearn import cross_validation from sklearn.ensemble import RandomForestClassifier predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"] # Initialize our algorithm with the default paramters # n_estimators is the number of trees we want to make # min_samples_split is the minimum number of rows we need to make a split # min_samples_leaf is the minimum number of samples we can have at the place where a tree branch ends (the bottom points of the tree) #決策樹10棵,min_samples_leaf=1葉子節點的最小個數為1 alg = RandomForestClassifier(random_state=1, n_estimators=10, min_samples_split=2, min_samples_leaf=1) # Compute the accuracy score for all the cross validation folds. (much simpler than what we did before!) #n_folds=3將訓練資料分成三組,兩組用於訓練,一組用於測試 kf = cross_validation.KFold(titanic.shape[0], n_folds=3, random_state=1) scores = cross_validation.cross_val_score(alg, titanic[predictors], titanic["Survived"], cv=kf) # Take the mean of the scores (because we have one for each fold) print(scores.mean()) #對模型進行引數調優 alg = RandomForestClassifier(random_state=1, n_estimators=100, min_samples_split=4, min_samples_leaf=2) # Compute the accuracy score for all the cross validation folds. (much simpler than what we did before!) kf = cross_validation.KFold(titanic.shape[0], 3, random_state=1) scores = cross_validation.cross_val_score(alg, titanic[predictors], titanic["Survived"], cv=kf) # Take the mean of the scores (because we have one for each fold) print(scores.mean()) #建立兩列 # Generating a familysize column titanic["FamilySize"] = titanic["SibSp"] + titanic["Parch"] # The .apply method generates a new series titanic["NameLength"] = titanic["Name"].apply(lambda x: len(x)) import re # A function to get the title from a name.從名稱中獲得標題的函式 def get_title(name): #使用正則表示式搜尋標題。標題總是由大寫字母和小寫字母組成,以句號結尾。 # Use a regular expression to search for a title. Titles always consist of capital and lowercase letters, and end with a period. title_search = re.search(' ([A-Za-z]+)\.', name) # If the title exists, extract and return it.如果標題存在,提取並返回它 if title_search: return title_search.group(1) return "" #獲取所有的標題並列印每一個的頻率 titles = titanic["Name"].apply(get_title) #value_counts返回的是該titles物件中獨一無二的元素的個數 print(pandas.value_counts(titles)) #將每個標題對映到一個整數。有些標題非常少見,並且被壓縮成與其他標題相同的程式碼。 # Map each title to an integer. Some titles are very rare, and are compressed into the same codes as other titles. title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Dr": 5, "Rev": 6, "Major": 7, "Col": 7, "Mlle": 8, "Mme": 8, "Don": 9, "Lady": 10, "Countess": 10, "Jonkheer": 10, "Sir": 9, "Capt": 7, "Ms": 2} for k,v in title_mapping.items(): titles[titles == k] = v print(pandas.value_counts(titles)) # Add in the title column.新增標題欄 titanic["Title"] = titles #觀察特徵的重要性 import numpy as np from sklearn.feature_selection import SelectKBest, f_classif import matplotlib.pyplot as plt predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked", "FamilySize", "Title", "NameLength"] # Perform feature selection進行特徵選擇 selector = SelectKBest(f_classif, k=5) selector.fit(titanic[predictors], titanic["Survived"]) # # Get the raw p-values for each feature, and transform from p-values into scores scores = -np.log10(selector.pvalues_) # Plot the scores. See how "Pclass", "Sex", "Title", and "Fare" are the best? plt.bar(range(len(predictors)), scores) plt.xticks(range(len(predictors)), predictors, rotation='vertical') plt.show() # Pick only the four best features. predictors = ["Pclass", "Sex", "Fare", "Title"] alg = RandomForestClassifier(random_state=1, n_estimators=50, min_samples_split=8, min_samples_leaf=4) from sklearn.ensemble import GradientBoostingClassifier import numpy as np # The algorithms we want to ensemble. # We're using the more linear predictors for the logistic regression, and everything with the gradient boosting classifier. algorithms = [ [GradientBoostingClassifier(random_state=1, n_estimators=25, max_depth=3), ["Pclass", "Sex", "Age", "Fare", "Embarked", "FamilySize", "Title",]], [LogisticRegression(random_state=1), ["Pclass", "Sex", "Fare", "FamilySize", "Title", "Age", "Embarked"]] ] # Initialize the cross validation folds kf = KFold(titanic.shape[0], n_folds=3, random_state=1) predictions = [] for train, test in kf: train_target = titanic["Survived"].iloc[train] full_test_predictions = [] # Make predictions for each algorithm on each fold for alg, predictors in algorithms: # Fit the algorithm on the training data. alg.fit(titanic[predictors].iloc[train, :], train_target) # Select and predict on the test fold. # The .astype(float) is necessary to convert the dataframe to all floats and avoid an sklearn error. test_predictions = alg.predict_proba(titanic[predictors].iloc[test, :].astype(float))[:, 1] full_test_predictions.append(test_predictions) # Use a simple ensembling scheme -- just average the predictions to get the final classification. #使用一個簡單的組合方案——只是平均預測來得到最終的分類。 test_predictions = (full_test_predictions[0] + full_test_predictions[1]) / 2 # Any value over .5 is assumed to be a 1 prediction, and below .5 is a 0 prediction. #任何超過0.5的值都被認為是一個1的預測,而低於0.5是一個0的預測。 test_predictions[test_predictions <= .5] = 0 test_predictions[test_predictions > .5] = 1 predictions.append(test_predictions) # Put all the predictions together into one array. predictions = np.concatenate(predictions, axis=0) # Compute accuracy by comparing to the training data. accuracy = sum(predictions[predictions == titanic["Survived"]]) / len(predictions) print(accuracy)