-
Notifications
You must be signed in to change notification settings - Fork 0
/
shotTest.py
580 lines (467 loc) · 20 KB
/
shotTest.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# -*- coding: UTF-8 -*-
#%matplotlib inline
#1 Load libraries
#Load libraries
import warnings
warnings.filterwarnings('ignore')
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.decomposition import PCA, KernelPCA
from sklearn.cross_validation import KFold, cross_val_score
from sklearn.metrics import make_scorer
from sklearn.grid_search import GridSearchCV
from sklearn.feature_selection import VarianceThreshold, RFE, SelectKBest, chi2
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import BaggingClassifier, ExtraTreesClassifier, GradientBoostingClassifier, VotingClassifier, RandomForestClassifier, AdaBoostClassifier
sns.set_style('whitegrid')
pd.set_option('display.max_columns', None) # display all columns
#Load dataset,convert some columns to category type
'''
问:category和object是什么类别?
答:翻阅了pandas官网没太看明白。If a pandas object contains data multiple dtypes IN A SINGLE COLUMN, the dtype of the column will be chosen to accommodate all of the data types (object is the most general)
This is an introduction to pandas categorical data type, including a short comparison with R’s factor.
Categoricals are a pandas data type, which correspond to categorical variables in statistics: a variable, which can take on only a limited, and usually fixed, number of possible values (categories; levels in R). Examples are gender, social class, blood types, country affiliations, observation time or ratings via Likert scales.
In contrast to statistical categorical variables, categorical data might have an order (e.g. ‘strongly agree’ vs ‘agree’ or ‘first observation’ vs. ‘second observation’), but numerical operations (additions, divisions, ...) are not possible.
All values of categorical data are either in categories or np.nan. Order is defined by the order of categories, not lexical order of the values. Internally, the data structure consists of a categories array and an integer array of codeswhich point to the real value in the categories array.
The categorical data type is useful in the following cases:
A string variable consisting of only a few different values. Converting such a string variable to a categorical variable will save some memory, see here.
The lexical order of a variable is not the same as the logical order (“one”, “two”, “three”). By converting to a categorical and specifying an order on the categories, sorting and min/max will use the logical order instead of the lexical order, see here.
As a signal to other python libraries that this column should be treated as a categorical variable (e.g. to use suitable statistical methods or plot types).
See also the API docs on categoricals.
'''
data = pd.read_csv('../kobe-bryant-shot-selection/Data/data.csv') #..是pycharm的默认项目路径,在setting中可以设置
data.set_index('shot_id', inplace=True)
data["action_type"] = data["action_type"].astype('object')
data["combined_shot_type"] = data["combined_shot_type"].astype('category')
data["game_event_id"] = data["game_event_id"].astype('category')
data["game_id"] = data["game_id"].astype('category')
data["period"] = data["period"].astype('object')
data["playoffs"] = data["playoffs"].astype('category')
data["season"] = data["season"].astype('category')
data["shot_made_flag"] = data["shot_made_flag"].astype('category')
data["shot_type"] = data["shot_type"].astype('category')
data["team_id"] = data["team_id"].astype('category')
#Quick look:
print("第一行 第二行")
print(data.head(2)) #看前两行
print('\n' * 2)
print("每个特征的类型")
print(data.dtypes) #看每个特征的类型
print('\n' * 2)
#2 Summarize data
#Descriptive statistics
print("有多少行、多少列")
print(data.shape)
print('\n' * 2)
#all numerical columns statistcs:
print("数值型数据")
print(data.describe(include=['number']))
print('\n' * 2)
#categorical columns:
print("非数值型数据")
print(data.describe(include=['object', 'category']))
print('\n' * 2)
#Data Visualization
#直方图
'''
问:以下代码什么意思?
答:在matplotlib中,整个图像为一个Figure对象。在Figure对象中可以包含一个或者多个Axes对象。每个Axes(ax)对象都是一个拥有自己坐标系统的绘图区域。
https://www.cnblogs.com/nju2014/p/5620776.html
'''
ax = plt.axes()
sns.countplot(x='shot_made_flag', data=data, ax=ax);
ax.set_title('Target class distribution')
plt.show()
#boxplot
f, axarr = plt.subplots(4, 2, figsize=(15, 15))
sns.boxplot(x='lat', y='shot_made_flag', data=data, showmeans=True, ax=axarr[0,0])
sns.boxplot(x='lon', y='shot_made_flag', data=data, showmeans=True, ax=axarr[0, 1])
sns.boxplot(x='loc_y', y='shot_made_flag', data=data, showmeans=True, ax=axarr[1, 0])
sns.boxplot(x='loc_x', y='shot_made_flag', data=data, showmeans=True, ax=axarr[1, 1])
sns.boxplot(x='minutes_remaining', y='shot_made_flag', showmeans=True, data=data, ax=axarr[2, 0])
sns.boxplot(x='seconds_remaining', y='shot_made_flag', showmeans=True, data=data, ax=axarr[2, 1])
sns.boxplot(x='shot_distance', y='shot_made_flag', data=data, showmeans=True, ax=axarr[3, 0])
axarr[0, 0].set_title('Latitude')
axarr[0, 1].set_title('Longitude')
axarr[1, 0].set_title('Loc y')
axarr[1, 1].set_title('Loc x')
axarr[2, 0].set_title('Minutes remaining')
axarr[2, 1].set_title('Seconds remaining')
axarr[3, 0].set_title('Shot distance')
plt.tight_layout()
plt.show()
#多变量作图
'''
问:sns.pairplot()的图没看懂。
答:多变量作图
seaborn可以一次性两两组合多个变量做出多个对比图,有n个变量,就会做出一个n × n个格子的图,譬如有2个变量,就会产生4个格子,每个格子就是两个变量之间的对比图
var1 vs var1
var1 vs var2
var2 vs var1
var2 vs var2
相同的两个变量之间(var1 vs var1 和 var2 vs var2)以直方图展示,不同的变量则以散点图展示(var1 vs var2 和var2 vs var1)
要注意的是数据中不能有NaN(缺失的数据),否则会报错
'''
sns.pairplot(data, vars=['loc_x', 'loc_y', 'lat', 'lon', 'shot_distance'], hue='shot_made_flag', size=3)
plt.show()
#countplot
f, axarr = plt.subplots(8, figsize=(15, 25))
sns.countplot(x="combined_shot_type", hue="shot_made_flag", data=data, ax=axarr[0])
sns.countplot(x="season", hue="shot_made_flag", data=data, ax=axarr[1])
sns.countplot(x="period", hue="shot_made_flag", data=data, ax=axarr[2])
sns.countplot(x="playoffs", hue="shot_made_flag", data=data, ax=axarr[3])
sns.countplot(x="shot_type", hue="shot_made_flag", data=data, ax=axarr[4])
sns.countplot(x="shot_zone_area", hue="shot_made_flag", data=data, ax=axarr[5])
sns.countplot(x="shot_zone_basic", hue="shot_made_flag", data=data, ax=axarr[6])
sns.countplot(x="shot_zone_range", hue="shot_made_flag", data=data, ax=axarr[7])
axarr[0].set_title('Combined shot type')
axarr[1].set_title('Season')
axarr[2].set_title('Period')
axarr[3].set_title('Playoffs')
axarr[4].set_title('Shot Type')
axarr[5].set_title('Shot Zone Area')
axarr[6].set_title('Shot Zone Basic')
axarr[7].set_title('Shot Zone Range')
plt.tight_layout()
plt.show()
#3. Prepare Data
unknown_mask = data['shot_made_flag'].isnull()
#Data Cleaning
data_cl = data.copy() # create a copy of data frame
target = data_cl['shot_made_flag'].copy()
# Remove some columns
data_cl.drop('team_id', axis=1, inplace=True) # Always one number
data_cl.drop('lat', axis=1, inplace=True) # Correlated with loc_x
data_cl.drop('lon', axis=1, inplace=True) # Correlated with loc_y
data_cl.drop('game_id', axis=1, inplace=True) # Independent
data_cl.drop('game_event_id', axis=1, inplace=True) # Independent
data_cl.drop('team_name', axis=1, inplace=True) # Always LA Lakers
data_cl.drop('shot_made_flag', axis=1, inplace=True)
#There are also many outliers, remove them:
'''
问:以下代码什么意思?
答:异常值检测 原理我还不知道
'''
def detect_outliers(series, whis=1.5):
q75, q25 = np.percentile(series, [75 ,25])
iqr = q75 - q25
return ~((series - series.median()).abs() <= (whis * iqr))
# For now - do not remove anything
#Data Transformation
#New features
# Remaining time
data_cl['seconds_from_period_end'] = 60 * data_cl['minutes_remaining'] + data_cl['seconds_remaining']
data_cl['last_5_sec_in_period'] = data_cl['seconds_from_period_end'] < 5 #增加last特征,若剩余时间小于5秒,则此值为1
data_cl.drop('minutes_remaining', axis=1, inplace=True)
data_cl.drop('seconds_remaining', axis=1, inplace=True)
data_cl.drop('seconds_from_period_end', axis=1, inplace=True)
## Matchup - (away/home)
data_cl['home_play'] = data_cl['matchup'].str.contains('vs').astype('int') #新增特征home_play,主场的此特征值为1,客场则此特征值为0
data_cl.drop('matchup', axis=1, inplace=True)
# Game date
data_cl['game_date'] = pd.to_datetime(data_cl['game_date'])
data_cl['game_year'] = data_cl['game_date'].dt.year #添加year特征年
data_cl['game_month'] = data_cl['game_date'].dt.month #添加month特征月
data_cl.drop('game_date', axis=1, inplace=True) #为什么不添加天?
# Loc_x, and loc_y binning
data_cl['loc_x'] = pd.cut(data_cl['loc_x'], 25) #25是什么意思?
data_cl['loc_y'] = pd.cut(data_cl['loc_y'], 25)
# Replace 20 least common action types with value 'Other'
#对投篮方式进行排序,选出最少的20种标记为其他。
rare_action_types = data_cl['action_type'].value_counts().sort_values().index.values[:20]
data_cl.loc[data_cl['action_type'].isin(rare_action_types), 'action_type'] = 'Other'
#Encode categorical variables
categorial_cols = [
'action_type', 'combined_shot_type', 'period', 'season', 'shot_type',
'shot_zone_area', 'shot_zone_basic', 'shot_zone_range', 'game_year',
'game_month', 'opponent', 'loc_x', 'loc_y']
#离散特征编码,one-hot编码
'''
问:解释get_dummies()
答:离散特征的编码分为两种情况:
1、离散特征的取值之间没有大小的意义,比如color:[red,blue],那么就使用one-hot编码。将分类变量(categorical variable)转换为“哑变量矩阵”(dummy matrix)或“指标矩阵”(indicator matrix)。如果DataFrame的某一列中含有k个不同的值,则可以派生出一个k列矩阵或DataFrame(其值全为1和0)。
2、离散特征的取值有大小的意义,比如size:[X,XL,XXL],那么就使用数值的映射{X:1,XL:2,XXL:3}
one-hot编码举例:
>>> s = pd.Series(list('abca'))
>>> pd.get_dummies(s)
a b c
0 1 0 0
1 0 1 0
2 0 0 1
3 1 0 0
'''
for cc in categorial_cols:
dummies = pd.get_dummies(data_cl[cc])
dummies = dummies.add_prefix("{}#".format(cc)) #加前缀?没明白
data_cl.drop(cc, axis=1, inplace=True) #删除cc列,即原来的categories列
data_cl = data_cl.join(dummies) #增加新的dunniies列
#Feature Selection
# Separate dataset for validation
data_submit = data_cl[unknown_mask] #测试集,本身标签值为空
# Separate dataset for training
X = data_cl[~unknown_mask] #训练集(带标签)
Y = target[~unknown_mask] #训练集的标签
#Variance Threshold
#Find all features with more than 90% variance in values.
threshold = 0.90
vt = VarianceThreshold().fit(X)
# Find feature names
'''
问:解释以下代码。
答:
'''
feat_var_threshold = data_cl.columns[vt.variances_ > threshold * (1-threshold)]
print("Variance Threshold选出的特征")
print(feat_var_threshold)
print('\n' * 2)
#Top 20 most important features
#According to RandomForestClassifier
model = RandomForestClassifier()
model.fit(X, Y)
feature_imp = pd.DataFrame(model.feature_importances_, index=X.columns, columns=["importance"])
feat_imp_20 = feature_imp.sort_values("importance", ascending=False).head(20).index
print("RandomForest选出的特征")
print(feat_imp_20)
print('\n' * 2)
#Univariate feature selection
#Select top 20 features using chi2chi2 test. Features must be positive before applying test.
X_minmax = MinMaxScaler(feature_range=(0,1)).fit_transform(X)
X_scored = SelectKBest(score_func=chi2, k='all').fit(X_minmax, Y)
feature_scoring = pd.DataFrame({
'feature': X.columns,
'score': X_scored.scores_
})
feat_scored_20 = feature_scoring.sort_values('score', ascending=False).head(20)['feature'].values
print("Univariate选出的特征")
print(feat_scored_20)
print('\n' * 2)
#Recursive Feature Elimination
rfe = RFE(LogisticRegression(), 20)
rfe.fit(X, Y)
feature_rfe_scoring = pd.DataFrame({
'feature': X.columns,
'score': rfe.ranking_
})
feat_rfe_20 = feature_rfe_scoring[feature_rfe_scoring['score'] == 1]['feature'].values
print("Recursive选出的特征")
print(feat_rfe_20)
print('\n' * 2)
#Final feature selection
#merged together
features = np.hstack([
feat_var_threshold,
feat_imp_20,
feat_scored_20,
feat_rfe_20
])
features = np.unique(features)
print('Final features set:\n')
for f in features:
print("\t-{}".format(f))
#Prepare dataset for further analysis
#注释:http://blog.csdn.net/xw_classmate/article/details/51333646
#选出features特征的数据
data_cl = data_cl.ix[:, features]
data_submit = data_submit.ix[:, features]
X = X.ix[:, features]
print('Clean dataset shape: {}'.format(data_cl.shape))
print('Subbmitable dataset shape: {}'.format(data_submit.shape))
print('Train features shape: {}'.format(X.shape))
print('Target label shape: {}'. format(Y.shape))
#PCA Visualization
#PCA https://github.com/worldveil/dejavu
components = 8
pca = PCA(n_components=components).fit(X)
pca_variance_explained_df = pd.DataFrame({
"component": np.arange(1, components+1),
"variance_explained": pca.explained_variance_ratio_
})
ax = sns.barplot(x='component', y='variance_explained', data=pca_variance_explained_df)
ax.set_title("PCA - Variance explained")
plt.show()
X_pca = pd.DataFrame(pca.transform(X)[:,:2])
X_pca['target'] = Y.values
X_pca.columns = ["x", "y", "target"]
sns.lmplot('x','y',
data=X_pca,
hue="target",
fit_reg=False,
markers=["o", "x"],
palette="Set1",
size=7,
scatter_kws={"alpha": .2}
)
plt.show()
#4. Evaluate Algorithms
#设置参数
seed = 7
processors=1
num_folds=3
num_instances=len(X)
scoring='log_loss'
kfold = KFold(n=num_instances, n_folds=num_folds, random_state=seed)
# Algorithms spot-check
# Prepare some basic models
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('K-NN', KNeighborsClassifier(n_neighbors=5)))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
# models.append(('SVC', SVC(probability=True)))
# Evaluate each model in turn
results = []
names = []
for name, model in models:
cv_results = cross_val_score(model, X, Y, cv=kfold, scoring=scoring, n_jobs=processors)
results.append(cv_results)
names.append(name)
print("{0}: ({1:.3f}) +/- ({2:.3f})".format(name, cv_results.mean(), cv_results.std()))
#Ensembles
#Bagging (Bootstrap Aggregation)
#Bagged Decision Trees
cart = DecisionTreeClassifier()
num_trees = 100
model = BaggingClassifier(base_estimator=cart, n_estimators=num_trees, random_state=seed)
results = cross_val_score(model, X, Y, cv=kfold, scoring=scoring, n_jobs=processors)
print("({0:.3f}) +/- ({1:.3f})".format(results.mean(), results.std()))
#Random Forest
num_trees = 100
num_features = 10
model = RandomForestClassifier(n_estimators=num_trees, max_features=num_features)
results = cross_val_score(model, X, Y, cv=kfold, scoring=scoring, n_jobs=processors)
print("({0:.3f}) +/- ({1:.3f})".format(results.mean(), results.std()))
#Extra Trees
num_trees = 100
num_features = 10
model = ExtraTreesClassifier(n_estimators=num_trees, max_features=num_features)
results = cross_val_score(model, X, Y, cv=kfold, scoring=scoring, n_jobs=processors)
print("({0:.3f}) +/- ({1:.3f})".format(results.mean(), results.std()))
#Boosting
#AdaBoost
model = AdaBoostClassifier(n_estimators=100, random_state=seed)
results = cross_val_score(model, X, Y, cv=kfold, scoring=scoring, n_jobs=processors)
print("({0:.3f}) +/- ({1:.3f})".format(results.mean(), results.std()))
#Stochastic Gradient Boosting
model = GradientBoostingClassifier(n_estimators=100, random_state=seed)
results = cross_val_score(model, X, Y, cv=kfold, scoring=scoring, n_jobs=processors)
print("({0:.3f}) +/- ({1:.3f})".format(results.mean(), results.std()))
#Hyperparameter tuning
#Logistic Regression
lr_grid = GridSearchCV(
estimator = LogisticRegression(random_state=seed),
param_grid = {
'penalty': ['l1', 'l2'],
'C': [0.001, 0.01, 1, 10, 100, 1000]
},
cv = kfold,
scoring = scoring,
n_jobs = processors)
lr_grid.fit(X, Y)
print(lr_grid.best_score_)
print(lr_grid.best_params_)
#Linear Discriminant Analysis
lda_grid = GridSearchCV(
estimator = LinearDiscriminantAnalysis(),
param_grid = {
'solver': ['lsqr'],
'shrinkage': [0, 0.25, 0.5, 0.75, 1],
'n_components': [None, 2, 5, 10]
},
cv = kfold,
scoring = scoring,
n_jobs = processors)
lda_grid.fit(X, Y)
print(lda_grid.best_score_)
print(lda_grid.best_params_)
#K-NN
knn_grid = GridSearchCV(
estimator = Pipeline([
('min_max_scaler', MinMaxScaler()),
('knn', KNeighborsClassifier())
]),
param_grid = {
'knn__n_neighbors': [25],
'knn__algorithm': ['ball_tree'],
'knn__leaf_size': [2, 3, 4],
'knn__p': [1]
},
cv = kfold,
scoring = scoring,
n_jobs = processors)
knn_grid.fit(X, Y)
print(knn_grid.best_score_)
print(knn_grid.best_params_)
#Random Forest
rf_grid = GridSearchCV(
estimator = RandomForestClassifier(warm_start=True, random_state=seed),
param_grid = {
'n_estimators': [100, 200],
'criterion': ['gini', 'entropy'],
'max_features': [18, 20],
'max_depth': [8, 10],
'bootstrap': [True]
},
cv = kfold,
scoring = scoring,
n_jobs = processors)
rf_grid.fit(X, Y)
print(rf_grid.best_score_)
print(rf_grid.best_params_)
#AdaBoost
ada_grid = GridSearchCV(
estimator = AdaBoostClassifier(random_state=seed),
param_grid = {
'algorithm': ['SAMME', 'SAMME.R'],
'n_estimators': [10, 25, 50],
'learning_rate': [1e-3, 1e-2, 1e-1]
},
cv = kfold,
scoring = scoring,
n_jobs = processors)
ada_grid.fit(X, Y)
print(ada_grid.best_score_)
print(ada_grid.best_params_)
#Gradient Boosting
gbm_grid = GridSearchCV(
estimator = GradientBoostingClassifier(warm_start=True, random_state=seed),
param_grid = {
'n_estimators': [100, 200],
'max_depth': [2, 3, 4],
'max_features': [10, 15, 20],
'learning_rate': [1e-1, 1]
},
cv = kfold,
scoring = scoring,
n_jobs = processors)
gbm_grid.fit(X, Y)
print(gbm_grid.best_score_)
print(gbm_grid.best_params_)
#Voting ensemble
#Create sub models
estimators = []
estimators.append(('lr', LogisticRegression(penalty='l2', C=1)))
estimators.append(('gbm', GradientBoostingClassifier(n_estimators=200, max_depth=3, learning_rate=0.1, max_features=15, warm_start=True, random_state=seed)))
estimators.append(('rf', RandomForestClassifier(bootstrap=True, max_depth=8, n_estimators=200, max_features=20, criterion='entropy', random_state=seed)))
estimators.append(('ada', AdaBoostClassifier(algorithm='SAMME.R', learning_rate=1e-2, n_estimators=10, random_state=seed)))
# create the ensemble model
ensemble = VotingClassifier(estimators, voting='soft', weights=[2,3,3,1])
results = cross_val_score(ensemble, X, Y, cv=kfold, scoring=scoring,n_jobs=processors)
print("({0:.3f}) +/- ({1:.3f})".format(results.mean(), results.std()))
#Make final predictions
model = ensemble
model.fit(X, Y)
preds = model. predict_proba(data_submit)
submission = pd.DataFrame()
submission["shot_id"] = data_submit.index
submission["shot_made_flag"]= preds[:,0]
submission.to_csv("subtest.csv",index=False)