name 'FillMissing' is not defined - python

When I run following code:
df = pd.read_csv('../input/marketingrar/marketing.csv')
df.head()
dep_var = 'Revenue'
cat_names = ['Day_Name','Promo']
cont_names = ['Date','Week','Month','Month_ID','Year','Visitors','Marketing Spend']
procs = [FillMissing, Categorify, Normalize]
I got this error bellow:
NameError Traceback (most recent call
last) in
----> 1 procs = [FillMissing, Categorify, Normalize]
NameError: name 'FillMissing' is not defined
P.S. I'm using Kaggle notebook. Why this error occurs and how to solve it?

from fastai.tabular.all import *
is the only working solution for me

With this code, you are trying to initiate a list named procs with the 3 references to FillMissing, Categorify and Normalise, but you never created those references before.
Did you maybe want to create a list of 3 strings? Then you forgot the '', compare the other lists like cat_names or cont_names
Maybe it could also help to include
from fastai import *
from fastai.tabular import *

Related

why Object is not callable at the second time of execution?

I am trying to use run the following code. The code generates output for the first time, however during the 2nd execution returns an error -- TypeError: 'YahooSecurity' object is not callable
I am not sure why this is happening. I am using the following python package https://pypi.org/project/pypf/
My code is -
import os
import time
from pypf.chart import PFChart
from pypf.instrument import YahooSecurity
symbol = "TATAPOWER","TATAMOTORS"
for i in symbol:
print(i)
YahooSecurity = YahooSecurity(i, True, True, '1d',1)
print(type(YahooSecurity))
chart = PFChart(YahooSecurity,0.01,1,'C',3,True,True,True)
chart.create_chart(dump=True)
time.sleep(20)
"TATAPOWER","TATAMOTORS" are csv file with data
Error -
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [38], line 9
7 for i in symbol:
8 print(i)
----> 9 YahooSecurity = YahooSecurity(i, True, True, '1d',1)
10 print(type(YahooSecurity))
11 chart = PFChart(YahooSecurity,0.01,1,'C',3,True,True,True)
TypeError: 'YahooSecurity' object is not callable
I am trying to get multiple charts in one go.
because YahooSecurity = YahooSecurity(i, True, True, '1d',1) replaces the object with the result of calling the object (or, more likely, the class). So, the second time you use it, it's not a class anymore. Just make it somethin like yahoo_security = YahooSecurity(i, True, True, '1d',1)

How to get a list of all tokens from Lucene 8.6.1 index using PyLucene?

I have got some direction from this question. I first make the index like below.
import lucene
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.index import IndexWriterConfig, IndexWriter, DirectoryReader
from org.apache.lucene.store import SimpleFSDirectory
from java.nio.file import Paths
from org.apache.lucene.document import Document, Field, TextField
from org.apache.lucene.util import BytesRefIterator
index_path = "./index"
lucene.initVM()
analyzer = StandardAnalyzer()
config = IndexWriterConfig(analyzer)
if len(os.listdir(index_path))>0:
config.setOpenMode(IndexWriterConfig.OpenMode.APPEND)
store = SimpleFSDirectory(Paths.get(index_path))
writer = IndexWriter(store, config)
doc = Document()
doc.add(Field("docid", "1", TextField.TYPE_STORED))
doc.add(Field("title", "qwe rty", TextField.TYPE_STORED))
doc.add(Field("description", "uio pas", TextField.TYPE_STORED))
writer.addDocument(doc)
writer.close()
store.close()
I then try to get all the terms in the index for one field like below.
store = SimpleFSDirectory(Paths.get(index_path))
reader = DirectoryReader.open(store)
Attempt 1: trying to use the next() as used in this question which seems to be a method of BytesRefIterator implemented by TermsEnum.
for lrc in reader.leaves():
terms = lrc.reader().terms('title')
terms_enum = terms.iterator()
while terms_enum.next():
term = terms_enum.term()
print(term.utf8ToString())
However, I can't seem to be able to access that next() method.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-47-6515079843a0> in <module>
2 terms = lrc.reader().terms('title')
3 terms_enum = terms.iterator()
----> 4 while terms_enum.next():
5 term = terms_enum.term()
6 print(term.utf8ToString())
AttributeError: 'TermsEnum' object has no attribute 'next'
Attempt 2: trying to change the while loop as suggested in the comments of this question.
while next(terms_enum):
term = terms_enum.term()
print(term.utf8ToString())
However, it seems TermsEnum is not understood to be an iterator by Python.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-48-d490ad78fb1c> in <module>
2 terms = lrc.reader().terms('title')
3 terms_enum = terms.iterator()
----> 4 while next(terms_enum):
5 term = terms_enum.term()
6 print(term.utf8ToString())
TypeError: 'TermsEnum' object is not an iterator
I am aware that my question can be answered as suggested in this question. Then I guess my question really is, how do I get all the terms in TermsEnum?
I found that the below works from here and from test_FieldEnumeration() in the test_Pylucene.py file which is in pylucene-8.6.1/test3/.
for term in BytesRefIterator.cast_(terms_enum):
print(term.utf8ToString())
Happy to accept an answer that has more explanation than this.

