forked from apachecn/Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn-python3.6.py
57 lines (48 loc) · 1.67 KB
/
nn-python3.6.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
#!/usr/bin/python
# coding: utf-8
'''
Created on 2018-05-14
Update on 2018-05-14
Author: 平淡的天
Github: https://github.com/apachecn/kaggle
'''
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
import pandas as pd
train_data = pd.read_csv(r"C:\Users\312\Desktop\digit-recognizer\train.csv")
test_data = pd.read_csv(r"C:\Users\312\Desktop\digit-recognizer\test.csv")
data = pd.concat([train_data, test_data], axis=0).reset_index(drop=True)
data.drop(['label'], axis=1, inplace=True)
label = train_data.label
pca = PCA(n_components=100, random_state=34)
data_pca = pca.fit_transform(data)
Xtrain, Ytrain, xtest, ytest = train_test_split(
data_pca[0:len(train_data)], label, test_size=0.1, random_state=34)
clf = MLPClassifier(
hidden_layer_sizes=(100, ),
activation='relu',
alpha=0.0001,
learning_rate='constant',
learning_rate_init=0.001,
max_iter=200,
shuffle=True,
random_state=34)
clf.fit(Xtrain, xtest)
y_predict = clf.predict(Ytrain)
zeroLable = ytest - y_predict
rightCount = 0
for i in range(len(zeroLable)):
if list(zeroLable)[i] == 0:
rightCount += 1
print('the right rate is:', float(rightCount) / len(zeroLable))
result = clf.predict(data_pca[len(train_data):])
i = 0
fw = open("C:\\Users\\312\\Desktop\\digit-recognizer\\result.csv", 'w')
with open('C:\\Users\\312\\Desktop\\digit-recognizer\\sample_submission.csv'
) as pred_file:
fw.write('{},{}\n'.format('ImageId', 'Label'))
for line in pred_file.readlines()[1:]:
splits = line.strip().split(',')
fw.write('{},{}\n'.format(splits[0], result[i]))
i += 1