<jupyter_start><jupyter_text><jupyter_code>!wget 'https://archive.ics.uci.edu/ml/machine-learning-databases/00423/hcc-survival.zip'
!unzip '/content/hcc-survival.zip'
import pandas as pd 

dataset = pd.read_csv (r'/content/hcc-survival/hcc-data.txt', header=None)
dataset.head()
import numpy as np 


dataset = dataset.replace(['?'], np.nan)
dataset.isna().sum()
missing_values_indexes = {}
for column in dataset:
  if dataset[column].isnull().values.any():
    missing_values_indexes[str(column)] = dataset[column].isnull().to_numpy().nonzero()
def missing_handle_classmean(dataframe, column, indexes):
  

  for index in indexes[0]:
    
    missing_value_class_values = []
  
    index_label = dataframe[49][index]
    for row_index, row in dataset.iterrows():
      
      row_label = dataframe[49][row_index]
      if str(index_label) == str(row_label):
        if (dataframe[int(column)][row_index] is None) or (str(dataframe[int(column)][row_index]) == 'nan'):
          pass
        else:
          missing_value_class_values.append( int(float(dataframe[int(column)][row_index])) )

    if missing_value_class_values:
      dataframe[int(column)][index] = np.mean(missing_value_class_values)
    else:
      dataframe[int(column)][index] = 0

  
  return dataframe
def missing_handle_mean(dataframe, column, indexes):
  
  for index in indexes[0]:

    
    column_without_nan = dataframe[int(column)].dropna()
    column_without_nan = np.array(np.array(column_without_nan.values, dtype=float), dtype=int)
    dataframe[int(column)][index] = column_without_nan.mean()
  
  return dataframe

def missing_handler(dataframe, missing_values_indexes, method='Class_mean'):
  

  if method=='Class_mean':
    for key in missing_values_indexes:
      dataframe = missing_handle_classmean(dataframe, key, missing_values_indexes[key])

  elif method=='feature_mean':
    for key in missing_values_indexes:
      dataframe = missing_handle_mean(dataframe, key, missing_values_indexes[key])
  else:
    pass

  
  return dataframe
dataset = missing_handler(dataset, missing_values_indexes, method='Class_mean')
dataset.isna().sum()
def split_dataset(df, train_percentage=0.8, test_percentage=0.2):



  train = df.sample(frac=(1-test_percentage),random_state=200) #random state is a seed value
  test = df.drop(train.index)

  return train, test

trainset, testset = split_dataset(dataset, 0.8, 0.2)
print(len(trainset))
print(len(testset))
x_train = trainset.drop([49], axis = 1).values
y_train = trainset.loc[:, [49]].values.astype('int')

x_test = testset.drop([49], axis = 1).values
y_test = testset.loc[:, [49]].values.astype('int')
print('x_train shapes: ', x_train.shape)
print('y_train shapes: ', y_train.shape)
print('x_test shapes: ', x_test.shape)
print('y_test shapes: ', y_test.shape)
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(max_depth=4, random_state=0)
clf.fit(x_train, y_train)
print(clf.predict(x_test))
print('-----------------')
print(y_test)
clf.score(x_test, y_test)
# accuracy score on test data is 0.87 using << the mean of the class >> while 
# it is 0.74 using << the mean of column >><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13662879950875037, 'fraction_numerical': 0.01289530242554498, 'mean_word_length': 4.254838709677419, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '158134', 'license': 'MIT', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1109, 'n_words': 209, 'path': 'HCC Survival/HCC_Survival.ipynb', 'repo_name': 'alirezash97/Pattern-Recognition-Course', 'size': '3257'}