'list' object is not callable when building one-hot vector

I was trying to construct a one-hot encoding system by using Python 3.7 + Jupyter notebook, and I ran into an error like this:
TypeError Traceback (most recent call last)
<ipython-input-87-0fc633482cfd> in <module>
1 for i, sample in enumerate(samples):
----> 2 for j, word in list(enumerate(sample.split()))[:max_length]:
3 index = all_token_index.get(word)
4 result[i,j,index] = 1
TypeError: 'list' object is not callable
Here are the original code:
import numpy as np
samples = ['This is a dog','This is a cat']
all_token_index = {}
for sample in samples:
for word in sample.split():
if word not in all_token_index:
all_token_index[word] = len(all_token_index) + 1
max_length = 10
result = np.zeros(shape=(len(samples), max_length, max(all_token_index.values()) + 1 ))
for i, sample in enumerate(samples):
# Get keys and values in sample
for j, word in list(enumerate(sample.split()))[:max_length]:
index = all_token_index.get(word)
result[i,j,index] = 1
Could you please help me? Thanks in advance!
The correct answer was given by #Samwise and #Ronald in the comments. Like they said, I used a list variable in one deleted notebook cell. After restarting the notebook, the problem was fixed.

KeyError in Python, even though the key exists

I have been scratching my head on this for a few days now and cannot seem to find a solution that works online for my problem. I am trying to access data on zendesk and go through the pagination. For some reason, I am getting a KeyError, even though I can see that the key does exist. Here is my code :
data_users2 = [[]]
while url_users:
users_pagination = requests.get(url_users,auth=(user, pwd))
data_user_page = json.loads(users_pagination.text)
print (data_user_page.keys())
for user in data_user_page['users']:
data_users2.append(user)
url = data_user_page['next_page']
Here is the output :
dict_keys(['users', 'next_page', 'previous_page', 'count'])
dict_keys(['error'])
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-22-fab95d95ddeb> in <module>
6 data_user_page = json.loads(users_pagination.text)
7 print (data_user_page.keys())
----> 8 for user in data_user_page["users"]:
9 data_users2.append(user)
10 url = data_user_page["next_page"]
KeyError: 'users'
As you can see, users does exist. same thing happens if I try to print the next_page, I get a KeyError for next_page.
Any help would be appreciated ! Thanks!
Your code is failing in its second iteration of the loop, in that moment your keys in data_user_page are just "error" as you can see in the output you have pasted
dict_keys(['users', 'next_page', 'previous_page', 'count']) <----- FIRST ITERATION
dict_keys(['error']) <---- SECOND ITERATION, THEREFORE, YOUR KEY DOES NOT EXISTS
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-22-fab95d95ddeb> in <module>
6 data_user_page = json.loads(users_pagination.text)
7 print (data_user_page.keys())
----> 8 for user in data_user_page["users"]:
9 data_users2.append(user)
10 url = data_user_page["next_page"]
KeyError: 'users'
EDIT: This could be due to the fact that you are saving the next url in a variable called url not url_users

basic python query. NameError in __init__ method

NameError: name 'the_shape' is not defined
I get the following error when I try to import my KMeans module containing the class named KMeansClass. The KMeans.py module has the following structure:
import numpy as np
import scipy
class KMeansClass:
#takes in an npArray like object
def __init__(self,dataset,the_shape=5):
self.dataset=dataset
self.mu = np.empty(shape=the_shape)
and in ipython when I try
import KMeans
I get the NameError: name 'the_shape' is not defined
I am really new to python OOP and don't know why this is happening as all I'm doing is passing arguments to init and assigning those arguments to instance variables.
Any help would be appreciated.
Thanks in advance!
Full Traceback:
NameError Traceback (most recent call last)
<ipython-input-2-44169aae5584> in <module>()
----> 1 import kmeans
/Users/path to file/kmeans.py in <module>()
1 import numpy as np
2 import scipy
----> 3 class KMeansClass:
4 #takes in an npArray like object
5 def __init__(self,dataset,the_shape=5):
/Users/path_to_file/kmeans.py in KMeansClass()
5 def __init__(self,dataset,the_shape=5):
6 self.dataset=dataset
----> 7 self.mu = np.empty(shape=the_shape)
8
9
NameError: name 'the_shape' is not defined

Categories