seaborn.regplot() fits a linear model to 2D data. It would be very convenient to get the parameters for the linear model returned from the function. However, it only returns an axis object. Is there a way to extract the data from the axis object? Is there a seaborn function that I could call in order to produce these parameters for me? Or would I need to go all the way to call my own fitting function?
x = range(10)
y = sin(x)
df = pd.DataFrame({'x': x, 'y': y})
ax = sns.regplot('x', 'y', df)
This would be very useful for figures that go into publications.
Related
So I'm working on a project and I have a set of data that I loaded in as a csv. The data has a spot that that I need to flatten out. I used the numpy.polyfit() function to find a line of best fit, but what I can't seem to figure out is how to subtract off the best fit line. Any advice?
Here is the code I'm using so far:
μ = pd.read_csv("C:\\Users\\ander\\Documents\\Data\\plots and code\\dataframe2.csv")
yvalue = "average"
xvalue = "xvalue"
X = μ[xvalue][173:852]
Y = μ[yvalue][173:852]
fit = np.polyfit(X, Y, 1)
μ = μ.subtract(fit, μ)
The polyfit function finds the linear coefficient of the best fit. In order to subtract the line from your data, you first need to create the linear function itself. For example, you can use the numpy.poly1d function.
I'll show you an example. Since we don't have access to the .csv file I made up X and Y:
import matplotlib.pyplot as plt
import numpy as np
DATA_SIZE = 500
μ_X = np.sort(np.random.uniform(0,10,DATA_SIZE))
μ_Y = 3*np.exp(-(μ_X-7)**2) + np.random.normal(0,0.08,DATA_SIZE) + 0.5*μ_X
X = μ_X[50:200]
Y = μ_Y[50:200]
plt.scatter(μ_X, μ_Y, label='Full data')
plt.scatter(X, Y, label='Selected region')
plt.legend()
plt.show()
Now we can fit the baseline from the orange data and subtract the linear function from all the data (blue).
fit = np.polyfit(X, Y, 1)
linear_baseline = np.poly1d(fit) # create the linear baseline function
μ_Y = μ_Y - linear_baseline(μ_X) # subtract the baseline from μ_Y
plt.scatter(μ_X, μ_Y, label='Linear baseline removed')
plt.legend()
plt.show()
I have data that is only accessible as a 2D point plot saved as pdf-file and need the raw data (the x and associated y values) out of it.
Is there any way I can do this with Mathematica, so that I am able to use the data internally for evaluation?
An example plot to Import would be (ListPlot of x^2; x=0-10)
Here is an approach you could take with Mathematica
img = First#Import[
"https://drive.google.com/uc?export=download&id=1Kgny29eM8q2oIj7BopP-dx0HQ4E449P_"];
mb = MorphologicalBinarize[img];
cn = ColorNegate[Closing[mb, DiskMatrix[0.5]]];
coords = Flatten[Last /# ComponentMeasurements[cn, {"Centroid"}], 1];
ListPlot[coords]
You will have to appropriately scale the coordinates if you want them to exactly match y = x^2.
as part of Unity's ML Agents images fed to a reinforcement learning agent can be converted to greyscale like so:
def _process_pixels(image_bytes=None, bw=False):
s = bytearray(image_bytes)
image = Image.open(io.BytesIO(s))
s = np.array(image) / 255.0
if bw:
s = np.mean(s, axis=2)
s = np.reshape(s, [s.shape[0], s.shape[1], 1])
return s
As I'm not familiar enough with Python and especially numpy, how can I get the dimensions right for plotting the reshaped numpy array? To my understanding, the shape is based on the image's width, height and number of channels. So after reshaping there is only one channel to determine the greyscale value. I just didn't find a way yet to plot it yet.
Here is a link to the mentioned code of the Unity ML Agents repository.
That's how I wanted to plot it:
plt.imshow(s)
plt.show()
Won't just doing this work?
plt.imshow(s[..., 0])
plt.show()
Explanation
plt.imshow expects either a 2-D array with shape (x, y), and treats it like grayscale, or dimensions (x, y, 3) (treated like RGB) or (x, y, 4) (treated as RGBA). The array you had was (x, y, 1). To get rid of the last dimension we can do Numpy indexing to remove the last dimension. s[..., 0] says, "take all other dimensions as-is, but along the last dimension, get the slice at index 0".
It looks like the grayscale version has an extra single dimension at the end. To plot, you just need to collapse it, e.g. with np.squeeze:
plt.imshow(np.squeeze(s))
I have a histogram of my data:
h is a 1-d array of counts
x is a 1-d array of bin values
Now if I do:
sns.kdeplot(h, shade=True);
I get a plot where x-axis goes from -20 to 100, which has nothing to do with
my original x data. How do I get the x-axis scaled to match my data?
I see I misunderstood the input to kde. It wants the original values. I had already created a histogram and wanted to feed that to kde.
In my histogram I have h.buckets, and h.results. I did
def hist_to_values (hist):
ret = []
for x,y in zip (hist.buckets, h.results):
ret.extend ([x] * y)
return np.array (ret)
Then feed this to kde, and I got the results I expect.
I am implementing the z transformation and I am using the built-in function ztrans in Matlab. Now i give
x=imread('lena512.bmp');
x=im2double(x);
z=ztrans(x);
where x contains the pixel values of an image and ztrans(x) should apply z-transformation. But i am getting an error like this
??? Undefined function or method 'ztrans' for input arguments of type 'double'.
How can I use the function and apply z transformation on images?
Z = zscore(X) returns the z-score for each element of X such that columns of X are centered to have mean 0 and scaled to have standard deviation 1. Z is the same size as X.
so in order to do what you wanted you should use this instead
x= zscore(x)
imshow(x,[])
keep in mind that this will give you some weird results as this is done for each column, to create a global transformation you should do the following
[m,n]= size(x)
x= zscore(x(:))
x = reshape(x,m,n)
imshow(x,[])
enjoy
enjoy