Data Science in the Cloud: Investment analysis using Ipython and Pandas

Source: Internet
Author: User
Tags data structures install pandas install matplotlib

Brief introduction

Let's do a common analysis and you may be able to do it yourself. Suppose you want to analyze stock performance, then you can:

Find a stock in the Yahoo financial zone.

Download historical data and save it as a CSV file format.

Import the CSV file into Excel.

Perform mathematical analysis: regression, descriptive statistics or linear optimization using the Excel Solver tool.

Good, but this article shows you a simpler, more intuitive, more powerful way to use IPython and pandas for the same kind of analysis.

The IPython Library is one of the most important tools for using Python data scientists. The biggest difference between this tool and Excel is that you can use it to interactively explore data and analysis from an interactive prompt. The examples in this article mainly use IPython as a mechanism to run them.

The Python Data Analysis Library (PANDAS) is a BSD-licensed open Source library that provides high-performance, easy-to-use data structures and data analytics tools for the Python programming language.

Entry

When you start using IPython and pandas, you need to set up your Linux or Unix operating system, such as Ubuntu or OS X.

Installation Pip,pip is a tool for installing and managing Python packets. You may have used Easy_install before, but the PIP has now replaced Easy_install. To install the PIP, go to the PIP index page of the Python Web site and follow the instructions.

After the PIP is installed, install IPython using the following command:

sudo pip install IPython

To install pandas using PIP:

sudo pip install pandas

There is also an installation tool-matplotlib, a drawing library for the Python programming language and its numpy numerical math extension. Please use the following command:

sudo pip install matplotlib

Now that you've done all the steps you need, let's start using IPython and Pandas!

Pandas introduction

To enter your portfolio data into pandas, use the following code:

Listing 1. Import of Pandas combined data

In [1]: Import pandas.io.data as Web into
    
[2]: From pandas import dataframe into
    
[3]: Data_feed = {} in
    
[4]: Symbo ls=[' AAPL ', ' FB ', ' GOOG ', ' splk ', ' YELP ', ' GG ', ' BP ', ' SCPJ ', ' jnj ', ' OMG '] in
    
[5]: for ticker in symbols:         Data_feed[ticker] = Web.get_data_yahoo (ticker, ' 05/21/2012 ', ' 11/1/2012 ')
...: in
    
[6]: Price = Dataframe ({tic: data[' Adj close ']
...:     for tic, data in Data_feed.iteritems ()}) in
    
[7]: volume = Dataframe ({tic:data[' Volume ']
...:     for tic, data in Data_feed.iteritems ()}) in
    
[8]: Returns = Price.pct_change ()

To determine the percentage of annual yield and analyze it, you can call the return Dataframe method and the plot method. This can be done by calling sum to sum the columns in Dataframe, which performs a lot of work to create the chart shown in Figure 1.

Listing 2. Annual rate of Return

In [9]: Import matplotlib.pyplot as Plt
[[]: Returns.sum (). Plot (kind= ' bar ', title= "% return to Year")
out[10 : <matplotlib.axes.axessubplot at 0x10c1b0350> in
[one]: Plt.show ()

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Servers/cloud-computing/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.