If by class weight you mean the dictionary to be used in model.fit the code below will return the class weight dictionary.
import numpy as np
def class_weight_calc(class_id_list, class_freq_list):
class_weight={}
total=0
for num in class_freq_list:
total += num
smallest=np.Inf
for klass, count in zip(class_id_list, class_freq_list):
class_weight[klass]=total/count
if class_weight[klass]<smallest:
smallest=class_weight[klass]
for c in class_id_list:
class_weight[c]=class_weight[c]/smallest
return class_weight
Note class_id_list is a list of your class indices. Class_freq_list is a corresponding list of how many samples there are for each class. For example if you have 3 classes the the class_id_list=[0,1,2]. If there are 10 samples for class 0, 20 samples for class 1 and 40 samples for class 2 then Class_freq_list=[10,20,40]. With these values the function would return a class_weight={0:4.0, 1:2.0, 2:1.0}