2015-03-13 65 views

回答

3

SciPy的有回归工具大量,虽然很多都没有在很好地包裹泛型方法将R风格。 Pandas是最接近复制R功能的python库,完成了预测方法。 R和python中的以下片段展示了相似之处。

[R线性回归:

data(trees) 
linmodel <- lm(Volume~., data = trees[1:20,]) 
linpred <- predict(linmodel, trees[21:31,]) 
plot(linpred, trees$Volume[21:31]) 

相同的数据使用熊猫ols在python设置:

import pandas as pd 
from pandas.stats.api import ols 
import matplotlib.pyplot as plt 

trees = pd.read_csv('trees.csv') 
linmodel = ols(y = trees['Volume'][0:20], x = trees[['Girth', 'Height']][0:20]) 
linpred = linmodel.predict(x = trees[['Girth', 'Height']][20:31]) 
plt.scatter(linpred,trees['Volume'][20:31])