Comments (12)
Hi,
The method is accessed with surprise.Dataset.load_from_df
, with an upper-case 'D'.
Nicolas
from surprise.
Thank you for the swift response. This allowed me to call the function.
I am now having a follow-up error I am not sure how to tackle. For context, I am loading a pandas df through the load_from_df function:
reader = Reader(line_format='user item rating', rating_scale=(1, 10))
data = Dataset.load_from_df(anime_rating[['user_id', 'anime_id', 'rating']], reader)
But when in evaluating KNNWithMeans's performance:
sim_options = {'name': 'pearson', 'user_based': False}
algo = KNNWithMeans(k=20, min_k=11, sim_options=sim_options)
perf = evaluate(algo, data, measures=['RMSE','MAE'])
I get this error:
AttributeError: 'int' object has no attribute 'sqrt'
Sorry to bother you again!
from surprise.
Hi,
Please give me your whole code and the details of the error message, because here I cannot know where the error is happening
Nicolas
from surprise.
Here's the code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from surprise import KNNWithMeans
from surprise import Dataset
from surprise import evaluate, print_perf
from surprise import Reader
anime_rating = pd.read_csv('rating.csv') # the data
anime_rating = anime_rating[anime_rating.rating > 0] # keep ratings between 1-10
anime_rating['user_anime_count'] = anime_rating.groupby('user_id')['user_id'].transform(np.count_nonzero)
anime_rating = anime_rating[anime_rating.user_anime_count > 19] # keep users that have seen at least 20 anime
anime_rating = anime_rating.drop('user_anime_count', axis=1) # having served its purpose, drop the user count column
#loading from pandas dataframe
reader = Reader(line_format='user item rating', rating_scale=(1, 10))
data = Dataset.load_from_df(anime_rating[['user_id', 'anime_id', 'rating']], reader)
#testing the knnwithmeans
sim_options = {'name': 'pearson', # compute Pearson correlation coefficient between all pairs of items
'user_based': False # compute similarities between items
}
algo = KNNWithMeans(k=20, min_k=11, sim_options=sim_options)
perf = evaluate(algo, data, measures=['RMSE','MAE'])
And here's the error:
Evaluating RMSE, MAE of algorithm KNNWithMeans.
------------
Fold 1
Computing the pearson similarity matrix...
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-017eb59430bf> in <module>()
3 }
4 algo = KNNWithMeans(k=20, min_k=11, sim_options=sim_options)
----> 5 perf = evaluate(algo, data, measures=['RMSE','MAE'])
c:\users\mike\appdata\local\programs\python\python35\lib\site-packages\surprise\evaluate.py in evaluate(algo, data, measures, with_dump, dump_dir, verbose)
63
64 # train and test algorithm. Keep all rating predictions in a list
---> 65 algo.train(trainset)
66 predictions = algo.test(testset, verbose=(verbose == 2))
67
c:\users\mike\appdata\local\programs\python\python35\lib\site-packages\surprise\prediction_algorithms\knns.py in train(self, trainset)
166
167 SymmetricAlgo.train(self, trainset)
--> 168 self.sim = self.compute_similarities()
169
170 self.means = np.zeros(self.n_x)
c:\users\mike\appdata\local\programs\python\python35\lib\site-packages\surprise\prediction_algorithms\algo_base.py in compute_similarities(self)
229 try:
230 print('Computing the {0} similarity matrix...'.format(name))
--> 231 sim = construction_func[name](*args)
232 print('Done computing similarity matrix.')
233 return sim
c:\users\mike\appdata\local\programs\python\python35\lib\site-packages\surprise\similarities.pyx in surprise.similarities.pearson (surprise/similarities.c:6558)()
AttributeError: 'int' object has no attribute 'sqrt'
from surprise.
If it helps, here's the first 10 columns of the dataframe before using load_from_df:
user_id anime_id rating
156 3 20 8
157 3 154 6
158 3 170 9
159 3 199 10
160 3 225 9
161 3 341 6
162 3 430 7
163 3 527 7
164 3 552 7
165 3 813 10
from surprise.
There are four columns in your example, is this normal?
Anyway the issue looks very weird and I don't see an obvious solution. I'll come back to it tomorrow (it's almost 11pm here).
As a side note, you can see that I formatted your previous post: please consider doing it next time, it really helps reading ;)
Nicolas
from surprise.
I think the first column is the index, and
anime_rating.shape
returns
(6164987, 3)
Sorry for not formatting earlier.
Mayank
from surprise.
Actually, just ran the evaluation with msd instead of pearson, and discovered no issue. Cosine returned the following while computing the similarity matrix:
...\surprise\prediction_algorithms\algo_base.py:231: RuntimeWarning: invalid value encountered in sqrt sim = construction_func[name](*args)
from surprise.
Looks like you always have a problem with np.sqrt
... Really special.
Can you try a new clean installation and see how it goes?
- install numpy
- install cython
- then
pip install https://github.com/NicolasHug/Surprise/archive/master.zip
from surprise.
I cannot reproduce the issue on my side. It works well with MSD, Pearson and Cosine.
Here is my code (I used the Kaggle dataset as you did, right?)
import surprise
import pandas as pd
df = pd.DataFrame.from_csv('rating.csv')
df.reset_index(level=0, inplace=True) # so that columns are user_id, anime_id and rating
df = df[df.rating>0]
# I'm only loading the first 1000 rows to save time
reader = surprise.dataset.Reader(rating_scale=(1, 10))
data = surprise.Dataset.load_from_df(df.head(1000)[['user_id', 'anime_id', 'rating']], reader)
sim_options = {'name': 'cosine', 'user_based': False}
algo = surprise.KNNWithMeans(k=20, min_k=11, sim_options=sim_options)
surprise.evaluate(algo, data, measures=['RMSE', 'MAE'])
Can you try that (on a clean environment, see previous post) and see how it goes?
Nicolas
from surprise.
I actually ran it in a clean environment last night, and it all worked out.
Thank you :)
from surprise.
Cool!
Very weird problem ^^
from surprise.
Related Issues (20)
- build_anti_testset() takes along time and at the end it doesnot work HOT 2
- question - surprise for implicit rating data? HOT 1
- Can Surprise work with PySpark?
- What to do if the dataset I want to read has more than three parameters HOT 1
- A bug when importing data from DataFrame HOT 2
- How do I apply ALS minimization in SVD? HOT 1
- Error: Sample larger than population or is negative? HOT 1
- Issues with running Suprise on M1 mac HOT 1
- trainset do not recommend new products
- Cross-validation kNN wrong results on custom dataset
- Possible memory leakage in SVDpp HOT 1
- GridSearchCV always recommends the first parameter combination as best HOT 2
- Wrong mapping of the raw IDs to the internal IDs
- How to remove NumPy installation in setup.py HOT 5
- Couldn't install Surprise in windows HOT 6
- No timestamp data in trainset HOT 1
- Couldn't install Surprise HOT 6
- How to do kfold crossvalidation on trainset (eg splitting movielens-100k using u1 split. then kfold crossvalidation on u1.base, test on u1.test) HOT 1
- Unexpected RMSE Differences in SVD Models with almost the same Training Data
- Compatibility with Python 3.12 HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from surprise.