Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I successfully created my function but when I call it I get the error:
not defined function ' elbow'
What's wrong?
import pandas as pd
import matplotlib.pyplot as plt
import seaborn
from sklearn.cluster import KMeans
import numpy as np
from scipy.spatial.distance import cdist, pdist
def eblow(df,n):
kMeansVar = [KMeans(n_clusters=NUM_CLUSTERS, n_jobs=1, random_state=0, n_init=1, verbose=True).fit(df) for k in range(1, n)]
centroids = [X.cluster_centers_ for X in kMeansVar]
k_euclid = [cdist(df, cent) for cent in centroids]
dist = [np.min(ke, axis=1) for ke in k_euclid]
wcss = [sum(d**2) for d in dist]
tss = sum(pdist(df)**2)/df.shape[0]
bss = tss - wcss
plt.plot(bss)
plt.show()
X=np.random.rand(60,45)
el=elbow(X,30)
You defined eblow but I don't see a function elbow.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
How to fix these errors that are appearing again and again??????
You should check out matplotlib.pyplot.show() and .plot() documents to know how to use them correctly.
You should use plt.plot(x, y) instead of print(plt.show(x, y))
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 25)
y = x * x + 2
plt.plot(x, y)
plt.show()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
enter image description here
import numpy as np
import numpy.linalg as LA
import pandas as pd
import matplotlib.pyplot as plt
path = "./A.csv";
A = pd.read_csv(path).values;
tposeA = np.transpose(A);
normA = LA.norm(A);
X0 = tposeA / ((normA)**2)
print(X0)
I have been working on this piece and it returns strange result in the matrix, do anyone know what this means?
Many thanks!!
[First col and 4th row]
[0.0089 0.0035 0.0017 0.0053]
[0.0035 0.0089 0.0035 0.0089]
[0.0035 0.0178 0.0106 0.0124]
[0.(blank) 0.0017 0.0124 0.0267]
Since you are looking at floating point numbers, 0. means that the number equals exactly zero. It is the way NumPy displays float.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
import statsmodels.api as sm
def train(x, y):
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(x,y)
return model
data = pd.read_csv("/home/andand/Downloads/Advertising.csv")
model = train(x,y)
x_new = 23.0
y_new = model.predict(x_new)
print(y_new)
NameError Traceback (most recent call last)
<ipython-input-4-d0295eb57fc4> in <module>
11 return model
12 data = pd.read_csv("/home/andand/Downloads/Advertising.csv")
---> 13 model = train(x,y)
14 x = data
15 x_new = 23.0
NameError: name 'x' is not defined
I suspect that my mistake is that I am not importing the training set correctly, please tell me how to do it correctly in this case.
define x and y first.
Try
data = pd.read_csv("/home/andand/Downloads/Advertising.csv")
y = data[-1]
x = data[:-1]
model = train(x,y)
You have to know what is "x" in data and what is "y"
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am new to Python.
Currently I am trying to solve the 4th order diff eqn d4y/dx4=1, using python with two boundary conditions y(0)=y(L)=0. I am using the bvp solver for the same but am facing errors. The code has been attached below:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from scipy.integrate import solve_bvp
def fun(y,x) :
return np.vstack(y[1],y[2],y[3],1)
def bc(ya,yb) :
return np.array(ya[0],ya[1],yb[0],yb[1])
xmesh= np.linspace(0,10,1000)
y= np.zeros((2,xmesh.size))
sol=solve_bvp(fun,bc,xmesh,y)
It is fun(x, y), the arguments are the other way around. Also the 1 needs to be a vector of suitable size.
Lacking some parenthesis in the array for bc.
Your initial guess y needs to be of size (4, len(xmesh))
Whole things looks like this then:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from scipy.integrate import solve_bvp
def fun(x, y):
return np.vstack([y[1], y[2], y[3], np.ones(y.shape[1])])
def bc(ya, yb) :
return np.array([ya[0], ya[1], yb[0], yb[1]])
xmesh = np.linspace(0, 10, 100)
y = np.zeros((4, len(xmesh)))
sol = solve_bvp(fun, bc, xmesh, y)
plt.figure()
for i in range(4):
plt.plot(xmesh, sol.sol(xmesh)[i], label = str(i+1))
plt.legend()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to matplotlib, and I want to create a plot, with the following information:
A line joining the medians of around 200 variable length vectors (input)
A line joining the corresponding quantiles of these vectors.
A line joining the corresponding spread (largest and smallest points).
So basically, its somewhat like a continuous box plot.
Thanks!
Using just scipy and matplotlib (you tagged only those libraries in your question) is a little bit verbose, but here's how you would do it (I'm doing it only for the quantiles):
import numpy as np
from scipy.stats import mstats
import matplotlib.pyplot as plt
# Create 10 columns with 100 rows of random data
rd = np.random.randn(100, 10)
# Calculate the quantiles column wise
quantiles = mstats.mquantiles(rd, axis=0)
# Plot it
labels = ['25%', '50%', '75%']
for i, q in enumerate(quantiles):
plt.plot(q, label=labels[i])
plt.legend()
Which gives you:
Now, I would try to convince you to try the Pandas library :)
import numpy as np
import pandas as pd
# Create random data
rd = pd.DataFrame(np.random.randn(100, 10))
# Calculate all the desired values
df = pd.DataFrame({'mean': rd.mean(), 'median': rd.median(),
'25%': rd.quantile(0.25), '50%': rd.quantile(0.5),
'75%': rd.quantile(0.75)})
# And plot it
df.plot()
You'll get:
Or you can get all the stats in just one line:
rd.describe().T.drop('count', axis=1).plot()
Note: I dropped the count since it's not a part of the "5 number summary".