FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ClassicComputerScienceProblemsInPython/Chapter7/wine_test.py at master · sta2k/ClassicComputerScienceProblemsInPython · GitHub
sta2k
/
ClassicComputerScienceProblemsInPython
Public
forked from
davecom/ClassicComputerScienceProblemsInPython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ClassicComputerScienceProblemsInPython
/
Chapter7
/
wine_test.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (57 loc) · 2.52 KB
Breadcrumbs
ClassicComputerScienceProblemsInPython
/
Chapter7
/
wine_test.py
Copy path
File metadata and controls
64 lines (57 loc) · 2.52 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
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
# wine_test.py
# From Classic Computer Science Problems in Python Chapter 7
# Copyright 2018 David Kopec
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
csv
from
typing
import
List
from
util
import
normalize_by_feature_scaling
from
network
import
Network
from
random
import
shuffle
if
__name__
==
"__main__"
:
wine_parameters
:
List
[
List
[
float
]]
=
[]
wine_classifications
:
List
[
List
[
float
]]
=
[]
wine_species
:
List
[
int
]
=
[]
with
open
(
'wine.csv'
,
mode
=
'r'
)
as
wine_file
:
wines
:
List
=
list
(
csv
.
reader
(
wine_file
,
quoting
=
csv
.
QUOTE_NONNUMERIC
))
shuffle
(
wines
)
# get our lines of data in random order
for
wine
in
wines
:
parameters
:
List
[
float
]
=
[
float
(
n
)
for
n
in
wine
[
1
:
14
]]
wine_parameters
.
append
(
parameters
)
species
:
int
=
int
(
wine
[
0
])
if
species
==
1
:
wine_classifications
.
append
([
1.0
,
0.0
,
0.0
])
elif
species
==
2
:
wine_classifications
.
append
([
0.0
,
1.0
,
0.0
])
else
:
wine_classifications
.
append
([
0.0
,
0.0
,
1.0
])
wine_species
.
append
(
species
)
normalize_by_feature_scaling
(
wine_parameters
)
wine_network
:
Network
=
Network
([
13
,
7
,
3
],
0.9
)
def
wine_interpret_output
(
output
:
List
[
float
])
->
int
:
if
max
(
output
)
==
output
[
0
]:
return
1
elif
max
(
output
)
==
output
[
1
]:
return
2
else
:
return
3
# train over the first 150 wines 10 times
wine_trainers
:
List
[
List
[
float
]]
=
wine_parameters
[
0
:
150
]
wine_trainers_corrects
:
List
[
List
[
float
]]
=
wine_classifications
[
0
:
150
]
for
_
in
range
(
10
):
wine_network
.
train
(
wine_trainers
,
wine_trainers_corrects
)
# test over the last 28 of the wines in the data set
wine_testers
:
List
[
List
[
float
]]
=
wine_parameters
[
150
:
178
]
wine_testers_corrects
:
List
[
int
]
=
wine_species
[
150
:
178
]
wine_results
=
wine_network
.
validate
(
wine_testers
,
wine_testers_corrects
,
wine_interpret_output
)
print
(
f"
{
wine_results
[
0
]
}
correct of
{
wine_results
[
1
]
}
=
{
wine_results
[
2
]
*
100
}
%"
)
Back
|
FazBrowse Home
|
New Git URL