Convert all labels in MR_df["label"] to numerical labels, using the convert_label function. Be sure to specify the appropriate argument to the direction parameter. Save them as a new column named "Y" in MR_df.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Hello I need help with following python problem. I don't know why Im keep getting errors. Please refer 1b to solve the problem. Thank you!

 

1b) Create a function that converts string labels to numerical labels
Function name: convert_label
The function should do the following:
• take two parameters label and direction
• if direction is 'tonumber',
· and if the input label is "pos" return 1.0
· and if the input label is "neg" return 0.0
• otherwise, return the input label as is
• if direction is 'tolabel'
and if the input label is 1.0 return "pos"
· and if the input label is 0.0 return "neg"
otherwise, return the label as is
1 def convert_label(label, direction):
"tonumber":
if direction
if 'pos' in label:
return 1.0
elif 'neg' in label:
return 0.0
return label
if direction
"tolabel":
if '1.0' in label:
return 'pos'
elif '0,0' in label:
return 'neg'
else:
10
11
12
13
14
15
return label
1 assert convert_label
1 assert callable(convert_label)
H N 3 4567 ∞0 o d NM
Transcribed Image Text:1b) Create a function that converts string labels to numerical labels Function name: convert_label The function should do the following: • take two parameters label and direction • if direction is 'tonumber', · and if the input label is "pos" return 1.0 · and if the input label is "neg" return 0.0 • otherwise, return the input label as is • if direction is 'tolabel' and if the input label is 1.0 return "pos" · and if the input label is 0.0 return "neg" otherwise, return the label as is 1 def convert_label(label, direction): "tonumber": if direction if 'pos' in label: return 1.0 elif 'neg' in label: return 0.0 return label if direction "tolabel": if '1.0' in label: return 'pos' elif '0,0' in label: return 'neg' else: 10 11 12 13 14 15 return label 1 assert convert_label 1 assert callable(convert_label) H N 3 4567 ∞0 o d NM
Convert all labels in MR_df["label"] to numerical labels, using the convert_label function. Be sure to specify the
appropriate argument to the direction parameter.
Save them as a new column named "Y" in MR_df.
1 # YOUR CODE HERE
2 df['MR_df']=[convert_label(x,'tonumber') for Y in df[MR_df]]
NameError
Traceback (most recent call last)
/tmp/ipykernel_456/4234017543.py in <module>
1 # YOUR CODE HERE
-> 2 df['MR_df']=[convert_label(x, 'tonumber')for Y in df [MR_df]]
NameError: name 'df' is not defined
1
assert sorted (set (MR_df['Y']))
[0., 1.]
Traceback (most recent call last)
KeyError
/opt/conda/lib/python3.9/site-packages/pandas/core/indexes/base.py in get_loc(self, key, me
thod, tolerance)
3360
try:
return self._engine.get_loc(casted_key)
except KeyError as err:
-> 3361
3362
/opt/conda/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEn
gine.get_loc( )
/opt/conda/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEn
gine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Py0bjectHashTable.get_ite
m()
pandas/_libs/hashtable_class_helper.pxi in pandas. libs.hashtable.Py0bjectHashTable.get_ite
m( )
КeyError: 'Y"
The above exception was the direct cause of the following exception:
КeyError
/tmp/ipykernel_456/2394443215.py in <module>
----> 1 assert sorted (set (MR_df['Y'])).
Traceback (most recent call last)
[0., 1.]
Transcribed Image Text:Convert all labels in MR_df["label"] to numerical labels, using the convert_label function. Be sure to specify the appropriate argument to the direction parameter. Save them as a new column named "Y" in MR_df. 1 # YOUR CODE HERE 2 df['MR_df']=[convert_label(x,'tonumber') for Y in df[MR_df]] NameError Traceback (most recent call last) /tmp/ipykernel_456/4234017543.py in <module> 1 # YOUR CODE HERE -> 2 df['MR_df']=[convert_label(x, 'tonumber')for Y in df [MR_df]] NameError: name 'df' is not defined 1 assert sorted (set (MR_df['Y'])) [0., 1.] Traceback (most recent call last) KeyError /opt/conda/lib/python3.9/site-packages/pandas/core/indexes/base.py in get_loc(self, key, me thod, tolerance) 3360 try: return self._engine.get_loc(casted_key) except KeyError as err: -> 3361 3362 /opt/conda/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEn gine.get_loc( ) /opt/conda/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEn gine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Py0bjectHashTable.get_ite m() pandas/_libs/hashtable_class_helper.pxi in pandas. libs.hashtable.Py0bjectHashTable.get_ite m( ) КeyError: 'Y" The above exception was the direct cause of the following exception: КeyError /tmp/ipykernel_456/2394443215.py in <module> ----> 1 assert sorted (set (MR_df['Y'])). Traceback (most recent call last) [0., 1.]
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Data Binding
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education