LinAlgError:奇异矩阵

时间:2017-12-08 08:05:53

标签: python numpy matrix regression linear-algebra

我正在使用pandas和numpy做一些回归实验。该软件包仅支持numpy数组。

我在数据集中有两种数据,连续和分类。 作为自变量

pandas dataframe:重新

我对连续做了什么:

built_year=re.built_year.values.reshape((-1,1))

输入:>>> built_year.shape

输出:(4508,1)

我对分类的做法:

condition=pd.factorize(re.condition.values)[0].reshape((-1,1))

输入:>>> condition.shape

输出:(4508,1)

对于从属变量

Y = re.price.values.reshape((-1,1))

然后生成'等式'

X = np.hstack([condition,built_year)]

然后将Y和X作为参数放入package方法。

每当我只考虑连续变量时,一切都很好。 只要我考虑分类变量,它就会出错:

~/miniconda3/lib/python3.6/site-packages/PySAL-1.14.3-py3.6.egg/pysal/contrib/gwr/sel_bw.py in search(self, search, criterion, bw_min, bw_max, interval, tol, max_iter)
    202         self.int_score = int_score
    203 
--> 204         self._bw()
    205 
    206         return self.bw[0]

~/miniconda3/lib/python3.6/site-packages/PySAL-1.14.3-py3.6.egg/pysal/contrib/gwr/sel_bw.py in _bw(self)
    215             delta = 0.38197 #1 - (np.sqrt(5.0)-1.0)/2.0
    216             self.bw = golden_section(a, c, delta, gwr_func, self.tol,
--> 217                     self.max_iter, self.int_score)
    218         elif self.search == 'interval':
    219             self.bw = equal_interval(self.bw_min, self.bw_max, self.interval,

~/miniconda3/lib/python3.6/site-packages/PySAL-1.14.3-py3.6.egg/pysal/contrib/gwr/search.py in golden_section(a, c, delta, function, tol, max_iter, int_score)
     51             d = np.round(d)
     52 
---> 53         score_a = function(a)
     54         score_b = function(b)
     55         score_c = function(c)

~/miniconda3/lib/python3.6/site-packages/PySAL-1.14.3-py3.6.egg/pysal/contrib/gwr/sel_bw.py in <lambda>(bw)
    209         gwr_func = lambda bw: getDiag[self.criterion](
    210                 GWR(self.coords, self.y, self.X_loc, bw, family=self.family,
--> 211                     kernel=self.kernel, fixed=self.fixed, constant=self.constant).fit())
    212         if self.search == 'golden_section':
    213             a,c = self._init_section(self.X_glob, self.X_loc, self.coords,

~/miniconda3/lib/python3.6/site-packages/PySAL-1.14.3-py3.6.egg/pysal/contrib/gwr/gwr.py in fit(self, ini_params, tol, max_iter, solve)
    259                 wi = self.W[i].reshape((-1,1))
    260                 rslt = iwls(self.y, self.X, self.family, self.offset, None,
--> 261                 ini_params, tol, max_iter, wi=wi)
    262                 params[i,:] = rslt[0].T
    263                 predy[i] = rslt[1][i]

~/miniconda3/lib/python3.6/site-packages/PySAL-1.14.3-py3.6.egg/pysal/contrib/glm/iwls.py in iwls(y, x, family, offset, y_fix, ini_betas, tol, max_iter, wi)
     74             n_betas = _compute_betas(wz, wx)
     75         else:
---> 76             n_betas, xtx_inv_xt = _compute_betas_gwr(wz, wx, wi)
     77         v = spdot(x, n_betas)
     78         mu  = family.fitted(v)

~/miniconda3/lib/python3.6/site-packages/PySAL-1.14.3-py3.6.egg/pysal/contrib/glm/iwls.py in _compute_betas_gwr(y, x, wi)
     32     xT = (x * wi).T
     33     xtx = np.dot(xT, x)
---> 34     xtx_inv = la.inv(xtx)
     35     xtx_inv_xt = np.dot(xtx_inv, xT)
     36     betas = np.dot(xtx_inv_xt, y)

~/miniconda3/lib/python3.6/site-packages/numpy/linalg/linalg.py in inv(a)
    511     signature = 'D->D' if isComplexType(t) else 'd->d'
    512     extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 513     ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
    514     return wrap(ainv.astype(result_t, copy=False))
    515 

~/miniconda3/lib/python3.6/site-packages/numpy/linalg/linalg.py in _raise_linalgerror_singular(err, flag)
     88 
     89 def _raise_linalgerror_singular(err, flag):
---> 90     raise LinAlgError("Singular matrix")
     91 
     92 def _raise_linalgerror_nonposdef(err, flag):

LinAlgError: Singular matrix

0 个答案:

没有答案