Skip to content

Commit c20c472

Browse files
author
Mofan Zhou
committed
create sk8
1 parent 34cb67a commit c20c472

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# View more python learning tutorial on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
from sklearn.datasets import load_iris
7+
from sklearn.cross_validation import train_test_split
8+
from sklearn.neighbors import KNeighborsClassifier
9+
10+
iris = load_iris()
11+
X = iris.data
12+
y = iris.target
13+
14+
# test train split #
15+
16+
17+
# this is cross_val_score #
18+
19+
20+
# this is how to use cross_val_score to choose model and configs #
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# View more python learning tutorial on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
from sklearn.datasets import load_iris
7+
from sklearn.cross_validation import train_test_split
8+
from sklearn.neighbors import KNeighborsClassifier
9+
10+
iris = load_iris()
11+
X = iris.data
12+
y = iris.target
13+
14+
# test train split #
15+
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=4)
16+
knn = KNeighborsClassifier(n_neighbors=5)
17+
knn.fit(X_train, y_train)
18+
y_pred = knn.predict(X_test)
19+
print(knn.score(X_test, y_test))
20+
21+
# this is cross_val_score #
22+
from sklearn.cross_validation import cross_val_score
23+
knn = KNeighborsClassifier(n_neighbors=5)
24+
scores = cross_val_score(knn, X, y, cv=5, scoring='accuracy')
25+
print(scores)
26+
27+
# this is how to use cross_val_score to choose model and configs #
28+
from sklearn.cross_validation import cross_val_score
29+
import matplotlib.pyplot as plt
30+
k_range = range(1, 31)
31+
k_scores = []
32+
for k in k_range:
33+
knn = KNeighborsClassifier(n_neighbors=k)
34+
## loss = -cross_val_score(knn, X, y, cv=10, scoring='mean_squared_error') # for regression
35+
scores = cross_val_score(knn, X, y, cv=10, scoring='accuracy') # for classification
36+
k_scores.append(scores.mean())
37+
38+
plt.plot(k_range, k_scores)
39+
plt.xlabel('Value of K for KNN')
40+
plt.ylabel('Cross-Validated Accuracy')
41+
plt.show()
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+

0 commit comments

Comments
 (0)