0

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

1
  • 1
    If you want to run that piece of code as a .py file remove %matplotlib inline from your code
    – Sreeram TP
    Commented Apr 15, 2019 at 11:08

4 Answers 4

1

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"

0
1

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

0

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

0

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" .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.