I've set up a Django project in which i create random points. These random points are stored in a database(sqlite) (i can see them via the admin website and change the values, so this works).
If i the write a script i can access the points and print them in a plot. See code below.
But if I then want to mine these points to sort them or only plot a selection of the dataset i seem to have trouble. If i readout the values they are not connected anymore and sorting x would mix up the point set.
Is there a way to sort the data set to a minimum value of in this case X and the sort the values and print the set? (keep all x, y, z and name value of the point intact?) (see answer below, point in Point3D.objects.all().order_by('x'):)
If i now want to to have the values of x between x = 12 and x = 30? how can i add this extra filter?
My code is as follows:
models.py:
class Point3D(models.Model):
name = models.CharField(max_length = 10)
x = models.DecimalField(max_digits=5, decimal_places=2)
y = models.DecimalField(max_digits=5, decimal_places=2)
z = models.DecimalField(max_digits=5, decimal_places=2)
generate the points:
from books.models import Point3D
def points():
for i in range(20):
x = random.randint(0,100)
y = random.randint(0,100)
z = random.randint(0,100)
p = Point3D(name = x , x = x ,y = y,z = z)
# print 'test'
p.save()
#
points()
in views.py:
def ThreeGraphs(request):
fig = Figure()
fig.suptitle('2D-punten')
ax = fig.add_subplot(111)
for point in Point3D.objects.all():
print point
name = int(point.name)
xs = int(point.x)
ys = int(point.y)
zs = int(point.z)
print (xs, ys, zs)
ax.plot(xs, ys, 'bo' )
HttpResponse(mimetype="image/png")
FigureCanvas(fig)
fig.savefig('template/images/testing.png')
picture = "testing.png"
return render_to_response('Test.html', {'picture': picture}, RequestContext(request))
Hope anyone knows how to solve my trouble.
Thanks a lot!
Tijl
You need to to this:
for point in Point3D.objects.all().order_by('x'):
This will return the points in sorted order by the 'x' field. You can say order_by('-x') to reverse the sort order.
Related
I was trying to solve a variance problem, but after the for loop I can't the sum values to finally dived by the numbers of items in the list.
lista = [1.86, 1.97, 2.05, 1.91, 1.80, 1.78]
n = len(lista) #NUMBERS OF DATA IN THE LIST
MA = sum(lista)/n #ARITHMETIC MEAN
for x in lista:
y = pow(x - MA, 2) #SUBTRACTION OF ALL DATA BY THE MA, RAISED TO THE POWER OF 2
print(y)
print(sum(y)/n) # AND THAT IS IT, I CAN'T FINISH
I‘m trying to do this work for days and I didn't discovery yet. Is it possible to finish or should I just quit it because there are better ways to solve it?
The result of the variance must be: 0.008891666666666652
PS: I don't want to have to install other programs or libs like pandas or numpy
You are just updating the value of y each time you run the loop so y will be a single element after it reached end of loop.
What you are printing is :
sum(pow(1.78 - MA, 2)/2)
So either you store each value of y inside function in a array of simply do a thing
y=0
y = pow(x-ma, 2)
y += y
I did it again and found the result, thanks Deepak Singh for your help, the asnwer is:
lista = [1.86, 1.97, 2.05, 1.91, 1.80, 1.78]
n = len(lista)
MA = sum(lista)/n
y = 0
for x in lista:
subts = pow(x - MA, 2)
y = (y + subts)
print("Varience:", y/n)
I have Three numpy arrays
X = [1,2,3,4,4,5,56,..,n]
Y = [1,2,344,4,4,4,..,n]
Z = [1,2,244,24,445,64,..,n]
I want to make output like this
final_list = [(X1,Y1,Z1),(X2,Y2,Z2),(X3,Y3,Z3), ... (Xn,Yn,Zn)]
And then to check if Z in any of them is > some threshold
Pop it up all with its correspondence X and Y
Is there some suggestions please?
I tried
np.conctatenate
but no any good results.
Thanks a lot:)
A simple way with if else could be:
X = [1,2,3,4,4,5,56]
Y = [1,2,344,4,4,4,89]
Z = [1,2,244,24,445,64,89]
d=[]
for i in range(len(X)):
if Z[i]>thresh:
print("print something")
else:
d.append([X[i],Y[i],Z[i]])
print(d)
if you check if z>thresh at the time of creating the list there is no need of poping those items later.
One way with just list comprehension:
out = [(x,y,z) for x,y,z in zip(X,Y,Z) if z<threshold]
With numpy you can do something like this:
xyz = np.array([X,Y,Z])
under_thresh = xyz[xyz[-1]<threshold]
I have a two datasets (x,y) in in table:
x = [4.14;5.07;3.61;4.07;3.68;4.13;3.95;3.88;5.41;6.14]
y = [69.78;173.07;19.28;32.88;15.87;53.73;41.69;35.14;228.08;267.11];
tb = table(x,y)
edges = linspace(30, 0, 61);
Based on this I have written following program
for k = 1:length(x)
New(k) = find(x(k)>edges,1,'last');
end
I want to see datasets y which satisfying condition of of above x values.
I'm trying to create a very simple example of a for steps in [] loop using a Polyline() inside an IronPython WPF application. Each iteration of the loop should draw a different colour however Brushes implements a set of predefined System.Windows.Media.SolidColorBrush objects. I can't work out how to swap Red for my steps variable.
def polylineShape(self):
x = self.myCanvas.Width/2
y = self.myCanvas.Height/2
polyline = Polyline()
polyline.StrokeThickness = 5
for steps in ['Red','Blue','Green','Black']:
x = x
y = x
polyline.Points.Add(Point(x,y))
x = x + 40
polyline.Points.Add(Point(x,y))
polyline.Stroke = Brushes.Red #change colour on iteration
self.myCanvas.Children.Add(polyline)
I created a solution with some trial and error, I couldn't work out how to pass colours directly to the Brushes type.
def polylineShape(self):
x = 0
y = 0
for steps in [Brushes.SteelBlue, Brushes.DarkOrange, Brushes.DarkSeaGreen, Brushes.Honeydew]:
polyline = Polyline()
polyline.StrokeThickness = self.myCanvas.Height/4
x = 0
y = y + self.myCanvas.Height/4
polyline.Points.Add(Point(x,y))
x = self.myCanvas.Width
polyline.Points.Add(Point(x,y))
polyline.Stroke = steps
self.myCanvas.Children.Add(polyline)
I am tring to get countour chart with sympy
I'm tring something like below but subs does not take array
and I tried for lambapy but lamdafy does not take 2 symbols or I don't know how to.
X,Y, formula = symbols('X Y formula')
formula = sp.sympify('X*2 + Y*3 +7*X*Y +34')
x = numpy.arange(1,10,1)
y = numpy.arange(1,10,1)
XValue,YValue = meshgrid(x,y)
ZValue = formula.sub([(X,XValue),(Y,YValue)])
Plot.contour(XValue, YValue, ZValue)
Are there any way to get result form 2 or more symbol with arrays
Answer was to lambdify the formula and get the result Z first. Then put XYZ value into the chart
X,Y, formula = symbols('X Y formula')
formula = sp.sympify('X*2 + Y*3 +7*X*Y +34')
x = numpy.arange(1,10,1)
y = numpy.arange(1,10,1)
XValue,YValue = meshgrid(x,y)
T = lambdify((x,y), formula,'numpy')
ZValue = T(XValue,YValue )
Plot.contour(XValue, YValue, ZValue)