Skip to content

Commit 302686a

Browse files
committed
add 001
1 parent afeb427 commit 302686a

File tree

12 files changed

+197616
-0
lines changed

12 files changed

+197616
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"#Digit Recognizer\n",
12+
"#sklean PCA + SVM "
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 2,
18+
"metadata": {
19+
"collapsed": true
20+
},
21+
"outputs": [],
22+
"source": [
23+
"import numpy as np\n",
24+
"from sklearn.decomposition import PCA \n",
25+
"from sklearn.svm import SVC "
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 3,
31+
"metadata": {
32+
"collapsed": true
33+
},
34+
"outputs": [],
35+
"source": [
36+
"#为什么设置为35 \n",
37+
"COMPONENT_NUM = 35 "
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 6,
43+
"metadata": {
44+
"collapsed": false
45+
},
46+
"outputs": [],
47+
"source": [
48+
"#导入数据\n",
49+
"\n",
50+
"with open('train.csv','r') as fd:\n",
51+
" fd.readline()\n",
52+
" train_label=[]\n",
53+
" train_data = []\n",
54+
"\n",
55+
" \n",
56+
" for line in fd.readlines():\n",
57+
" data = list(map(int, line.rstrip().split(',')))\n",
58+
" train_label.append(data[0])\n",
59+
" train_data.append(data[1:])\n",
60+
" "
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 7,
66+
"metadata": {
67+
"collapsed": true
68+
},
69+
"outputs": [],
70+
"source": [
71+
"#赋值\n",
72+
"train_label = np.array(train_label)\n",
73+
"train_data = np.array(train_data)\n",
74+
"\n",
75+
"#PCA \n",
76+
"pca = PCA(n_components=COMPONENT_NUM,whiten=True)\n",
77+
"pca.fit(train_data)\n",
78+
"train_data = pca.transform(train_data)\n"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 14,
84+
"metadata": {
85+
"collapsed": false
86+
},
87+
"outputs": [
88+
{
89+
"data": {
90+
"text/plain": [
91+
"SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
92+
" decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',\n",
93+
" max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
94+
" tol=0.001, verbose=False)"
95+
]
96+
},
97+
"execution_count": 14,
98+
"metadata": {},
99+
"output_type": "execute_result"
100+
}
101+
],
102+
"source": [
103+
"#训练\n",
104+
"svc = SVC()\n",
105+
"svc.fit(train_data, train_label)\n"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 15,
111+
"metadata": {
112+
"collapsed": true
113+
},
114+
"outputs": [],
115+
"source": [
116+
"#导入测试数据\n",
117+
"with open('test.csv','r') as fd:\n",
118+
" fd.readline()\n",
119+
" test_data=[]\n",
120+
" for line in fd.readlines():\n",
121+
" pixels = list(map(int, line.rstrip().split(',')))\n",
122+
" test_data.append(pixels)\n",
123+
" "
124+
]
125+
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": 16,
129+
"metadata": {
130+
"collapsed": true
131+
},
132+
"outputs": [],
133+
"source": [
134+
"#赋值\n",
135+
"test_data = np.array(test_data)\n",
136+
"test_data = pca.transform(test_data)\n",
137+
"\n"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 17,
143+
"metadata": {
144+
"collapsed": true
145+
},
146+
"outputs": [],
147+
"source": [
148+
"#预测\n",
149+
"predict = svc.predict(test_data)"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 20,
155+
"metadata": {
156+
"collapsed": false
157+
},
158+
"outputs": [],
159+
"source": [
160+
"#保存到本地\n",
161+
"with open('predict.csv','w') as fd:\n",
162+
" fd.write('\"ImageID\",\"Label\"\\n')\n",
163+
" count = 0\n",
164+
" for p in predict:\n",
165+
" count+=1\n",
166+
" fd.write(str(count)+',\"'+str(p)+'\"\\n')"
167+
]
168+
}
169+
],
170+
"metadata": {
171+
"kernelspec": {
172+
"display_name": "Python [Root]",
173+
"language": "python",
174+
"name": "Python [Root]"
175+
},
176+
"language_info": {
177+
"codemirror_mode": {
178+
"name": "ipython",
179+
"version": 3
180+
},
181+
"file_extension": ".py",
182+
"mimetype": "text/x-python",
183+
"name": "python",
184+
"nbconvert_exporter": "python",
185+
"pygments_lexer": "ipython3",
186+
"version": "3.5.2"
187+
}
188+
},
189+
"nbformat": 4,
190+
"nbformat_minor": 0
191+
}

0 commit comments

Comments
 (0)