Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'NoneType' object has no attribute 'name' #8

Open
gurvinderd opened this issue May 6, 2017 · 6 comments
Open

'NoneType' object has no attribute 'name' #8

gurvinderd opened this issue May 6, 2017 · 6 comments

Comments

@gurvinderd
Copy link

Everything is working at stage, except i get an error on recommendation page.
Line causing the error :
user_cluster_name =
User.objects.get(username=request.user.username).cluster_set.first().name

'NoneType' object has no attribute 'name'

Request Method: GET
Request URL: http://192.168.99.100:8000/reviews/recommendation/
Django Version: 1.11
Exception Type: AttributeError
Exception Value:
'NoneType' object has no attribute 'name'
Exception Location: /code/reviews/views.py in user_recommendation_list, line 92
Python Executable: /opt/conda/bin/python
Python Version: 3.6.0

@sandeep135
Copy link

same problem..please anybody provide the solution for this?

@Tikam02
Copy link

Tikam02 commented Mar 17, 2018

same problem

@Vijeta141
Copy link

This error is coming because User.objects.get(username=request.user.username).cluster_set.first() is returning null..
One of the reasons is because code in update_clusters method inside the update_step is not working.I manually changed the value of update_step so that the if condition is satisfied and it worked.`

def update_clusters():
    num_reviews = Review.objects.count()
    print (num_reviews)
    # update_step = ((num_reviews/100)+1) * 5
    update_step = 6
    print (update_step)
    if num_reviews % update_step == 0: # using some magic numbers here, sorry...
        # Create a sparse matrix from user reviews
        all_user_names = list(map(lambda x: x.username, User.objects.only("username")))
        all_wine_ids = set(map(lambda x: x.wine.id, Review.objects.only("wine")))
        num_users = len(all_user_names)
        ratings_m = dok_matrix((num_users, max(all_wine_ids)+1), dtype=np.float32)
        for i in range(num_users): # each user corresponds to a row, in the order of all_user_names
            user_reviews = Review.objects.filter(user_name=all_user_names[i])
            for user_review in user_reviews:
                ratings_m[i,user_review.wine.id] = user_review.rating

        # Perform kmeans clustering
        k = int(num_users / 10) + 2
        kmeans = KMeans(n_clusters=k)
        clustering = kmeans.fit(ratings_m.tocsr())

        # Update clusters
        Cluster.objects.all().delete()
        new_clusters = {i: Cluster(name=i) for i in range(k)}
        for cluster in new_clusters.values(): # clusters need to be saved before referring to users
            cluster.save()
        for i,cluster_label in enumerate(clustering.labels_):
            new_clusters[cluster_label].users.add(User.objects.get(username=all_user_names[i]))

        print (new_clusters)

@XiaoheLiu
Copy link

This problem occurs when you create a new user, but they do not belong to any cluster and could not get any recommendation.

In the suggestions.py, this line of code:

 if num_reviews % update_step == 0:

means that the cluster only updates when the num_reviews is some magic number, i.e. not every time. But we need to update the cluster every time when a new user is registered so that they can get recommendations based on their cluster.

I fix this issuer by changing the update_clusters() function to take a boolean "is_new_user" as argument, like this:

def update_clusters(is_new_user):
    num_reviews = Review.objects.count()
    update_step = 10
    # Recluster users when every <update_step> reviews are added or when a new user is created
    if num_reviews % update_step == 0 or is_new_user:
    ...

Then, in the views.py, for the recommendation view:

@login_required
def recommendation(request):
    ...
    try:
        user_cluster_name = User.objects.get(
            username=request.user.username).cluster_set.first().name
    except:
        # If this user does not belong to any cluster, update the clusters.
        update_clusters(is_new_user=True)
        user_cluster_name = User.objects.get(
            username=request.user.username).cluster_set.first().name

While in the add_review view:

def add_review(request):
   ...   
    update_clusters(is_new_user=False)
   ...

This way, it guarantees that when a freshly registered hit the recommendation page, the cluster will update and they will be put into some new cluster, but when users are adding reviews, the cluster only updates every 10 new reviews are added.

@venkat527
Copy link

This problem occurs when you create a new user, but they do not belong to any cluster and could not get any recommendation.
In the suggestions.py, this line of code:
if num_reviews % update_step == 0:
means that the cluster only updates when the num_reviews is some magic number, i.e. not every time. But we need to update the cluster every time when a new user is registered so that they can get recommendations based on their cluster.
I fix this issuer by changing the update_clusters() function to take a boolean "is_new_user" as argument, like this:
def update_clusters(is_new_user):
num_reviews = Review.objects.count()
update_step = 10
# Recluster users when every <update_step> reviews are added or when a new user is created
if num_reviews % update_step == 0 or is_new_user:
...
Then, in the views.py, for the recommendation view:
@login_required
def recommendation(request):
...
try:
user_cluster_name = User.objects.get(
username=request.user.username).cluster_set.first().name
except:
# If this user does not belong to any cluster, update the clusters.
update_clusters(is_new_user=True)
user_cluster_name = User.objects.get(
username=request.user.username).cluster_set.first().name
While in the add_review view:
def add_review(request):
...
update_clusters(is_new_user=False)
...
This way, it guarantees that when a freshly registered hit the recommendation page, the cluster will update and they will be put into some new cluster, but when users are adding reviews, the cluster only updates every 10 new reviews are added.

I tried this but I am getting this error- raise TypeError('Expected rank <=2 dense array or matrix.')
TypeError: Expected rank <=2 dense array or matrix.
suggestions.py line no:20

@XiaoheLiu
Copy link

@venkat527 I'm not sure why you get that error. I suspect it has something to do with the matrix manipulation with the dok_matrix, csr_matrix packages from scipy.sparse. My fix works for my own project though. You could compare with my version of recommendations.py. I changed and added a bunch of things on top of this original WineRama tutorial. Instead, I built a sword recommendation system... But the core recommendation algorithm is almost the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants