I apologize in advance is this question is too broad, but I need some help conceptualizing.
The end result is that I want to enable radius-based searching. I am using Django. To do this, I have two classes: Users and Places. Inside the Users class is a function that defines the radius in which people want to search. Inside the Places class I have a function that defines the midpoint if someone enters a city and state and not a zip (i.e., if someone enters New York, NY a lot of zipcodes are associated with that so I needed to find the midpoint).
I have those two parts down. So now I have a radius where people want to search and I know (the estimate) of the places. Now, I am having a tremendous amount of difficulty combining the two, or even thinking about HOW to do this.
I attempted doing the searching against each other in the view, but I ran into a lot of trouble when I was looping through one model in the template but trying to display results based on an if statement of the other model.
It seemed like a custom template tags would be the solution for that problem, but I wanted to make sure I was conceptualizing the problem correctly in the first place. I.e.,
Do I want to do the displays based on an if statement in the template?
Or should I be creating another class based on the other two in my models file?
Or should I create a new column for one of the classes in the models file?
I suppose my ultimate question is, based on what it is I want to do (enable radius based searching), where/how should most of the work be done? Again, I apologize if the question is overly broad.
Perhaps you could put it in the view which renders the search page.
asuuming you have a view function like search you could:
get users radius request.user.get_radius
search for places based on that radius relevant_places = Places.get_all_places_in_radius
Render those places to a user
Based on what you are describing, I believe GeoDjango would be worth your time to look into: http://geodjango.org/
Especially if you want to enable radius based searching, most of the heavy lifting is already done by GeoDjango, you'll just have to invest some time learning how to use it (which is a small fraction of the time you would have had to spend "reinventing the wheel", so to speak)
I just decided to add the function to the view so that the information can be input directly into the model after a user enters it. Thanks for the help. I'll probably wind up looking into geodjango.
Related
I have a website built with Node.js and MongoDB. Documents are structured something like this:
{
price: 500,
location: [40.23, 49.52],
category: "A"
}
Now I want to create a recommendation system, so when a user is watching item "A" I can suggest to him/her similar items "B", "C" and "D".
The thing is collection of items is changing relatively often. New items are created every hour and they do exist only for about a month.
So my questions are:
What algorithm should I use? Cosine similarity seems to be the most suitable one.
Is there a way to create such recommendation system with Node.js or it's better to use python/R?
When similarity score must be calculated? Only once (when a new item is created) or I should recalculate it every time a user visits an item page?
What algorithm should I use? Cosine similarity seems to be the most suitable one.
No one can really answer this for you, what makes a product similar to you? this is 100% product decision, it sounds like this is more of a pet side project and in that case I'd say use whatever you'd like.
If this is not the case I would assume best recommendations would be based on purchase correlation, i.e previous Users that bought product "A" also bought (or looked) at product "B" the most, hence it should be the top recommendation. Obviously you can create a much more complex model in the future.
Is there a way to create such recommendation system with Node.js or it's better to use python/R?
If it's a basic rule based system it can be done in node with ease, for any more data science related approach it will be more natural to implement this in python/R
When similarity score must be calculated? Only once (when a new item is created) or I should recalculate it every time a user visits an item page?
Again it depends on what your score is, how many resources you can invest, what the scale is etc.
as I mentioned before It sounds like this is a personal project. If this is the case I would try and choose the simpler solution for all these questions. Once you have the entire project up and running it'll be easier to improve on.
This is my first post here, hope it is easily readable and also my answer is asked! (:
First of all, to put you a bit into perspective, I wanted to create an efficiency calculator for the brand new game No Man's Sky. The economy in that game is pretty much the same as in real life. You can sell items at a much higher price than the components needed to create it. E.g: You can sell a rock for 5000€ or 30 rock parts for 30€ each. If you craft the rock with the rock parts your profit will be 5000-900, right? (:
Here is the code.
What I want it to do is the following. User enters a product, the program compares the price of the product if sold and the price of the components to craft the product and shows you the profit doing so.
I have the following questions about it:
Is there a better way to save the data to use it after? (lines 1-16)
Is there any way to compare all variables I will create (line 18) or do I have to create an if loop for every product (lines 22-24). What I mean is something like
profit = products[input] - input_recipe
print profit
Since I want to check a lot of recipes, it would be a pain in the ass if there's a better for to do it.
How you save the data and access it will really depend on how you want to handle your calculator. I would say the best way to handle this would be if there were an excel file or JSON file or something of the sort that is all inclusive of all materials and items of the game (you may have to be the one to make this or someone else may already have). In the event you have to put the list together yourself, it could be a long process and very annoying, so try to find a list somewhere you can download then open the file and parse the data as needed. You could put all the data in the code itself but that doesn't allow you to write code against the data with say a different language if you so desired.
As far as loops are concerned, I'm not sure what you mean by that? You have dictionaries for your data so there's no need to loop over every value right? Now if you are referring to taking in multiple user inputs, a loop wouldn't be a bad idea for command line:
continue_calculations = 'y'
while continue_calculations != 'n':
# Do your logic here.
continue_calculations = raw_input('Would you like to continue(y/n)?')
Of course if you are making a calculator you could look into GUI development, or web development if you want to make it into a site. PyQT is a handy little module to work in and there are some good tutorials for that: https://pythonprogramming.net/basic-gui-pyqt-tutorial/
Cheers,
About your first question, another way would be to use json format to store your data and not to use three separate dictionaries, I mean something like:
data = {"elements":{"Th":20.6,"Pu":41.3},"alloys":{"Aronium":1546.9,"Herox":2877.5},"Products":{"Antimatter":5232,"Warp Cell":46750}}
You could parse for example "Th" price by writing:
th_price = data['elements']['Th']
As for your second question you could create a fourth dictionary that would contain the prices of all the possible recipes which of course you have predefined - just not to compute them every time you need them and to have them available for fast parsing. So you would write something like:
profit = products[input] - input_recipe[input]
print profit
where input_recipe would be your fourth dictionary with the recipe prices.
Been studying Python on my own for some months. I'm about to ventur into the field of databases. I am currently aiming to create a very small scale application which would feature retrieving data based on a simple search (keyword) and reflect static data linked to it. Some numbers to put it in perspective:
About 150-200 "key" values
About 5-10 values to be displayed per "key" value
Editable (although the info tends to remain same most of time - maybe 1 or 2 amendments/month in total on all data stored)
An example would be the cards you see when you do a simple google search. For example you search for an actor (key value) and your query generates a "card" with values (age, length, wage, brothers,...).
As it would be the first attempt on creating something which works with such a (for me personally) larger amount of data, I am a bit puzzled with the options available to me. I have been reading up on different database models (relational,..). Ultimately I came to below 3 possibilities:
XML
database
hardcode
I intend on making the data amendable by 1 privileged user. This alone makes hardcoding it not really an option. Even though, as I am a novice, I could be interpreting this wrong here.
I'd be happy if you could point me in the right direction and if you'd go for a database, which one you'd recommend going for (MySQL,..).
Many thanks in advance. If I made any error with the post (as it is my initial post), do not hesitate to point this out.
So this snippet practically explains what I want to do
bloc_results = bloc_results.annotate(num_mates=Count(profilebloc__profile__in=probable_mates)).order_by("-num_mates")
I know this is not possible but it was the easiest way I found to explain the situation. the variable probable_mates is a list filled with the pk's of a user table. So I have to get that number and order by it. That's no problem using filter. The thing is that using filter only retrieves the objects that match the id's on that probable_mates list, not all of them with the ones with the highest number of matches up at the top which is what I need to get. How can I do that with the Django ORM without going raw SQL? Thanks!
EDIT: I'll clarify a bit by explaining what this is doing: There are a bunch of clubs that will be added to this webapp and I need sorting by two different parameters. One is by the number of members of each club which is pretty easy. The second one if by "Club friends" so that all of the clubs are ordered by the number of common friends they have with the logged in user even if that user hasn't joined that club. Also that is not so hard. The problem is that when I get that queryset and order it it only displays the clubs that have club friends in common, not the rest. I need the ones that have club friends at the top, ordered by number of mutual club friends and the rest at the bottom, even if they don't have club friends in common.
Like akonsu said in his comment, I'm not exactly sure what you are trying to accomplish. That said, this sounds similar to this question I just answered. Adapted to your model names, does the following do what you want?
matches = bloc_results.objects.filter(profilebloc__profile__in=probable_mates)
matches = matches.annotate(Count('num_mates'))
results = matches.order_by('-num_mates')
If not, I think you need to clarify your question a bit.
I think you need to approach this from your profilebloc model. Count mates using that model, and then filter the results to leave only profile blocs that are in your bloc_results. Sorry, without your models I cannot be clearer.
This was a pretty badly explained question but I solved it by using the | operator to concatenate both querysets like this:
new_queryset = one_queryset | second_queryset
Did not have to use lists after all.
In Django how can i return the total number of items (count) that are related to another model, e.g the way stackoverflow does a list of questions then on the side it shows the count on the answers related to that question.
This is easy if i get the questionid, i can return all answers related to that question but when am displaying the entire list of question it becomes a bit tricky to display on the side the count showing the total count.
I don't know if am clear but just think how stackoverflow displays its questions with answer,views count next to each question!
QuerySet.count()
See also an example how to build QuerySets of related models.
If you're willing to use trunk, you can take advantage of the brand new annotate() QuerySet method added just a week or so ago, which solves this exact problem:
http://docs.djangoproject.com/en/dev/topics/db/aggregation/
If you want to stick with Django 1.0, you can achieve this in a slightly less elegant way using the select argument of the extra() QuerySet method. There's an example of exactly what you are talking about using extra() here:
http://docs.djangoproject.com/en/dev/ref/models/querysets/#extra-select-none-where-none-params-none-tables-none-order-by-none-select-params-none
Finally, if you need this to be really high performance you can denormalise the count in to a separate column. I've got some examples of how to do this in the unit testing part of my presentation here:
http://www.slideshare.net/simon/advanced-django