By alancc


2019-04-15 11:05:10 8 Comments

I just read an article at https://www.analyticsvidhya.com/blog/2018/10/predicting-stock-price-machine-learningnd-deep-learning-techniques-python/ so I want to follow it to make a test.

I download Python and then copy the first part of the code to a .py file as below:

    #import packages
    import pandas as pd
    import numpy as np

    #to plot within notebook
    import matplotlib.pyplot as plt
    %matplotlib inline

    #setting figure size
    from matplotlib.pylab import rcParams
    rcParams['figure.figsize'] = 20,10

    #for normalizing data
    from sklearn.preprocessing import MinMaxScaler
    scaler = MinMaxScaler(feature_range=(0, 1))

    #read the file
    df = pd.read_csv('NSE-TATAGLOBAL(1).csv')

    #print the head
    df.head()

But when running it, I get "Invalid syntax" error on the codeline:

    %matplotlib inline

After googling for the problem, I understand %xxx is a magic command and should be run with IPython. Therefore, I try to download Anaconda and install it on my computer. However, when I try to run the script in Spyder(I belive it is for IPython), I still get the same error.

How can I run the script in the article?

Thanks

4 comments

@Prayson W. Daniel 2019-04-15 12:38:38

Explanation:

%matplotlib inline 

A magic command used in iPython, and Jupyter Notebook/Lab to allow plots to be displayed without calling the .show() function.

How to:

If you want to execute that code without changing anything, you can do:

conda update -n base -c defaults conda
conda create -n mlenv python=3.6 pandas scikit-learn jupyter

This will update conda and create environment called mlenv which has Python 3.6 and pandas, scikit-learn and Jupyter tools. Installing Pandas with conda will automatically install numpy and matplotlib. jupyter installation will add iPython and Jupyter Lab & Notebook.

You can now activate and start coding:

conda activate mlenv
ipython -i name_of_file.py

This will excute and enter your file.

In the location of file or parent-folder, you can also run:

jupyter lab

This will open a web server where you can interactively execute your code cell by cell.

Hope this helps

@Ali Ghofrani 2019-04-15 11:14:47

A particularly interesting backend, provided by IPython, is the inline backend. This is available only for the Jupyter Notebook and the Jupyter QtConsole. It can be invoked as follows:

%matplotlib inline

With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.

if you don't use frontends such as jupyter note book just remove the "%*****inline"

@alancc 2019-04-15 13:40:22

Thank you very much.

@BoarGules 2019-04-15 11:11:37

The directive %matplotlib inline isn't Python, it's a Jupyter Notebook directive. If you are using another interpreter then it doesn't get executed, it just causes a syntax error.

This is explained in Purpose of "%matplotlib inline" .

@Amit Gupta 2019-04-15 11:10:02

install jupyter directly without installing anaconda or spyder

just open cmd or powershell and python -m pip install --upgrade pip

and then to open jupyter notebook again open cmd or powershell and type jupyter notebook

then you should be able to run the article code. More help

or remove%matplotlib inline from your code

Related Questions

Sponsored Content

13 Answered Questions

[SOLVED] How can I make a time delay in Python?

38 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

33 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 2774445 View
  • 2410 Score
  • 33 Answer
  • Tags:   python datetime time

26 Answered Questions

[SOLVED] How can I safely create a nested directory?

21 Answered Questions

15 Answered Questions

[SOLVED] What are metaclasses in Python?

28 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3287429 View
  • 2704 Score
  • 28 Answer
  • Tags:   python list indexing

56 Answered Questions

[SOLVED] Calling an external command in Python

25 Answered Questions

[SOLVED] Is there a way to run Python on Android?

Sponsored Content