ERROR TypeError: __init__() takes 2 positional arguments but 3 were given - error-handling

Request help in following code
model = LinearRegression()
rfe = RFE(model, 3)
X_rfe = rfe.fit_transform(X,y)
model.fit(X_rfe,y)
print(rfe.support_)
print(rfe.support_)

Put: n_features_to_select = 3
That worked for me.

Related

Initialise tensors with ones in tensorflow 1

I want to initialise a variable declared using get_variable function with 1s .
I tried the following methods :
1.tf.get_variable(name = 'yd1', shape = shape_t, dtype = tf.float32,initializer = tf.ones())
Error received -> TypeError: ones() takes at least 1 argument (0 given)
tf.get_variable(name = 'yd1', shape = shape_t ,dtype = tf.float32,initializer = tf.ones(shape=shape_t))
Error received -> ValueError("If initializer is a constant, do not specify shape.")
What is the best way to initialise a variable with ones?
tf.zeros_initializer can be used to initialise with 0s, but there is no equivalent for ones in tf 1
You need to use tf.ones_initializer:
tf.get_variable(name='yd1', shape=shape_t, dtype=tf.float32,
initializer=tf.ones_initializer())
Alternatively, as the second error message says, you can use a constant value, but then do not pass a shape:
tf.get_variable(name='yd1', initializer=tf.ones(shape=shape_t, dtype=tf.float32))
tf.ones_initializer is not available in tf 1, It was introduced in tf 2.
The following code does the work
tf.get_variable(name = 'yd1', shape = shape_t ,dtype = tf.float32,initializer = tf.constant_initializer(1))

"AttributeError:" After converting script to TF2

I used to the automatic update script but am still running into some issues. This area seems to be the part which is causing the issues, any help would be appreciated.
Error issued:" AttributeError: 'BatchDataset' object has no attribute 'output_types' "
# network parameters
n_hidden_1 = 50
n_hidden_2 = 25
ds_train = tf.data.Dataset.from_tensor_slices((X_placeholder, Y_placeholder)).shuffle(buffer_size=round(len(X_train) * 0.3)).batch(batch_size_placeholder)
ds_test = tf.data.Dataset.from_tensor_slices((X_placeholder, Y_placeholder)).batch(batch_size_placeholder)
ds_iter = tf.compat.v1.data.make_one_shot_iterator(ds_train.output_types, ds_train.output_shapes)
next_x, next_y = ds_iter.get_next()
train_init_op = ds_iter.make_initializer(ds_train)
test_init_op = ds_iter.make_initializer(ds_test)
Replace:
ds_train.output_types
with:
tf.compat.v1.data.get_output_types(ds_train)
similarly you may need tf.compat.v1.data.get_output_shapes(ds_train)

Scrapy : TypeError: argument of type 'NoneType' is not iterable

Whit scrapy, I receive this NoneType error when I launch my spider:
if 'Jockey' in tab_arrivee_th: TypeError: argument of type 'NoneType'
is not iterable
The code works fine in the console test with a list, but not with the response.css.
I think the problem comes from the response_arrivee_th, and I don't understand why, because the 'scrapy shell' gives me a list in return, and it's the same that I use in the test.
def parse(self, response):
tab_arrivee_th = response.css('.arrivees th::text').extract()
# list obtained whit the response.css from above in scrapy shell
# tab_arrivee_th = ['Cl.', 'N°', 'Cheval', 'S/A', 'Œill.', 'Poids', 'Corde', 'Ecart', 'Jockey', 'Entraîneur', 'Tx', 'Récl.', 'Rapp. Ouv.']
if 'Jockey' in tab_arrivee_th:
col_jockey = tab_arrivee_th.index('Jockey') + 1
elif 'Driver' in tab_arrivee_th:
col_jockey = tab_arrivee_th.index('Driver') + 1
else:
col_jockey = 0
jockey = partant.css('td:nth-child(' + str(col_jockey) + ') > a::text').extract()
if 'Jockey' in tab_arrivee_th: TypeError: argument of type 'NoneType'
is not iterable
thx for the help
Solved : the 'response.css('.arrivees th::text').extract()' point to a list construct in js.
So I used scrapy-splash to have a 0.5 second delay. And it works fine.
the response for this line tab_arrivee_th = response.css('.arrivees th::text').extract() is empty , check the response again.

pyautogui.center, TypeError: 'NoneType' object is not subsriptable

I'm trying to code a program to take care of some boring stuff. When I try to use pyautogui.center() I get an error. Here is an example of the code and the error:
c = pyautogui.locateOnScreen('sample.png')
d = pyautogui.center((c))
d = pyautogui.center((c))
File "C:\Users\\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyscreeze\__init__.py",
line 404, in center
return (coords[0] + int(coords[2] / 2), coords[1] + int(coords[3] / 2)) TypeError: 'NoneType' object is not subscriptable
This could be because of image is not found on screen too. But try this
sample_image = pyautogui.locateAllOnScreen('sample.png')
center_of_image = pyautogui.center(sample_image)
pyautogui.mouseDown(center[0], center[1])
pyautogui.mouseUp(center[0], center[1])

How to use tf.check_numerics

I am trying to use tf.check_numerics (TensorFlow 1.2) to prevent NaN in my gradients, inspired by this SO post. My code is:
optimizer = tf.train.RMSPropOptimizer(learning_rate, decay=0.99)
grads_and_vars = optimizer.compute_gradients(graph.loss_total)
grads, variables = zip(*grads_and_vars)
clipped_gradients, _ = (tf.clip_by_global_norm(grads, 1.))
grad_check = tf.check_numerics(clipped_gradients, 'check_numerics caught bad gradients')
# ^ this line causes an error
with tf.control_dependencies([grad_check]):
graph.train_op = optimizer.apply_gradients(zip(clipped_gradients, variables))
But I get the error message:
ValueError: Tried to convert 'tensor' to a tensor and failed. Error: Shapes must be equal rank, but are 2 and 1
From merging shape 2 with other shapes. for 'training/CheckNumerics/packed' (op: 'Pack') with input shapes: [4,16], [16], [16,2], [2].
What am I doing wrong, and how do I fix it?
clipped_gradients is a list. Try
grad_check = tf.check_numerics(clipped_gradients[0], 'check_numerics caught bad gradients')