This is the final episode of the data visualization story that continued until previous.
We will use the data from pydata-book as before.
pydata-book/ch08/macrodata.csv https://github.com/pydata/pydata-book/blob/master/ch08/macrodata.csv
import numpy as np
from pandas import *
import matplotlib.pyplot as plt
#Read CSV data
macro = read_csv('macrodata.csv')
#Pick up some columns
data = macro[['cpi', 'm1', 'tbilrate', 'unemp']]
# .diff()The method changes the value to the difference from the previous row
#Because it starts with NaN.dropna()Remove with method
trans_data = np.log(data).diff().dropna()
# trans_data will be a dataset showing the changes from the previous row
#Show last 5 lines
print( trans_data[-5:] )
# =>
# cpi m1 tbilrate unemp
# 198 -0.007904 0.045361 -0.396881 0.105361
# 199 -0.021979 0.066753 -2.277267 0.139762
# 200 0.002340 0.010286 0.606136 0.160343
# 201 0.008419 0.037461 -0.200671 0.127339
# 202 0.008894 0.012202 -0.405465 0.042560
#Plot a scatter plot from two columns
plt.scatter(trans_data['m1'], trans_data['unemp'])
plt.show()
plt.savefig("image.png ")
[Scatter Plot Matrix](http://www.okada.jp.org/RWiki/?%A5%B0%A5%E9%A5%D5%A5%] is a scatter plot of all pairs of a series of variables. A3% A5% C3% A5% AF% A5% B9% BB% B2% B9% CD% BC% C2% CE% E3% BD% B8% A1% A7% BB% B6% C9% DB% BF% DE% It is B9% D4% CE% F3). You can create this with the scatter_matrix function.
#Generate a scatterplot matrix
from pandas.tools.plotting import scatter_matrix
scatter_matrix(trans_data, diagonal='kde', color='k', alpha=0.3)
plt.show()
plt.savefig("image2.png ")
It serves as a simple and powerful way to look at the correlation of any two 1D data.
Introduction to data analysis with Python-Data processing using NumPy and pandas http://www.oreilly.co.jp/books/9784873116556/
Recommended Posts