I would like to plot y data frame but I had an error like below. Any suggestions?
g1= sns.scatterplot( x= "events", y= "ways" , data = x2)
Output message
DataFrame' object has no attribute 'get
Seaborn expects pandas dataframes and not spark dataframes. Change your code to:
g1= sns.scatterplot( x= "events", y= "ways" , data = x2.toPandas())
and it should work.
Related
#import the needed pandas module
import pandas as pd
import statsmodels.formula.api as smf
#Upload the contents of an excel file to a DataFrame
df= pd.read_excel("C:/Users/ME/OneDrive/Desktop/weather.xlsx")
#Create a multiple logistic regression model
logRegModel = smf.logit('sunny ~ temp + barom', data = df)
#Fit the data in df into the model
results = logRegModel.fit()
#Print the results summary
print(results.summary())
#plot the scatterplot with the actual data
z = df.sunny
x = df.temp
y = df.barom
#make a prediction for a given temp x and barometer y reading
prediction = results.predict(pd.DataFrame({'temp': [21],'barom':[12]})
prediction.summary_frame(alpha=0.05)
# Creating figure
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize = (10, 7))
ax = plt.axes(projection ="3d")
# Creating plot
ax.scatter3D(x, y, z, color = "blue")
plt.title("3D scatter plot")
# show plot
plt.show()
I ran the code above. Everything works fine until it hits the code for making a prediction using a single x and a single y value. When I run the code to include:
#make a prediction for a given temp x and barometer y reading
prediction = results.predict(pd.DataFrame({'temp': [21],'barom':[12]})
prediction.summary_frame(alpha=0.05)
I recieve the following error:
File "<ipython-input-78-b26a4bf65d01>", line 36
from mpl_toolkits import mplot3d
^
SyntaxError: invalid syntax
This is so incredibly odd??? WHy does it run perfectly without the two prediction lines above and then when I include them it tells me a simple import function is a syntax error? It is my understanding reading the statsmodels docs, that in order to make a prediction for a multiple logistic regression model I need to pass a dataFrame into the predict function. Wasn't this done correctly above? My logistic regression is trying to predict if there is a sunny day from temperature and barameter reading. WHen I comment out the import statement above and run it I receive another error on another import statement. This is so strange. WHy soes it not accept my import statements? I ran the code on multiple IDEs and receive the same results. Thank you everyone in advance.
I'm trying to pull out the 7th and 9th fields from an RDD. I used the following code
val logData = sc.textFile("path",2).map(item => {val comps=item.split(" "); (comps(6).toFloat, comps(8).toFloat)})
But, I got the output as
(x1,y1)
(x2,y2)
(x3,y3)
where as I need the output as
x1 y1
x2 y2
x3 y3
Can anyone give me a solution to this
Do you mean as a String, such as "0.54 0.123"? If so, you could replace:
(comps(6).toFloat, comps(8).toFloat)
with
s"${comps(6).toFloat} ${comps(8).toFloat}"
(or you can use f"${comps(6).toFloat}%0.3f ${comps(8).toFloat}%0.3f" or similar instead of s"..." for greater control over formatting).
My teacher has given me a task of expressing a sound file(.wav) in polynomial form in MATLAB(curve fitting),.
I tried using polyfit() like this:-
a=wavread('filename');
x=linspace(0,1,239915);
p=polyfit(x,a,4);
display(p);
but I am getting error as
X and Y vectors must be the same size
Please help me.
Size of a is not 239915, it is for x we have taken these many samples, for line space, by the way, why do we need x for polyfit().
Thank you in advanced
Try this
a = wavread('filename')
x = linspace(0, 1, numel(a))';
p = polyfit(x,a,4);
display(p)
Note that I transpose linspace output as it returns a row vector where wavread returns a column vector, which is why you get the error message.
Im trying to plot a 3D function with Matlab, my script contains the following :
function f=Untitled(s1 ,s2 , s3)
s =[s1 s2 s3];
f= 0.0663 + 0.2099245 *(s(1)^2+s(2)^2+s(3)^2);
endfunction
xdata = linspace(36,36,36);
ydata = linspace(36,36,36);
zdata = linspace(36,36,36);
contour( xdata , ydata , zdata , [1 36 72 110])
I got the following error:
Error in ====> Untitled at 2
s=[s1 s2 s3]
I'm newest in Matlab, can any one help me to resolve this issue?
There are many issues in your code:
s = [s1 s2 s3];
This concatenates s1 s2 and s3 horizontally to variable s
To do that, "the number of rows in each variable should be same". That is the cause of your error.
You need to check the dimensions of the variable you passed to the
function untitled
endfunction is Octave syntax. You just need to use end for
Matlab
Then you are using s(1), s(2), s(3) separately inside your
function.
Then why did you cat them? you could have used the corresponding
variables itself like this:
f= 0.0663 + 0.2099245 *(s1^2+s2^2+s3^2);
Then you are using linspace(I don't know what you actually intend to do with linspace) for replicating values. Although this
doesn't pose any issues, there is a separate built-in function for
that called
repmat
You could use it like this: repmat(36,[1,36])
Note: Also name your function to any other name for good practice
I have a 1000x2 data file that I'm using for this problem.
I am supposed to fit the data with Acos(wt + phi). t is time, which is the first column in the data file, i.e. the independent variable. I need to find the fit parameters (A, f, and phi) and their uncertainties.
My code is as follows:
%load initial data file
data = load('hw_fit_cos_problem.dat');
t = data(:,1); %1st column is t (time)
x = t;
y = data(:,2); %2nd column is y (signal strength)
%define fitting function
f = fittype('A*cos(w*x + p)','coefficients','A','problem',{'w','p'});
% check fit parameters
coeffs = coeffnames(f);
%fit data
[A] = fit(x,y,f)
disp('confidence interval/errorbars');
ci = confint(A)
which yields 4 different error messages that I don't understand.
Error Messages:
Error using fit>iAssertNumProblemParameters (line 1113)
Missing problem parameters. Specify the values as a cell array with one element for each problem parameter
in the fittype.
Error in fit>iFit (line 198)
iAssertNumProblemParameters( probparams, probnames( model ) );
Error in fit (line 109)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in problem2 (line 14)
[A] = fit(x,y,f)
The line of code
f = fittype('A*cos(w*x + p)','coefficients','A','problem',{'w','p'});
specifies A as a "coefficient" in the model, and the values w and p as "problem" parameters.
Thus, the fitting toolbox expects that you will provide some more information about w and p, and then it will vary A. When no further information about w and p was provided to the fitting tool, that resulted in an error.
I am not sure of the goal of this project, or why w and p were designated as problem parameters. However, one simple solution is to allow the toolbox to treat A, w, and p as "coefficients", as follows:
f = fittype('A*cos(w*x + p)','coefficients', {'A', 'w', 'p'});
In this case, the code will not throw an error, and will return 95% confidence intervals on A, w, and p.
I hope that helps.
The straightforward answer to your question is that the error "Missing problem parameters" is generated because you have identified w and p as problem-specific fixed parameters,
but you have not told the fit function what these fixed values are.
You can do this by changing the line
[A] = fit(x,y,f)
to
[A]=fit(x,y,f,'problem',{100,0.1})
which supplies the values w=100 and p=0.1 in the fit. This should resolve the errors you specified (all 4 error messages result from this error)
In general specifying some of the quantities in your fit equation as problem-specific fixed parameters might be a valid thing to do - for example if you have determined them independently and have good reason to believe the values you obtained to be reliable. In this case, you might know the frequency w in this way, but you most probably won't know the phase p, so that should be a fit coefficient.
Hope that helps.