How to obtain all possible values using interpolate of scipy? - python-3.x

I'm trying to identify all the y-axis values when my data passes through zero on the x-axis. Using scipy's interpolation function it only shows me the last value, why? or Can anyone help me to deal with this issue?
This is my code:
x = <https://drive.google.com/file/d/1shJtrg9orwLFm9Da5wXaysbQZVtZYO-r/view?usp=share_link>
y = np.arange(0,150,1)
y_interp = interp1d(x, y, kind='cubic')
y_interp(0)
The result is:
array(112.72889837)
But it also passes by ~50.

Related

How to fix scatter plot not sorting data on y - axis?

I am trying to graph these two sets of data I read from a csv file with pandas. For some reason, the data always fits into a line and consequently the y axis gets messed up. How would I get this to graph correctly?
What it looks like is happening is that both your x and y variables are stored as strings, so the plot is showing you the string pairs in order. Try converting them to floats:
Example code:
arr0 = [float(x) for x in pred2[:,0]]
And do similarly for arr1 and it should plot as expected. Let me know if it works!

Scipy stats.probplot not returning r^2 value bug?

I'm currently plotting the normal proability plot of a set of feature variables in a data matrix X using scipy. However, the module I'm using is not returning my r^2 value. Here's my simple code:
Data_Matrix=pd.read_csv('My csv')
My_datum=My_Data.as_matrix()
#loop through all feature variables
for i in range(My_datum.shape[1]):
#just a simple print line for the column index and its shapiro test
results
print(i)
print(a)
#plot
pplot=stats.probplot(My_datum[:,i],dist='norm',fit=True, plot=plt )
a=sp.stats.shapiro(My_datum[:,i])
I've tried using the same line on some simpler numpy array, but to no avail. I'm working the Ipython console on spyder 3. Thanks in advance!
Probplot returns the R value already. You just need to square it to give you R^2.
(slope, intercept, r) = stats.probplot(My_datum[:,i], dist='norm', fit=True, plot=plt)
R_squared = r**2
So this is a little weird but it returns a list of tuples. You actually just need to extract the second tuple from the list by indexing the probplot function. You'll need to unpack the tuple to then square it.
slope, intercept, r = stats.probplot(My_datum[:,i], dist='norm', fit=True, plot=plt)[1]
r2 = r**2

fit function in gnuplot at x-log(y) scale

My data has two columns: date (in Month/Year format) and corresponding value. I plotted this data on x-log(y) scale using gnuplot. It looks very close to a straight line. I am interested to draw a straight line using curve fitting. I tried with few fit functions but did not get success.
I tried the following fit functions:
f(x) = a * x + b (f(x) is not linear as scale is x-log(y))
f(x) = a*10**x + b (overflow error)
Any help in this regard would be appreciated.
The overflow error should be due to at least one large value of x. If you can rescale the x data so that there is no overflow when calculating 10**x, the fit might work. As a test, try something like:
x_scaled = x / 1000.0
f(x_scaled) = a*10**x_scaled + b
Inspecting the maximum value of x will give you an idea of the scaling value, shown as 1000.0 in my example.

How to use Matlab to make a heat map with values given by coordinates from Excel

data = xlsread('excel.xlsx');
x = linspace(min(data(:,5)),max(data(:,5)),150);
y = linspace(min(data(:,6)),max(data(:,6)),150);
[X,Y] = meshgrid(x,y);
F=TriScatteredInterp(data(:,5),data(:,6)-1);
contourf(X,Y,F(X,Y),150,'LineColor','none')
Error: Error using TriScatteredInterp
The input points must be 2D or 3D coordinates in numpoints-by-ndim format.
Error in testing (line 6):
F=TriScatteredInterp(data(:,5),data(:,6)-1);
I wish to plot the heatmap from 2 column of data from excel. What other method can be used to construct a heat map as shown below?Any help is appreciated.

How to produce X values of a stretched graph?

I'm trying to "normalize" monthly data in a way.
What I mean by that is, I need to take daily values and check the data from each month against the data in another month.
The only problem with this is that some months are longer than others. So I have come up with a way that I want to do this, but I'm kind of confused as to exactly how to do it...
Basically, I'm looking at this website: http://paulbourke.net/miscellaneous/interpolation/ and trying to transform each set of coordinates into a graph with 31 X- and Y-values (I would like to use the Cosine interpolator, but I'm still trying to figure out what to do with it).
Now, the X values have a function. I can pass in something like (1..28) and morph it into 31 values...simple enough code, something like this works for me:
def total = (1..30)
def days = 28
def resize = {x, y->
result = []
x.each{ result << (it * (y/x.size())}
return result
}
resize(total,days)
Which returns a list of 31 Y-values spanning from 0 to 28.
My question is: How do I translate a list of the corresponding Y values to these values? I'm having a really hard time wrapping my head around the concept and could use a little help.
My first thought was to simply run the Y-values through this function too, but that returns values that are all lower than the original input.
I'm looking for something that will retain the values at certain points, but simply stretch the graph out horizontally.
For example, at the x value at (1/3) of the graph's length, the value needs to be the same as it would be at (1/3) of the original graph's length.
Can anyone help me out on this? It's got me stumped.
Thanks in advance!
Not sure where the problem lies with this, so I made up some data
I think your algorithm is correct, and you only need to normalize the x-axis.
I came up with this code (and some plots) to demonstrate what I believe is the answer
Define some x and y values:
def x = 1..30
def y = [1..15,15..1].flatten()
Then, generate a list of xy values in the form: [ [ x, y ], [ x, y ], ...
def xy = [x,y].transpose()
If we plot this list, we get:
Then define a normalize function (basically the same as yours, but it doesn't touch the y value)
def normalize( xylist, days ) {
xylist.collect { x, y -> [ x * ( days / xylist.size() ), y ] }
}
Then we can normalize our list to 28 days
def normalxy = normalize( xy, 28 )
Now, if we plot these points, we get
As you can see, both plots have the same shape, they are just different widths...
Have I missed the point?

Resources