2016-05-09 8 views
1

私はStatsmodelを2日以内に「使用」しており、インポートコマンドなどに慣れていません。variance_inflation_factorhereから実行したいのですが、問題があります。私のコードは次の通りです:パンダ> Statsmodel:variance_inflation_factorを実装する構文エラー

from numpy import * 
import numpy as np 
import pandas as pd 
from pandas import DataFrame, Series 
import statsmodels.formula.api as sm 
from sklearn.linear_model import LinearRegression 
import scipy, scipy.stats 
import matplotlib.pyplot as plt 
import matplotlib 
matplotlib.style.use('ggplot') 
from statsmodels.api import add_constant 
from numpy import linalg as LA 
import statsmodels as sm 

## I have been adding libraries and modules/packages with the intention of erring on the side of caution 

a = df1.years_exp 
b = df1.leg_totalbills 
c = df1.log_diff_rgdp 
d = df1.unemployment 
e = df1.expendituresfor 
f = df1.direct_expenditures 
g = df1.indirect_expenditures 

sm.variance_inflation_factor((['a', 'b', 'c', 'd', 'e', 'f']), g) 

then I get the following error: 

AttributeError       Traceback (most recent call last) 
<ipython-input-61-bb126535eadd> in <module>() 
----> 1 sm.variance_inflation_factor((['a', 'b', 'c', 'd', 'e', 'f']), g) 

AttributeError: module 'statsmodels' has no attribute 'variance_inflation_factor' 

このモジュールの読み込みと実行の正しい構文を教えてもらえますか?いくつかのソースコードへのリンクを投稿する方が便利な場合は、お尋ねください。しかし、私はこれが単なる構文上の問題であると感じています。 in the docs見られるように

答えて

1

機能variance_inflation_factorが正しくインポートする必要がありますので、それを使用するために、statsmodels.stats.outlier_influenceで発見され、オプションは、この質問をため

from statsmodels.stats import outliers_influence 
# code here 
outliers_influence.variance_inflation_factor((['a', 'b', 'c', 'd', 'e', 'f']), g) 
+0

コメントありがとうございました場合にのみ動作します。ここではこれを行うためのプログラム的な方法です!私はあなたのインポートコードを適用しましたが、 'outliers_influence.variance_inflation_factor((['a'、 'b'、 'c'、 'd'、 'e'、f '])、g)'を実装するいくつかの問題があります。 'AttributeError: 'list'オブジェクトには属性 'shape'がありません。私は質問を更新し、コードを操作して動作させることができるかどうかを確認します。 –

+1

質問を更新しないでください - それは別の問題です。あなたは別の質問がある場合は、あなたの力の中でそれを試して解決するためにすべてを行い、それでもあなたがそれを理解できない場合は、スタックオーバーフローを求めて新しい質問をすることができます。 – miradulo

0
a = df1.years_exp 
b = df1.leg_totalbills 
c = df1.log_diff_rgdp 
d = df1.unemployment 
e = df1.expendituresfor 
f = df1.direct_expenditures 
g = df1.indirect_expenditures 

ck=np.array([a,b,c,d,e,f,g]) 
outliers_influence.variance_inflation_factor(ck, 6) 
1

おかげだろう!私は今日、同じ問題を抱えていましたが、フィーチャごとの変動インフレ率を計算したいとは別でした。

from patsy import dmatrices 
from statsmodels.stats.outliers_influence import variance_inflation_factor 

# 'feature_1 + feature_2 ... feature_p' 
features_formula = "+".join(df1.columns - ["indirect_expenditures"]) 

# get y and X dataframes based on this formula: 
# indirect_expenditures ~ feature_1 + feature_2 ... feature_p 
y, X = dmatrices('indirect_expenditures ~' + features_formula, df1, return_type='dataframe') 

# For each Xi, calculate VIF and save in dataframe 
vif = pd.DataFrame() 
vif["vif"] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])] 
vif["features"] = X.columns 
vif 

注意してください上記のコードは、あなたがpandasを輸入しているとDF1はpandas DataFrame

関連する問題