FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
MachineLearning/Basic Python/load_csv.py at master · falling-star/MachineLearning · GitHub
falling-star
/
MachineLearning
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
MachineLearning
/
Basic Python
/
load_csv.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (45 loc) · 1.38 KB
Breadcrumbs
MachineLearning
/
Basic Python
/
load_csv.py
Copy path
File metadata and controls
58 lines (45 loc) · 1.38 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
from
csv
import
reader
import
os
# Load CSV data
def
load_csv
(
filename
):
dataset
=
list
()
with
open
(
os
.
getcwd
()
+
'/'
+
filename
,
'r'
)
as
file
:
csv_reader
=
reader
(
file
)
for
row
in
csv_reader
:
if
not
row
:
continue
dataset
.
append
(
row
)
return
dataset
# verify CSV data
filename
=
'data.csv'
dataset
=
load_csv
(
filename
)
print
(
'Loaded data file {0} with {1} rows and {2} columns'
).
format
(
filename
,
len
(
dataset
),
len
(
dataset
[
0
]))
print
(
dataset
[
0
])
# str column to float column
def
str_column_to_float
(
dataset
,
column
):
for
row
in
dataset
:
row
[
column
]
=
float
(
row
[
column
].
strip
())
# verify
for
i
in
range
(
len
(
dataset
[
0
])):
str_column_to_float
(
dataset
,
i
)
print
(
dataset
[
0
])
# str column to float , int respectively
def
str_column_to_int
(
dataset
,
column
):
class_values
=
[
row
[
column
]
for
row
in
dataset
]
unique
=
set
(
class_values
)
lookup
=
dict
()
for
i
,
value
in
enumerate
(
unique
):
lookup
[
value
]
=
i
for
row
in
dataset
:
row
[
column
]
=
lookup
[
row
[
column
]]
return
lookup
filename
=
'iris.csv'
dataset
=
load_csv
(
filename
)
print
(
'Loaded data file {0} with {1} rows and {2} columns'
).
format
(
filename
,
len
(
dataset
),
len
(
dataset
[
0
]))
print
(
dataset
[
0
])
# verify
for
i
in
range
(
4
):
str_column_to_float
(
dataset
,
i
)
lookup
=
str_column_to_int
(
dataset
,
4
)
print
(
dataset
[
0
])
print
(
lookup
)
Back
|
FazBrowse Home
|
New Git URL