takuti / flurs

:ocean: FluRS: A Python library for streaming recommendation algorithms
https://flurs.readthedocs.io/
MIT License
108 stars 17 forks source link

error : ValueError: Attempted relative import in non-package #2

Closed Sandy4321 closed 6 years ago

Sandy4321 commented 6 years ago

\Anaconda2\Lib\site-packages\flurs\base.py", line 1, in from .data.entity import User, Item

ValueError: Attempted relative import in non-package

pls help

Sandy4321 commented 6 years ago

I try to run test case, to learn how it works, do you have more examples?

Sandy4321 commented 6 years ago

Anaconda2\Lib\site-packages\flurs\evaluator.py", line 7, in from . import logger

ValueError: Attempted relative import in non-package

takuti commented 6 years ago

@Sandy4321 Thanks for report. Could you describe the detail of what you did? On my local Python 2 (anaconda2-4.3.1) environment, I cannot see any import-related problems:

$ pip --version
pip 9.0.1 from /path/to/home/.pyenv/versions/anaconda2-4.3.1/lib/python2.7/site-packages (python 2.7)
$ pip install flurs
$ python
Python 2.7.13 |Anaconda 4.3.1 (x86_64)| (default, Dec 20 2016, 23:05:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> from flurs.data.entity import User, Item, Event
>>> user = User(0)
>>> item = Item(0)
>>> event = Event(user, item)

And CI including unit tests on Python2 successfully passes: https://travis-ci.org/takuti/flurs

takuti commented 6 years ago

Sorry about poor examples; I would add more examples and better documentation in the future.

For now, you may refer to an introductory notebook and repository (experiment.py) which uses this package for the purpose of research experiments.

Sandy4321 commented 6 years ago

super , thanks I try to run this notebook, all runs perfect - no errors , but this lines plt.plot(accuracy) plt.xlabel('sample index (time)') plt.ylabel('recall (window size = 200)') give only straight line, may you help pls? pls see in attachment pfd file with notebook copy euroscipy_2017_jan22.pdf

takuti commented 6 years ago

Ah, the notebook assumes Python 3; if you are using Python 2, modify:

accuracy = []
for top_score, rank, recommend_time, update_time in evaluator.evaluate(events[300:]):
    window.append(int(rank < 10))  # consider top-10 recommendation
    accuracy.append(sum(window) / len(window))

to:

accuracy = []
for top_score, rank, recommend_time, update_time in evaluator.evaluate(events[300:]):
    window.append(int(rank < 10))  # consider top-10 recommendation
    accuracy.append(sum(window) / float(len(window)))

Even though accuracy should be float, sum(window) / len(window) rounds the value to integer in Python 2.

Sandy4321 commented 6 years ago

super thanks , will try by the way this line P[user], Q[user] = next_p, next_q should it be P[user], Q[item] = next_p, next_q since matrix Q is matrix of items? thanks

Sandy4321 commented 6 years ago

by the way , may you share this paper Sketching Dynamic Interactions for Online Item Recommendation "User Item" filetype:pdf pls?

Sandy4321 commented 6 years ago

my email sndr.stpnv@gmail.com thanks

takuti commented 6 years ago

You are right; Q is an item matrix.

Sent the paper to your email.

Sandy4321 commented 6 years ago

regarding to what was written above the problem is when I run file evaluator.py from flurs package there is error in line from . import logger Anaconda2\Lib\site-packages\flurs\evaluator.py", line 7, in from . import logger

ValueError: Attempted relative import in non-package

Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved.

D:\Factorisatoin_Machines\code\flurs_master_Jan21>pip --version pip 9.0.1 from c:\users\sanderoct27\anaconda2\lib\site-packages (python 2.7)

D:\Factorisatoin_Machines\code\flurs_master_Jan21>pip install flurs Requirement already satisfied: flurs in c:\users\sanderoct27\anaconda2\lib\site- packages Requirement already satisfied: scikit-learn in c:\users\sanderoct27\anaconda2\li b\site-packages (from flurs) Requirement already satisfied: numpy in c:\users\sanderoct27\anaconda2\lib\site- packages (from flurs) Requirement already satisfied: mmh3 in c:\users\sanderoct27\anaconda2\lib\site-p ackages (from flurs) Requirement already satisfied: scipy in c:\users\sanderoct27\anaconda2\lib\site- packages (from flurs)

D:\Factorisatoin_Machines\code\flurs_master_Jan21>python Python 2.7.12 |Continuum Analytics, Inc.| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://anaconda.org

hopefully now I put enough details to explain by the way how to run flurs for python 3 ?

takuti commented 6 years ago

when I run file evaluator.py from flurs package

How did you call the module? Could you provide code you executed?

As the notebook suggests, even in Python 2, I have confirmed that evaluator can be successfully loaded as:

from flurs.evaluator import Evaluator
evaluator = Evaluator(recommender)
...

For Python 3, there is no difference; currently, FluRS supports both Python 2.7 and 3.4+ as much as I can, and hence you can use this library in a similar way to what you are now doing in Python 2.

Sandy4321 commented 6 years ago

hello problem not in calling Evaluator as you describe from flurs.evaluator import Evaluator evaluator = Evaluator(recommender) work without error but if I use this file evaluator.py stand alone , then this line from . import logger the error message is Anaconda2\Lib\site-packages\flurs\evaluator.py", line 7, in from . import logger

may I just can not run this file evaluator.py but should call it from another code

takuti commented 6 years ago

In fact, since FluRS is standard python library, such usage is not expected.

But, if you remove from . import logger and logger.xxx lines from evaluation.py as workaround, your problem should be resolved; the error is just related to logging code, so there is no negative effect on the behavior of evaluatior.py.

Sandy4321 commented 6 years ago

I see thanks a lot, i am going to learn how it works , do you have simple example like https://github.com/takuti/stream-recommender/tree/master/config but only not for streamed data, but for static data. Meaning to read data from LastFM or ML1M and to get recommendation and some score how good it works? and compare for example to svd? Thanks a lot for help

Sandy4321 commented 6 years ago

as example to compare with http://surpriselib.com/

takuti commented 6 years ago

flurs/examples/movielens1m.py provides an example of how to use this package, and, if you are interested in non-streamed recommendation, you simply need to call evaluator.fit for all batch samples; that is,

stream:

    evaluator = Evaluator(rec, data.can_repeat)

    n_batch_train = int(data.n_sample * 0.2)  # 20% for pre-training to avoid cold-start
    n_batch_test = int(data.n_sample * 0.1)  # 10% for evaluation of pre-training
    batch_tail = n_batch_train + n_batch_test

    # pre-train
    # 20% for batch training | 10% for batch evaluate
    # after the batch training, 10% samples are used for incremental updating
    logging.info('batch pre-training before streaming input')
    evaluator.fit(
        data.samples[:n_batch_train],
        data.samples[n_batch_train:batch_tail],
        n_epoch=1  # single pass even for batch training
    )

    # 70% incremental evaluation and updating
    logging.info('incrementally predict, evaluate and update the recommender')
    res = evaluator.evaluate(data.samples[batch_tail:])

batch:

    evaluator = Evaluator(rec, data.can_repeat)

    n_batch_train = int(data.n_sample * 0.8)  # 80% for training

    # batch-train for all samples
    # 80% for batch training | 20% for batch evaluate
    logging.info('batch pre-training before streaming input')
    evaluator.fit(
        data.samples[:n_batch_train],
        data.samples[n_batch_train:],
        n_epoch=100  # set number of iterations as you want
    )

In my Factorization Machines paper, evaluation has been conducted for both incremental (i.e., stream) and static (i.e., batch) model. You can also refer to the methodology.

takuti commented 6 years ago

The topic above is now different from the original one error : ValueError: Attempted relative import in non-package, so I close this issue for now. If you have further questions, open another issue.