箱线图
"""
Created on Sun Mar 26 11:41:50 2017
@author: Administrator
"""
import pandas as pd
import matplotlib.pyplot as plt
catering_sale = 'F:\data\chapter3\demo\data\catering_sale.xls'
data = pd.read_excel(catering_sale, index_col = u'日期' )
plt.rcParams['font.sans-serif' ] = ['SimHei' ]
plt.rcParams['axes.unicode_minus' ] = False
plt.figure()
p = data.boxplot(return_type='dict' )
x = p['fliers' ][0 ].get_xdata()
y = p['fliers' ][0 ].get_ydata()
y.sort()
print(y)
for i in range(len(x)):
print(i)
if i>0 :
plt.annotate(y[i], xy = (x[i],y[i]), xytext=(x[i]+0.05 -0.8 /(y[i]-y[i-1 ]),y[i]))
else :
plt.annotate(y[i], xy = (x[i],y[i]), xytext=(x[i]+0.08 ,y[i]))
plt.show()
盈利占比分析
from __future__ import print_function
import pandas as pd
dish_profit = 'F:\data\chapter3\demo\data\catering_dish_profit.xls'
data = pd.read_excel(dish_profit, index_col = u'菜品名' )
data = data[u'盈利' ].copy()
data.sort(ascending = False )
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif' ] = ['SimHei' ]
plt.rcParams['axes.unicode_minus' ] = False
plt.figure()
data.plot(kind='bar' )
plt.ylabel(u'盈利(元)' )
p = 1.0 *data.cumsum()/data.sum()
p.plot(color = 'r' , secondary_y = True , style = '-o' ,linewidth = 2 )
plt.annotate(format(p[6 ], '.4%' ), xy = (6 , p[6 ]), xytext=(6 *0.9 , p[6 ]*0.9 ), arrowprops=dict(arrowstyle="->" , connectionstyle="arc3,rad=.2" ))
plt.ylabel(u'盈利(比例)' )
plt.show()
饼状图
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
labels='Frogs' ,'Hogs' ,'Dogs' ,'Logs'
sizes=[15 ,30 ,40 ,10 ]
color=['yellowgreen' ,'gold' ,'lightskyblue' ,'lightcoral' ]
explode=(0 ,0.1 ,0 ,0 )
plt.pie(sizes,explode=explode,labels=labels,colors=color,autopct='%1.1f%%' ,shadow=True ,startangle=90 )
plt.axis('equal' )
plt.show()
拉格朗日插值
import pandas as pd
from scipy.interpolate import lagrange
inputfile = 'F:\data\chapter4\demo\data\catering_sale.xls'
outputfile = 'F:\data\chapter4\demo\data\sales.xls'
data = pd.read_excel(inputfile)
data[u'销量' ][(data[u'销量' ] < 400 ) | (data[u'销量' ] > 5000 )] = None
def ployinterp_column (s, n, k=5 ) :
y = s[list(range(n-k, n)) + list(range(n+1 , n+1 +k))]
y = y[y.notnull()]
return lagrange(y.index, list(y))(n)
for i in data.columns:
for j in range(len(data)):
if (data[i].isnull())[j]:
data[i][j] = ployinterp_column(data[i], j)
data.to_excel(outputfile)
PCA
import pandas as pd
inputfile = '../data/principal_component.xls'
outputfile = '../tmp/dimention_reducted.xls'
data = pd.read_excel(inputfile, header = None )
from sklearn.decomposition import PCA
pca = PCA(3 )
pca.fit(data)
low_d=pca.transform(data)
pd.DataFrame(low_d).to_excel(outputfile)
pca.inverse_transform(low_d)