FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PythonForDataScience/crossValidate.py at master · datanalyst/PythonForDataScience · GitHub
datanalyst
PythonForDataScience
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
PythonForDataScience
/
crossValidate.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
29 lines (23 loc) · 1.1 KB
Breadcrumbs
PythonForDataScience
/
crossValidate.py
Copy path
File metadata and controls
29 lines (23 loc) · 1.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
from
sklearn
.
ensemble
import
RandomForestClassifier
from
sklearn
import
cross_validation
import
logloss
import
numpy
as
np
def
main
():
#read in data, parse into training and target sets
dataset
=
np
.
genfromtxt
(
open
(
'Data/train.csv'
,
'r'
),
delimiter
=
','
,
dtype
=
'f8'
)[
1
:]
target
=
np
.
array
([
x
[
0
]
for
x
in
dataset
])
train
=
np
.
array
([
x
[
1
:]
for
x
in
dataset
])
#In this case we'll use a random forest, but this could be any classifier
cfr
=
RandomForestClassifier
(
n_estimators
=
100
)
#Simple K-Fold cross validation. 5 folds.
cv
=
cross_validation
.
KFold
(
len
(
train
),
k
=
5
,
indices
=
False
)
#iterate through the training and test cross validation segments and
#run the classifier on each one, aggregating the results into a list
results
=
[]
for
traincv
,
testcv
in
cv
:
probas
=
cfr
.
fit
(
train
[
traincv
],
target
[
traincv
]).
predict_proba
(
train
[
testcv
])
results
.
append
(
logloss
.
llfun
(
target
[
testcv
], [
x
[
1
]
for
x
in
probas
]) )
#print out the mean of the cross-validated results
print
"Results: "
+
str
(
np
.
array
(
results
).
mean
() )
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL