def createMatrixInteract(self, event): dlg = RegressDialog(self.parent, "Matrix Interaction Plot") log = wx.CheckBox(dlg, label="Logistic Fit?") dlg.Add(log) if dlg.ShowModal() == wx.ID_OK: y, xs = dlg.GetValue() log = log.GetValue() data = self.parent.data[list(xs) + [y]] df = data[list(xs)] fig, axes = plt.subplots(nrows=len(xs), ncols=len(xs)) for i, l1 in enumerate(df): for j, l2 in enumerate(df): ax = axes[j, i] ax.grid(False) plt.subplot(ax) if i == j: sns.regplot(data[l1], data[y], ax=ax), # would like to do logistic plot, but takes too long elif i < j: sns.interactplot(l1, l2, y, data, ax=ax, logistic=log, cmap=settings["cmap"]) if i != 0 and j != 0: ax.yaxis.set_visible(False) if j != len(xs) - 1: ax.xaxis.set_visible(False) plt.show()
def make_plot(X_train, y_train, X, y, test_data, model, model_name, features, response): feature = X.columns f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharey=False) sns.regplot(X[feature[4]], y, test_data, ax=ax1) sns.boxplot(X[feature[4]], y, color="Blues_r", ax=ax2) model.fit(X_train, y_train) sns.residplot(X[feature[4]], (model.predict(X) - y) ** 2, color="indianred", lowess=True, ax=ax3) if model_name is 'linear': sns.interactplot(X[feature[3]], X[feature[4]], y, ax=ax4, filled=True, scatter_kws={"color": "dimgray"}, contour_kws={"alpha": .5}) elif model_name is 'logistic': pal = sns.blend_palette(["#4169E1", "#DFAAEF", "#E16941"], as_cmap=True) levels = np.linspace(0, 1, 11) sns.interactplot(X[feature[3]], X[feature[4]], y, levels=levels, cmap=pal, logistic=True) else: pass ax1.set_title('Regression') ax2.set_title(feature[4]+' Value') ax3.set_title(feature[4]+' Residuals') ax4.set_title('Two-value Interaction') f.tight_layout() plt.savefig(model_name+'_'+feature[4], bbox_inches='tight') # Multi-variable correlation significance level f, ax = plt.subplots(figsize=(10, 10)) cmap = sns.blend_palette(["#00008B", "#6A5ACD", "#F0F8FF", "#FFE6F8", "#C71585", "#8B0000"], as_cmap=True) sns.corrplot(test_data, annot=False, diag_names=False, cmap=cmap) ax.grid(False) ax.set_title('Multi-variable correlation significance level') plt.savefig(model_name+'_multi-variable_correlation', bbox_inches='tight') # complete coefficient plot - believe this is only for linear regression sns.coefplot("diagnosis ~ "+' + '.join(features), test_data, intercept=True) plt.xticks(rotation='vertical') plt.savefig(model_name+'_coefficient_effects', bbox_inches='tight')
def interactplot(ax): rs = np.random.RandomState(11) n = 80 x1 = rs.randn(n) x2 = x1 / 5 + rs.randn(n) b0, b1, b2, b3 = 1.5, 4, -1, 3 y = b0 + b1 * x1 + b2 * x2 + b3 * x1 * x2 + rs.randn(n) sns.interactplot(x1, x2, y, colorbar=False, ax=ax) ax.set_title("interactplot()")
def _update_plot(self, axis, view, style): style.pop('zorder', None) if self.plot_type == 'factorplot': opts = dict(style, **({'hue': view.x2} if view.x2 else {})) sns.factorplot(x=view.x, y=view.y, data=view.data, **opts) elif self.plot_type == 'regplot': sns.regplot(x=view.x, y=view.y, data=view.data, ax=axis, **style) elif self.plot_type == 'boxplot': style.pop('return_type', None) style.pop('figsize', None) sns.boxplot(view.data[view.y], view.data[view.x], ax=axis, **style) elif self.plot_type == 'violinplot': if view.x: sns.violinplot(view.data[view.y], view.data[view.x], ax=axis, **style) else: sns.violinplot(view.data, ax=axis, **style) elif self.plot_type == 'interact': sns.interactplot(view.x, view.x2, view.y, data=view.data, ax=axis, **style) elif self.plot_type == 'lmplot': sns.lmplot(x=view.x, y=view.y, data=view.data, ax=axis, **style) elif self.plot_type in ['pairplot', 'pairgrid', 'facetgrid']: style_keys = list(style.keys()) map_opts = [(k, style.pop(k)) for k in style_keys if 'map' in k] if self.plot_type == 'pairplot': g = sns.pairplot(view.data, **style) elif self.plot_type == 'pairgrid': g = sns.PairGrid(view.data, **style) elif self.plot_type == 'facetgrid': g = sns.FacetGrid(view.data, **style) for opt, args in map_opts: plot_fn = getattr(sns, args[0]) if hasattr( sns, args[0]) else getattr(plt, args[0]) getattr(g, opt)(plot_fn, *args[1:]) if self._close_figures: plt.close(self.handles['fig']) self.handles['fig'] = plt.gcf() else: super(SNSFramePlot, self)._update_plot(axis, view, style)
def bvContour(x1, x2, y, **kwargs): contour_plot = plt.figure() outfile = kwargs.pop("savefig", None) sns.interactplot(x1, x2, y, filled=True, scatter_kws={ "marker": "x", "markersize": 1 }, contour_kws={"linewidths": 2}, **kwargs) if outfile: plt.title(kwargs.pop("title", "")) contour_plot.savefig(outfile) plt.close() return contour_plot
def createInteraction(self, event): dlg = GraphDialog(self.parent, "Matrix Plot Input", ("X1", "X2", "Y"), size=(700, 200), add=False, groups=False) fill = wx.CheckBox(dlg, label="Fill") fill.SetValue(True) log = wx.CheckBox(dlg, label="Logistic Fit?") dlg.Add(fill) dlg.Add(log) if dlg.ShowModal() == wx.ID_OK: (x1, x2, y), fill = dlg.GetName()[0], fill.GetValue() log = log.GetValue() data = self.parent.data[[x1, x2, y]].astype(float) dlg.Destroy() temp = data[[x1, x2, y]] sns.interactplot(x1, x2, y, temp, cmap=settings["cmap"], filled=fill, logistic=log) plt.show()
def interact(model, data, x, y, z, **kwargs): df = data.as_dataframe(_combine_keys(x, y, z)) sns.interactplot(x, y, z, df, **kwargs)
parameters["density"] = mu["density"] parameters["energy"] = mu["energy"] parameters["temperature"] = temperature data.append(parameters) data = pd.DataFrame(data).dropna() Q = data.pivot_table(index=["q0", "sigma0"], columns=["temperature"], values=["energy", "density"]) Q = data.pivot_table(index=["q0", "sigma0"], columns="temperature", values="energy") dE0 = (Q[300.] - Q[280.]) dE0.name = "dE0" dE0 = dE0.reset_index() dE0.dropna(inplace=True) temperature = 280. ind = data.temperature == temperature mydata = data[ind] figure() sns.interactplot("q0", "sigma0", "density", mydata, cmap="coolwarm", filled=True, levels=25); figure() sns.interactplot("q0", "sigma0", "dE0", dE0, cmap="coolwarm", filled=True) data["sigmasum"] = data["sigma0"] + data["sigma1"] data["epsilonsqrt"] = (data["epsilon0"] * data["epsilon1"]) ** 0.5 figure() sns.interactplot("sigmasum", "epsilonsqrt", "density", data, cmap="coolwarm", filled=True)
""" Continuous interactions ======================= """ import numpy as np import pandas as pd import seaborn as sns sns.set(style="darkgrid") # Generate a random dataset with strong simple effects and an interaction n = 80 rs = np.random.RandomState(11) x1 = rs.randn(n) x2 = x1 / 5 + rs.randn(n) b0, b1, b2, b3 = .5, .25, -1, 2 y = b0 + b1 * x1 + b2 * x2 + b3 * x1 * x2 + rs.randn(n) df = pd.DataFrame(np.c_[x1, x2, y], columns=["x1", "x2", "y"]) # Show a scatterplot of the predictors with the estimated model surface sns.interactplot("x1", "x2", "y", df)
#On the diagonals we have bivariate kernel density estimation g.map_offdiag(sns.kdeplot, cmap="Blues_d", n_levels=6); #It is often useful to give some relationships a closer look sns.set() g = sns.jointplot("educ", "loyalty", data=reduced, kind="reg", color="b") #Violin plots are a good way to not learn about distributions sns.set_style("whitegrid") sns.violinplot(y='loyalty', data=reduced) #also distributions that are affected by independent variables sns.violinplot(x='age', y='loyalty', hue='gender', data=reduced, split=True) #Interaction plots are a great way to learn about multivariate interactions sns.interactplot(x1='Emotional', x2='Trust', y='loyalty', data=reduced) #Step 3 #Think! Come up with some hypothesis! #Step 4 Test #the usual social science tests #Hypothesis: Emotional people are more loyal #Simple linear regression res = sm.ols(formula="loyalty ~ Emotional", data=reduced).fit() res.summary() #Answer: Yes they are but that is not enought to explain loyalty
#data = pd.read_hdf("./symmetric-grid2.h5", 'data') data = pd.read_hdf("./ccl4-grid.h5", 'data') Q = data.pivot_table(index=keys, columns=["temperature"], values=["energy", "density", "dielectric", "kappa"]) temperature = 280. ind = data.temperature == temperature mydata = data[ind] figure() sns.interactplot(keys[0], keys[1], "density", mydata, cmap="coolwarm", filled=True, levels=25) figure() sns.interactplot(keys[0], keys[1], "evap", mydata, cmap="coolwarm", filled=True, levels=25) figure() sns.interactplot(keys[0],
# axarr[1, 1].set_title('Axis [1,1]') # In[166]: X.cholesterol # In[167]: from sklearn.grid_search import GridSearchCV # In[168]: sns.interactplot(X[X.columns[4]], X[X.columns[5]], y.values, ax=ax4, cmap="coolwarm", filled=True, levels=25) # In[169]: y.values # In[170]: from sklearn.svm import SVC linear_reg = LinearRegression() parameters = {'normalize':(True, False)} # In[306]:
import seaborn as sns sns.set() from astropy.table import Table import pandas import numpy as np import sys # table = Table.read(sys.argv[1]) table = pandas.read_csv(sys.argv[1]).dropna(how='all') # table.pivot("AZ", "ALT") ax1 = sns.interactplot("AZ", "ALT", "AIRMASS", data=table) sns.plt.show()
# ---------------------- sns.set(style="darkgrid") ax = plt.subplot(gs[2:4, 1]) plt.title("interactplot()") rs = np.random.RandomState(11) n = 80 x1 = rs.randn(n) x2 = x1 / 5 + rs.randn(n) b0, b1, b2, b3 = 1.5, 4, -1, 3 y = b0 + b1 * x1 + b2 * x2 + b3 * x1 * x2 + rs.randn(n) sns.interactplot(x1, x2, y, colorbar=False, ax=ax) # Correlation matrix # ------------------ ax = plt.subplot(gs[4:, 0]) rs = np.random.RandomState(0) x0, x1 = rs.randn(2, 60) x2, x3 = rs.multivariate_normal([0, 0], [(1, -.5), (-.5, 1)], 60).T x2 += x0 / 8 x4 = x1 + rs.randn(60) * 2 data = np.c_[x0, x1, x2, x3, x4] sns.corrplot(data, ax=ax)
import pymc import sklearn.gaussian_process import os import pandas as pd import glob #keys = ["q0", "sigma0"] keys = ["q0", "sigma1"] #data = pd.read_hdf("./symmetric-grid2.h5", 'data') data = pd.read_hdf("./ccl4-grid.h5", 'data') Q = data.pivot_table(index=keys, columns=["temperature"], values=["energy", "density", "dielectric", "kappa"]) temperature = 280. ind = data.temperature == temperature mydata = data[ind] figure() sns.interactplot(keys[0], keys[1], "density", mydata, cmap="coolwarm", filled=True, levels=25); figure() sns.interactplot(keys[0], keys[1], "evap", mydata, cmap="coolwarm", filled=True, levels=25); figure() sns.interactplot(keys[0], keys[1], "dielectric", mydata, cmap="coolwarm", filled=True, levels=25); figure() sns.interactplot(keys[0], keys[1], "kappa", mydata, cmap="coolwarm", filled=True, levels=25);