Can I know how to fix this problem for imbalanced datasets for both NearMiss and SMOTETomek - data-science

TypeError Traceback (most recent call last)
Input In [65], in <cell line: 1>()
----> 1 X_res,y_res = nm.fit(X,y)
TypeError: cannot unpack non-iterable NearMiss object

Related

How to fix an attribute error on a saved model

I have my model saved as a numpy array. I am trying to pickle the model to run predictions on a new dataset. However I keep running into an attribute error.
model = y_pred_binary
filename = 'transfer_prediction_model.pkl'
pickle.dump(model, open(filename, 'wb'))
ickled_model = pickle.load(open(filename, 'rb'))
df = pd.read_csv(f"transfer_students_prediction_dataset.csv")
predictions = pickled_model.predict(df.drop('on_time_graduation', axis = 1))
print(predictions)
AttributeError Traceback (most recent call last)
Input In [24], in <cell line: 3>()
`1 pickled_model = pickle.load(open(filename, 'rb'))
2 df = pd.read_csv(f"transfer_students_prediction_dataset.csv")
----> 3 predictions = pickled_model.predict(df.drop('on_time_graduation', axis = 1))
4 print(predictions)`
AttributeError: 'numpy.ndarray' object has no attribute 'predict'`
I have tried using other methods such as joblib or fitting the model using sklearn but I still run into the same problem.

How to solve this TensorFlow 'NameError: name 'tf' is not defined' error?

I tried to change the value of cap = cv2.VideoCapture(0) from 0 t0 3 and so on, so that I can try to access my external webcam, but I am unable. Following is the error I am facing in Jupyter notebook
NameError Traceback (most recent call last)
C:\Users\MIRZAT~1\AppData\Local\Temp/ipykernel_14320/2909626117.py in <module>
16 # np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)
17
---> 18 input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 3), dtype=tf.float32)
19 detections, predictions_dict, shapes = detect_fn(input_tensor)
20
NameError: name 'tf' is not defined

Google colab shows error in "tutorial_deep_learning_basics.ipynb"

AttributeError Traceback (most recent call last)
in ()
5 print('.', end='')
6
----> 7 model = build_model()
8
9 early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=50)
in build_model()
5 ])
6
----> 7 model.compile(optimizer=tf.train.AdamOptimizer(),
8 loss='mse',
9 metrics=['mae', 'mse'])
AttributeError: module 'tensorflow._api.v2.train' has no attribute 'AdamOptimizer'
can you help resolve this?
Replace tf.train.AdamOptimizer() with tf.optimizers.Adam()

How to concatenate ndrarray with different dimensions(shape)?

I have two data which type is ndarray.
I want to merge X_train and Y_train, but they are have different shape.
I try to use concatenate, stack but it show me same error.
print(X_train.shape)
print(type(X_train[1]))
print(Y_train.shape)
print(type(Y_train[1]))
(17731, 30, 14, 1)
<class 'numpy.ndarray'>
(17731, 1)
<class 'numpy.ndarray'>
x_train_tt = np.stack([X_train , Y_train])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-43-74b8e20eca7c> in <module>
----> 3 x_train_tt = np.stack([X_train , Y_train])
<__array_function__ internals> in stack(*args, **kwargs)
~\anaconda3\envs\newenvironment2.3.6\lib\site-packages\numpy\core\shape_base.py in stack(arrays, axis, out)
424 shapes = {arr.shape for arr in arrays}
425 if len(shapes) != 1:
--> 426 raise ValueError('all input arrays must have the same shape')
427
428 result_ndim = arrays[0].ndim + 1
ValueError: all input arrays must have the same shape
Is there any solution?
I want to get shape like that
x_train_tt.shape
(17731,30,15,1)

Tensorflow,assign value to variable

I am total newbie to tensorflow, I am learning from
https://www.tensorflow.org/get_started/get_started
fixW = tf.assign(W, [-1.])
works fine,but
fixb = tf.assign(b, [1.])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/milenko/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py", line 272, in assign
return ref.assign(value)
AttributeError: 'Tensor' object has no attribute 'assign'
One other example
zero_tsr = tf.zeros([1.,2.])
zero_tsr
<tf.Tensor 'zeros:0' shape=(1, 2) dtype=float32>
If I try to change zero_tsr
fixz = tf.assign(zero_tsr, [2.,2.])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/milenko/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py", line 272, in assign
return ref.assign(value)
AttributeError: 'Tensor' object has no attribute 'assign'
Again,the same problem.
I have not changed shell,everything is the same.Why do I have problem here?
In the example you posted:
zero_tsr = tf.zeros([1.,2.])
zero_tsr
<tf.Tensor 'zeros:0' shape=(1, 2) dtype=float32>
zero_tsr is a constant and not a variable, so you cannot assign a value to it.
From the documentation:
assign(
ref,
value,
validate_shape=None,
use_locking=None,
name=None )
ref: A mutable Tensor. Should be from a Variable node. May be
uninitialized.
For example, this will work fine:
import tensorflow as tf
zero_tsr = tf.Variable([0,0])
tf.assign(zero_tsr,[4,5])
while this code will raise an error
import tensorflow as tf
zero_tsr = tf.zeros([1,2])
tf.assign(zero_tsr,[4,5])
The error that is raised is exactly the error you posted:
AttributeError: 'Tensor' object has no attribute 'assign'