<jupyter_start><jupyter_text>missing value filed with random sample<jupyter_code>df=data.copy()
df.head()
df["red blood cells"].isnull().sum()
random_sample=df["red blood cells"].dropna().sample(df["red blood cells"].isnull().sum())
df[df["red blood cells"].isnull()].index
random_sample.index
random_sample.index=df[df["red blood cells"].isnull()].index
random_sample.index
random_sample
df.loc[df["red blood cells"].isnull(),"red blood cells"]=random_sample
df["red blood cells"].isnull().sum()<jupyter_output><empty_output><jupyter_text>missing impuation funtiom<jupyter_code>def Random_value_impuation(feature):
    random_sample=df[feature].dropna().sample(df[feature].isnull().sum())
    random_sample.index=df[df[feature].isnull()].index
    df.loc[df[feature].isnull(),feature]=random_sample
for col in num_col:
    Random_value_impuation(col)
df[num_col].isnull().sum()
df[cat_col].isnull().sum()
Random_value_impuation("pus cell")
df["pus cell clumps"].mode()[0]
def impute_mode(feature):
    mode=df[feature].mode()[0]
    df[feature]=df[feature].fillna(mode)
for col in cat_col:
    impute_mode(col)
    
df[cat_col].isnull().sum()<jupyter_output><empty_output><jupyter_text>lable encoding<jupyter_code>for col in cat_col:
    print("{} has {} categories".format(col,df[col].nunique()))
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
for col in cat_col:
    df[col]=le.fit_transform(df[col])
df.head()
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
ind_col=[col for col in df.columns if col!="class"]
dep_col="class"
x=df[ind_col]
y=df[dep_col]
order_rank_feature=SelectKBest(score_func=chi2,k=20)
order_feature=order_rank_feature.fit(x,y)
order_feature.scores_
datascore=pd.DataFrame(order_feature.scores_,columns=["score"])
datascore
dfcols=pd.DataFrame(x.columns)
dfcols
feature_rank=pd.concat([dfcols,datascore],axis=1)
feature_rank.columns=["features","score"]
feature_rank
feature_rank.nlargest(10,"score")
selected_columns=feature_rank.nlargest(10,"score")['features'].values
x_new=df[selected_columns]
x_new.shape
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x_new,y,random_state=0,test_size=0.25)
from xgboost import XGBClassifier
xgb=XGBClassifier()
params={"learning_rate":[0.05,0.20,0.25],
        "max_depth":[5,8,10],
        "min_child_weight":[1,3,5,7],
        "gamma":[0.0,0.1,0.2,0.4],
        "colsample_bytree":[0.3,0.4,0.7]
       }
from sklearn.model_selection  import RandomizedSearchCV
random_serach=RandomizedSearchCV(xgb,param_distributions=params,n_iter=5,scoring="roc_auc",n_jobs=-1,cv=5,verbose=3)
random_serach.fit(x_train,y_train)
random_serach.best_estimator_
random_serach.best_params_
classifier=XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
       colsample_bynode=1, colsample_bytree=0.4, gamma=0.1, gpu_id=-1,
       importance_type='gain', interaction_constraints='',
       learning_rate=0.2, max_delta_step=0, max_depth=5,
       min_child_weight=1,monotone_constraints='()',
       n_estimators=100, n_jobs=0, num_parallel_tree=1,
       objective='binary:logistic', random_state=0, reg_alpha=0,
       reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method='exact',
       validate_parameters=1, verbosity=None)
classifier.fit(x_train,y_train)
y_pred=classifier.predict(x_test)
from sklearn.metrics import confusion_matrix,accuracy_score
confusion_matrix(y_pred,y_test)
accuracy_score(y_pred,y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14730878186968838, 'fraction_numerical': 0.021246458923512748, 'mean_word_length': 8.124031007751938, 'pattern_counts': {'":': 5, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '9592', 'license': 'Apache-2.0', 'n_tokens_mistral': 1331, 'n_tokens_neox': 1307, 'n_words': 166, 'path': 'choronic kidney.ipynb', 'repo_name': 'ankitkumar0/chornic-kidney', 'size': '3530'}