1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt
income = pd.read_excel('D:\Download\income.xlsx')
print(income.apply(lambda x: np.sum(x.isnull())))
income.fillna(value={'workclass': income.workclass.mode()[0], 'occupation': income.occupation.mode()[0], 'native-country': income['native-country'].mode()[0]}, inplace=True)
print(income.describe())
print(income.describe(include=['object']))
plt.style.use('ggplot')
fig, axes = plt.subplots(2, 1)
income.age[income.income == ' <=50K'].plot(kind='kde', label='<=50K', ax=axes[0], legend=True, linestyle='-') income.age[income.income == ' >50K'].plot(kind='kde', label='>50K', ax=axes[0], legend=True, linestyle='--')
income['hours-per-week'][income.income == ' <=50K'].plot(kind='kde', label='<=50K', ax=axes[1], legend=True, linestyle='-') income['hours-per-week'][income.income == ' >50K'].plot(kind='kde', label='>50K', ax=axes[1], legend=True, linestyle='--')
plt.show()
race = pd.DataFrame(income.groupby(by=['race', 'income']).aggregate(np.size).loc[:, 'age'])
race = race.reset_index()
race.rename(columns={'age': 'counts'}, inplace=True)
race.sort_values(by=['race', 'counts'], ascending=False, inplace=True)
relationship = pd.DataFrame(income.groupby(by=['relationship', 'income']).aggregate(np.size).loc[:, 'age']) relationship = relationship.reset_index() relationship.rename(columns={'age': 'counts'}, inplace=True) relationship.sort_values(by=['relationship', 'counts'], ascending=False, inplace=True)
plt.figure(figsize=(9, 5)) sns.barplot(x='race', y='counts', hue='income', data=race) plt.show()
plt.figure(figsize=(9, 5)) sns.barplot(x='relationship', y='counts', hue='income', data=relationship) plt.show()
for feature in income.columns: if income[feature].dtype == 'object': income[feature] = pd.Categorical(income[feature]).codes print(income.head())
income.drop(['education', 'fnlwgt'], axis=1, inplace=True) print(income.head())
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(income.loc[:, 'age':'native-country'], income['income'], train_size=0.75, test_size=0.25, random_state=1234) print('训练数据集共有%d条观测' % X_train.shape[0]) print('训练数据集共有%d条观测' % X_test.shape[0])
from sklearn.neighbors import KNeighborsClassifier
kn = KNeighborsClassifier() kn.fit(X_train, y_train) print(kn)
from sklearn.ensemble import GradientBoostingClassifier
gbdt = GradientBoostingClassifier() gbdt.fit(X_train, y_train) print(gbdt)
from sklearn.grid_search import GridSearchCV
k_options = list(range(1, 12)) parameters = {'n_neighbors': k_options}
grid_kn = GridSearchCV(estimator=KNeighborsClassifier(), param_grid=parameters, cv=10, scoring='accuracy') grid_kn.fit(X_train, y_train)
print(grid_kn.grid_scores_, grid_kn.best_params_, grid_kn.best_score_)
learning_rate_options = [0.01, 0.05, 0.1] max_depth_options = [3, 5, 7, 9] n_estimators_options = [100, 300, 500] parameters = {'learning_rate': learning_rate_options, 'max_depth': max_depth_options, 'n_estimators': n_estimators_options} grid_gbdt = GridSearchCV(estimator=GradientBoostingClassifier(), param_grid=parameters, cv=10, scoring='accuracy') grid_gbdt.fit(X_train, y_train)
print(grid_gbdt.grid_scores_, grid_gbdt.best_params_, grid_gbdt.best_score_)
kn_pred = kn.predict(X_test) print(pd.crosstab(kn_pred, y_test))
print('模型在训练集上的准确率%f' % kn.score(X_train, y_train)) print('模型在测试集上的准确率%f' % kn.score(X_test, y_test))
from sklearn import metrics
fpr, tpr, _ = metrics.roc_curve(y_test, kn.predict_proba(X_test)[:, 1])
plt.plot(fpr, tpr, linestyle='solid', color='red')
plt.stackplot(fpr, tpr, color='steelblue')
plt.plot([0, 1], [0, 1], linestyle='dashed', color='black')
plt.text(0.6, 0.4, 'AUC=%.3f' % metrics.auc(fpr, tpr), fontdict=dict(size=18)) plt.show()
grid_kn_pred = grid_kn.predict(X_test) print(pd.crosstab(grid_kn_pred, y_test))
print('模型在训练集上的准确率%f' % grid_kn.score(X_train, y_train)) print('模型在测试集上的准确率%f' % grid_kn.score(X_test, y_test))
fpr, tpr, _ = metrics.roc_curve(y_test, grid_kn.predict_proba(X_test)[:, 1]) plt.plot(fpr, tpr, linestyle='solid', color='red') plt.stackplot(fpr, tpr, color='steelblue') plt.plot([0, 1], [0, 1], linestyle='dashed', color='black') plt.text(0.6, 0.4, 'AUC=%.3f' % metrics.auc(fpr, tpr), fontdict=dict(size=18)) plt.show()
gbdt_pred = gbdt.predict(X_test) print(pd.crosstab(gbdt_pred, y_test))
print('模型在训练集上的准确率%f' % gbdt.score(X_train, y_train)) print('模型在测试集上的准确率%f' % gbdt.score(X_test, y_test))
fpr, tpr, _ = metrics.roc_curve(y_test, gbdt.predict_proba(X_test)[:, 1]) plt.plot(fpr, tpr, linestyle='solid', color='red') plt.stackplot(fpr, tpr, color='steelblue') plt.plot([0, 1], [0, 1], linestyle='dashed', color='black') plt.text(0.6, 0.4, 'AUC=%.3f' % metrics.auc(fpr, tpr), fontdict=dict(size=18)) plt.show()
grid_gbdt_pred = grid_gbdt.predict(X_test) print(pd.crosstab(grid_gbdt_pred, y_test))
print('模型在训练集上的准确率%f' % grid_gbdt.score(X_train, y_train)) print('模型在测试集上的准确率%f' % grid_gbdt.score(X_test, y_test))
fpr, tpr, _ = metrics.roc_curve(y_test, grid_gbdt.predict_proba(X_test)[:, 1]) plt.plot(fpr, tpr, linestyle='solid', color='red') plt.stackplot(fpr, tpr, color='steelblue') plt.plot([0, 1], [0, 1], linestyle='dashed', color='black') plt.text(0.6, 0.4, 'AUC=%.3f' % metrics.auc(fpr, tpr), fontdict=dict(size=18)) plt.show()
|