f



>>> %matplotlib inline results in SyntaxError: invalid syntax

I have installed Python 3.4.4 on XPSP3 and am trying to work my way 
through this tutorial.

A Complete Tutorial on Ridge and Lasso Regression in Python
http://www.analyticsvidhya.com/blog/2016/01/complete-tutorial-ridge-lasso-regression-python/

In Step 2 "Why Penalize the Magnitude of Coefficients?" we are shown 
this code:

#Importing libraries. The same will be used throughout the article.
import numpy as np
import pandas as pd
import random
import matplotlib.pyplot as plt
%matplotlib inline

I get an error on the last line. I am running this code in Idle Python 
3.4.4 Shell...

Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 
bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
 >>> import numpy as np
 >>> import pandas as pd
 >>> import random
 >>> import matplotlib.pyplot as plt
 >>> %matplotlib inline
SyntaxError: invalid syntax

What am I doing wrong? Suggested reading?
TIA,
Mike
0
Mike
1/29/2016 7:04:29 AM
comp.lang.python 77058 articles. 5 followers. Post Follow

3 Replies
7012 Views

Similar Articles

[PageSpeed] 59

On Fri, Jan 29, 2016 at 6:04 PM, Mike S via Python-list
<python-list@python.org> wrote:
>
> I get an error on the last line. I am running this code in Idle Python 3.4.4
> Shell...
>
> Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit
> (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
>>>> import numpy as np
>>>> import pandas as pd
>>>> import random
>>>> import matplotlib.pyplot as plt
>>>> %matplotlib inline
> SyntaxError: invalid syntax
>
> What am I doing wrong? Suggested reading?

That's a magic command for ipython - it's not actual Python syntax.
You can either install ipython and do the exercises there, or continue
with Idle and accept that things will be slightly different. Skim
through a few more of the examples; do you see more lines starting
with percent signs? If not, you can probably get away with using any
interactive Python interpreter (including Idle as you're using above).
Alternatively, take the certain route and install ipython - then
everything should work.

ChrisA
0
Chris
1/29/2016 7:32:58 AM
On Fri, 29 Jan 2016 06:04 pm, Mike S wrote:

> %matplotlib inline
> 
> I get an error on the last line. I am running this code in Idle Python
> 3.4.4 Shell...
> 
> Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32
> bit (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
>  >>> import numpy as np
>  >>> import pandas as pd
>  >>> import random
>  >>> import matplotlib.pyplot as plt
>  >>> %matplotlib inline
> SyntaxError: invalid syntax
> 
> What am I doing wrong? Suggested reading?

That %matplotlib looks to me like iPython "magic" -- yes, that's the
official name for it. It's not real Python code, and won't run in a regular
Python interpreter like IDLE.

IPython is a custom Python interactive interpreter. As well as running
regular Python code, it includes special syntax for magic such as:

%cd
%run
%bg

and many more. 

http://ipython.org/ipython-doc/rel-0.10.2/html/interactive/reference.html#magic

You could try installing iPython and running the tutorial examples in that,
or perhaps finding a better tutorial that actually bothers to mention what
it needs to run.

(You have read the tutorial from the beginning, yes?)




-- 
Steven

0
Steven
1/29/2016 7:57:31 AM
On 1/28/2016 11:57 PM, Steven D'Aprano wrote:
> On Fri, 29 Jan 2016 06:04 pm, Mike S wrote:
>
>> %matplotlib inline
>>
>> I get an error on the last line. I am running this code in Idle Python
>> 3.4.4 Shell...
>>
>> Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32
>> bit (Intel)] on win32
>> Type "copyright", "credits" or "license()" for more information.
>>   >>> import numpy as np
>>   >>> import pandas as pd
>>   >>> import random
>>   >>> import matplotlib.pyplot as plt
>>   >>> %matplotlib inline
>> SyntaxError: invalid syntax
>>
>> What am I doing wrong? Suggested reading?
>
> That %matplotlib looks to me like iPython "magic" -- yes, that's the
> official name for it. It's not real Python code, and won't run in a regular
> Python interpreter like IDLE.
>
> IPython is a custom Python interactive interpreter. As well as running
> regular Python code, it includes special syntax for magic such as:
>
> %cd
> %run
> %bg
>
> and many more.
>
> http://ipython.org/ipython-doc/rel-0.10.2/html/interactive/reference.html#magic
>
> You could try installing iPython and running the tutorial examples in that,
> or perhaps finding a better tutorial that actually bothers to mention what
> it needs to run.
>
> (You have read the tutorial from the beginning, yes?)

Thank you Chris and Steven, I'll see if I can get iPython running and do 
a tutorial.

0
Mike
1/29/2016 8:10:55 AM
Reply: