kharukaのブログ~お金と技術とキャリア~

Edu Fin~金融×教育~若いうちからお金について学ぶってだいじ!学んだテクノロジーはみんなの財産。過去、現在、将来の人生についてのブログ

pandas-データと統計量-

入門 Python 3

入門 Python 3

目次

前提条件

OS:Windows 10 64-bit, version 1607

Anaconda 4.4.0(Python 3.6 version 64-bit)インストール

NumPyのインストール

手順概要

1.コマンドプロンプトでIPython Notebookを起動します。

2.データと統計量

手順

1.コマンドプロンプトでIPython Notebookを起動します。

ipython notebook

2.データと統計量

2.1.numpy、panadasとpandasからSeriesとDataFrameをインポートします。

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

2.2.arrayを作成します。

arr=np.array([[1,2,np.nan],[np.nan,3,4,]])
arr

Out:

array([[  1.,   2.,  nan],
       [ nan,   3.,   4.]])

2.3.DataFrameを作成します。

dframe1=DataFrame(arr,index=['A','B'],columns=['One','Two','Three'])
dframe1

Out:

   One Two Three
A   1  2  NaN
B   NaN 3  4

f:id:kharuka2016:20170813200508p:plain

2.4.列の和を求めます。

dframe1.sum()

Out:

One      1.0
Two      5.0
Three    4.0
dtype: float64

2.5.行の和を求めます。

dframe1.sum(axis=1)

Out:

A    3.0
B    7.0
dtype: float64

f:id:kharuka2016:20170813200513p:plain

2.6.各列の最小値を求めます。

dframe1.min()

Out:

One      1.0
Two      2.0
Three    4.0
dtype: float64

2.7.各列の最小値のindexを返します。

dframe1.idxmin()

Out:

One      A
Two      A
Three    B
dtype: object

f:id:kharuka2016:20170813200517p:plain

2.8.DataFrameの列の累積和を求めます。

dframe1.cumsum()

Out:

   One Two Three
A   1  2  NaN
B   NaN 5  4

2.9.DataFrameの詳細を表示します。

dframe1.describe()

Out:

   One Two Three
count   1  2  1
mean    1  2.5    4
std NaN 0.707107   NaN
min  1  2  4
25%    1  2.25   4
50%    1  2.5    4
75%    1  2.75   4
max  1  3  4

f:id:kharuka2016:20170813200521p:plain

2.10.エラーがでました。pandas.io.dataは別のパッケージに移動されてしまったので、pandas-datareader packageをインストールしなさいと。

import pandas.io.data as pdweb

f:id:kharuka2016:20170813200535p:plain

2.11.pandas_datareaderパッケージをインストールします。

pip install pandas_datareader

f:id:kharuka2016:20170813200526p:plain

2.12.pandas_datareader.dataをwebとしてインポートします。

import pandas_datareader.data as web
import datetime

2.13.アメリカの証券取引所の株価データを取得します。株価データを取得する期間を指定しています。

prices=web.get_data_yahoo(['CVX','XOM','BP'],
                         start=datetime.datetime(2010,1,1),
                         end=datetime.datetime(2013,1,1))['Adj Close']

2.14.pricesの先頭5行を取得します。

prices.head()

Out:

   BP  CVX XOM
Date            
12/31/2012   31.217405  90.781021  74.38414
12/28/2012   30.902534  89.362282  73.137985
12/27/2012   31.202412  91.100006  74.650581
12/26/2012   31.247395  91.049652  74.831062
12/24/2012   31.277386  91.192337  74.702133

2.15.株価の変化の割合を表示します。

rets=prices.pct_change()
rets.head()

Out:

   BP  CVX XOM
Date            
12/31/2012   NaN NaN NaN
12/28/2012   -0.010086  -0.015628  -0.016753
12/27/2012   0.009704   0.019446   0.020681
12/26/2012   0.001442   -0.000553  0.002418
12/24/2012   0.00096    0.001567   -0.001723

f:id:kharuka2016:20170813200446p:plain

2.16.株価を描画します。

%matplotlib inline
prices.plot()

2.17.相関係数を求めます。

rets.corr()

Out:

   BP  CVX XOM
BP  1  0.574921   0.608057
CVX 0.574921   1  0.856295
XOM 0.608057   0.856295   1

f:id:kharuka2016:20170813200451p:plain

2.18.seabornをインストールします。

conda install seaborn

f:id:kharuka2016:20170813200455p:plain

2.19.seabornとmatplotlib.pyplotをインポートします。heatmapで相関係数を描画します。

import seaborn as sns
import matplotlib.pyplot as plt
sns.heatmap(rets.corr())

f:id:kharuka2016:20170813200459p:plain

2.20.Seriesを作成します。

ser1=Series(['w','w','x','y','z','w','w','x','x','y','a','z'])
ser1

Out:

0     w
1     w
2     x
3     y
4     z
5     w
6     w
7     x
8     x
9     y
10    a
11    z
dtype: object

2.21.uniqueで重複した値を取り除くことができます。

ser1.unique()

Out:

array(['w', 'x', 'y', 'z', 'a'], dtype=object)

2.22.countsは各要素がいくつあるかカウントしてくれます。

ser1.value_counts()

Out:

w    4
x    3
y    2
z    2
a    1
dtype: int64

f:id:kharuka2016:20170813200503p:plain

Pythonではじめる機械学習 ―scikit-learnで学ぶ特徴量エンジニアリングと機械学習の基礎

Pythonではじめる機械学習 ―scikit-learnで学ぶ特徴量エンジニアリングと機械学習の基礎

参考:

udemy 実践Pythonデータサイエンス

www.udemy.com

Pythonスタートブック

Pythonスタートブック