Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
row_indices = [row_indices]
if (len(row_indices) == 0) or (row_indices == [0]) or (row_indices is None):
return self
# Where do the submatrices begin and end?
lower_idx = np.append(0, row_indices)
upper_idx = np.append(row_indices, len(self.df))
dfs = []
for idx, a, b in zip(range(len(lower_idx)), lower_idx, upper_idx):
new_columns = dict(
('{}'.format(val), '{}'.format(val) + ' {}'.format(idx + 1))
for val in list(self.df.columns))
dfs.append(self.df[a:b].rename(columns=new_columns))
new_df = pd.concat(dfs, axis=1).fillna(0)
prior_mu = np.hstack([self.prior_mu for idx in range(len(dfs))])
prior_sigma = np.hstack([self.prior_sigma for idx in range(len(dfs))])
return DesignMatrix(new_df, name=self.name, prior_mu=prior_mu,
prior_sigma=prior_sigma)
Returns
-------
`.DesignMatrix`
A new design matrix with PCA applied.
"""
# nterms cannot be langer than the number of columns in the matrix
if nterms > self.shape[1]:
nterms = self.shape[1]
# We use `fbpca.pca` instead of `np.linalg.svd` because it is faster.
# Note that fbpca is randomized, and has n_iter=2 as default,
# we find this to be too few, and that n_iter=10 is still fast but
# produces more stable results.
from fbpca import pca # local import because not used elsewhere
new_values, _, _ = pca(self.values, nterms, n_iter=10)
return DesignMatrix(new_values, name=self.name)
def append_constant(self, prior_mu=0, prior_sigma=np.inf):
"""Returns a new `.DesignMatrix` with a column of ones appended.
Returns
-------
`.DesignMatrix`
New design matrix with a column of ones appended. This column is
named "offset".
"""
extra_df = pd.DataFrame(np.atleast_2d(np.ones(self.shape[0])).T, columns=['offset'])
new_df = pd.concat([self.df, extra_df], axis=1)
prior_mu = np.append(self.prior_mu, prior_mu)
prior_sigma = np.append(self.prior_sigma, prior_sigma)
return DesignMatrix(new_df, name=self.name,
prior_mu=prior_mu, prior_sigma=prior_sigma)
left unchanged.
Returns
-------
`.DesignMatrix`
A new design matrix with median-subtracted & sigma-divided columns.
"""
ar = np.asarray(np.copy(self.df))
ar[ar == 0] = np.nan
# If a column has zero standard deviation, it will not change!
is_const = np.nanstd(ar, axis=0) == 0
median = np.atleast_2d(np.nanmedian(ar, axis=0)[~is_const])
std = np.atleast_2d(np.nanstd(ar, axis=0)[~is_const])
ar[:, ~is_const] = (ar[:, ~is_const] - median) / std
new_df = pd.DataFrame(ar, columns=self.columns).fillna(0)
return DesignMatrix(new_df, name=self.name)
matrix values.
Parameters
----------
ax : `~matplotlib.axes.Axes`
A matplotlib axes object to plot into. If no axes is provided,
a new one will be created.
**kwargs : dict
Extra parameters to be passed to `.plot_image`.
Returns
-------
`~matplotlib.axes.Axes`
The matplotlib axes object.
"""
temp_dm = DesignMatrix(pd.concat([d.df for d in self], axis=1))
ax = temp_dm.plot(**kwargs)
ax.set_title("Design Matrix Collection")
return ax