<jupyter_start><jupyter_text>*missing:Number_weeks_used*   > *64 unique values* train: *79858/88858*  test: *53417/59310*<jupyter_code>train['Crop_Damage'].value_counts().plot(kind='bar');
train['Crop_Damage'].value_counts()
plt.hist(train['Number_Weeks_Used']);
p=train['Number_Weeks_Used']+train['Number_Weeks_Quit']
p.value_counts().plot(kind='bar',figsize=(30,10));
nulltr= train[train['Number_Weeks_Used'].isnull()].index.tolist()
for i in nulltr:
  if train['Number_Doses_Week'][i]==0:
    train['Number_Weeks_Used'][i]=0

  else:
    train['Number_Weeks_Used'][i]=42-train['Number_Weeks_Quit'][i]
p=test['Number_Weeks_Used']+test['Number_Weeks_Quit']
p.value_counts().plot(kind='bar',figsize=(30,10));
nullte= test[test['Number_Weeks_Used'].isnull()].index.tolist()
for i in nullte:
  if test['Number_Doses_Week'][i]==0:
    test['Number_Weeks_Used'][i]=0

  else:
    test['Number_Weeks_Used'][i]=42-test['Number_Weeks_Quit'][i]
test.info()
import seaborn as sns
corr = train.corr()

# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=np.bool))

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))

# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)

t= f.suptitle('Correlation Heatmap', fontsize=20)
# Draw the heatmap with the mask and correct aspect ratio
#sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.5,
           # center=0,square=True, linewidths=.5, cbar_kws={"shrink": .5})
hm = sns.heatmap(corr, annot=True, ax=ax, cmap=cmap,fmt='.2f',
                 linewidths=.1,cbar_kws={"shrink": .7})
dep_var = 'Crop_Damage'
cat_names = [	'Pesticide_Use_Category' ]
cont_names = ['Estimated_Insects_Count', 'Number_Doses_Week', 'Number_Weeks_Used', 'Number_Weeks_Quit']<jupyter_output><empty_output><jupyter_text>***using SVC***<jupyter_code>y=train['Crop_Damage']
y.shape
train=train.drop(columns=['Crop_Damage','ID'])
test=test.drop(columns='ID')
print(train.shape)
train.head()
test.shape
X=pd.get_dummies(train,columns=cat_names)
X.shape
Xt=pd.get_dummies(test,columns=cat_names)
Xt.shape<jupyter_output><empty_output><jupyter_text>**  **<jupyter_code>from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
class_weights= {0:0.89897850367626425, 1:1.709531557182564, 2:4.725742928750448}
from sklearn.svm import SVC
classifier2 = SVC(kernel = 'rbf', random_state =0,class_weight=class_weights ,degree=2,C=10)
classifier2.fit(x_train, y_train)
y_pred= classifier2.predict(x_test)
from sklearn.metrics import confusion_matrix
cm=confusion_matrix(y_test,y_pred)
cm
from sklearn.metrics import f1_score, accuracy_score
print('f1-score :', f1_score(y_test,y_pred, average='weighted'))
print('accuracy:', accuracy_score(y_test,y_pred))
#CV
from xgboost import XGBClassifier
classifier2=SVC(kernel = 'rbf', random_state =0,class_weight=class_weights ,degree=2,C=10)
classifier2.fit(x_train, y_train)

from sklearn.model_selection import cross_val_score
acc=cross_val_score(estimator=classifier2, X=X , y=y , cv=10) 
print(acc)
print(acc.mean())
acc.std()<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>class_weights= {0:0.89897850367626425, 1:1.709531557182564, 2:4.725742928750448}
from sklearn.ensemble import RandomForestClassifier
classifier3 = RandomForestClassifier(n_estimators = 25,max_depth=4, criterion = 'entropy', class_weight=random_state = 0)
classifier3.fit(x_train, y_train)
y_pred= classifier3.predict(x_test)
from sklearn.metrics import confusion_matrix
cm=confusion_matrix(y_test,y_pred)
cm
from sklearn.metrics import f1_score, accuracy_score
print('f1-score :', f1_score(y_test,y_pred, average='weighted'))
print('accuracy:', accuracy_score(y_test,y_pred))<jupyter_output><empty_output><jupyter_text><jupyter_code>from sklearn.utils import class_weight
'''class_weights = list(class_weight.compute_class_weight('balanced',
                                             np.unique(y_train),
                                             y_train))'''
class_weights= [0.89897850367626425, 1.409531557182564, 2.025742928750448]

w_array = np.ones(y_train.shape[0], dtype = 'float')
for i, val in enumerate(y_train):
    w_array[i] = class_weights[val-1]
set(w_array)
class_weights
from xgboost import XGBClassifier
classifier4=XGBClassifier(n_estimators=625,reg_lambda=0.22,max_depth=5,tree_method='gpu_hist')
classifier4.fit(x_train, y_train,sample_weight=w_array)
y_pred= classifier4.predict(x_test)
from sklearn.metrics import confusion_matrix
cm=confusion_matrix(y_test,y_pred)
cm
from sklearn.metrics import f1_score, accuracy_score
print('f1-score :', f1_score(y_test,y_pred, average='weighted'))
print('accuracy:', accuracy_score(y_test,y_pred))
#CV
from xgboost import XGBClassifier
classifier4=XGBClassifier(n_estimators=625,reg_lambda=0.1,max_depth=4,tree_method='gpu_hist')
classifier4.fit(x_train, y_train)

from sklearn.model_selection import cross_val_score
acc=cross_val_score(estimator=classifier4, X=X , y=y , cv=10) 
print(acc)
print(acc.mean())
acc.std()



from sklearn.datasets import make_classification
from lightgbm import LGBMClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.metrics import classification_report
model = LGBMClassifier(boosting_type='gbdt',objective='multiclass',num_class=3,metric='multi_logloss',subsample=0.8,
                       n_estimators=665,max_depth=10,learning_rate=0.03,min_data_in_leaf=10,
                       lambda_l2=0.3,random_seed=42,class_weight= {0: 0.5, 1: 0.36, 2: 0.3},verbose=0)
cv = RepeatedStratifiedKFold(n_splits=3, n_repeats=3, random_state=1)
n_scores = cross_val_score(model, x_train, y_train, scoring='accuracy', cv=cv, n_jobs=-1, error_score='raise')
print("done")
print('Accuracy: %.3f (%.3f)' % (np.mean(n_scores), np.std(n_scores)))
model.fit(x_train, y_train)
yhat = model.predict(x_test)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, yhat)
cm
(14713+309)/177772

target_names = ['alive', 'damaged by pesticides', 'damaged by other reason']
print(classification_report(y_test, yhat,target_names=target_names))
ys=model.predict(Xt)
s=pd.read_csv('/content/test.csv')
sub=pd.DataFrame(s['ID'])
sub.head()
sub['Crop_Damage']=ys
sub.head()
sub['Crop_Damage'].value_counts()
sub.to_csv("sub.csv",index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1409759876065066, 'fraction_numerical': 0.04926413632842758, 'mean_word_length': 7.642570281124498, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '398946', 'license': 'MIT', 'n_tokens_mistral': 2662, 'n_tokens_neox': 2482, 'n_words': 374, 'path': 'approaches.ipynb', 'repo_name': 'nikhilreddybilla28/ML4E-Task', 'size': '6455'}