-
Notifications
You must be signed in to change notification settings - Fork 0
/
mLeaf2.py
64 lines (52 loc) · 2.02 KB
/
mLeaf2.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
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 11 20:24:54 2017
@author: a
Kaggle - Leaf competition
concise extract from mLeaf1 + using a pipeline
knn + GridSearchCV + inner/outer CV
"""
print(__doc__)
import pandas as pd
import os
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
# data collection
os.getcwd()
os.chdir('/home/michal/Dropbox/cooperation/_python/Leaf-competition/models')
os.chdir('D:\\data\\Dropbox\\cooperation\\_python\\Leaf-competition\\Models')
os.chdir('G:\\Dropbox\\cooperation\\_python\\Leaf-competition\\Models')
train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')
# initial preprocessing
train_id = train.pop('id')
train_y = train.pop('species')
test_id = test.pop('id')
le = LabelEncoder()
y = le.fit_transform(train_y)
# CV
skf = StratifiedKFold(5, shuffle=True) # 10 obs. per class, select 2 for testing
''' running and tuning knn model '''
# KNN
knn = KNeighborsClassifier()
params = {'n_neighbors':[2,3,5,7], 'n_jobs':[-1]}
gs = GridSearchCV(knn, params, cv=skf, n_jobs=-1)
gs.fit(train, y) # best k=3, (0.874)
print("The best parameters are %s with a score of %0.3f" % (gs.best_params_, gs.best_score_))
# validation
scores = cross_val_score(gs, train, y, cv=skf, n_jobs=-1)
print("Accuracy: %0.3f (+/- %0.3f)" % (scores.mean(), 2*scores.std(ddof=1))) # (0.880)
''' the same as a pipeline '''
from sklearn.pipeline import Pipeline
pipeline = Pipeline([('knn', KNeighborsClassifier())])
parameters = {'knn__n_neighbors':[2,3,5,7],
'knn__n_jobs':[-1]}
gs = GridSearchCV(pipeline, parameters, cv=skf, n_jobs=-1)
gs.fit(train, y) # best k=3
print("The best parameters are %s with a score of %0.3f" % (gs.best_params_, gs.best_score_))
# validation
scores = cross_val_score(gs, train, y, cv=skf, n_jobs=-1)
print("Accuracy: %0.3f (+/- %0.3f)" % (scores.mean(), 2*scores.std(ddof=1)))