<jupyter_start><jupyter_text>exploratory data analysis (EDA)<jupyter_code>df.Survived.value_counts().plot(kind='bar')
df.Survived.value_counts()
plt.figure(figsize=(10, 5))
sns.distplot(df.Age[df.Survived == 1], bins=[0, 5, 12, 18, 40, 120], label='Survived', color='green')
sns.distplot(df.Age[df.Survived == 0], bins=[0, 5, 12, 18, 40, 120], label='Died :(', color='red')
plt.legend()
plt.figure(figsize=(25, 5))
sns.distplot(df.Fare[df.Survived == 1], bins=25, label='Survived', color='green')
sns.distplot(df.Fare[df.Survived == 0], bins=25, label='Died :(', color='red')
plt.legend()
df.head()
catVar = ['Pclass', 'Sex', 'SibSp', 'Parch', 'Embarked']

fig, axes = plt.subplots(2,3, figsize=(15,10))
for cat, ax in zip(catVar, axes.flatten()):
  sns.countplot(cat, data=df, hue="Survived", ax=ax)
pd.crosstab(df.Pclass, df.Survived, normalize="index").style.background_gradient(cmap='summer_r')
df["alone"] = (df.SibSp==0) & (df.Parch==0)
df.head()
df.Age = pd.cut(df.Age, [0, 5, 12, 18, 40, 120], labels=['balita', 'anak-anak', 'remaja', 'dewasa', 'lansia'])
df.head()
df.Fare = pd.cut(df.Fare, [0, 25, 100, 600], labels=['murah', 'mahal', 'esksekutif'])
df.head()<jupyter_output><empty_output><jupyter_text>data splitting<jupyter_code># splitting data 
X = df.drop(columns='Survived')
y = df.Survived

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,stratify=y, random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>preprocessor<jupyter_code>X_train.head()
from jcopml.pipeline import num_pipe, cat_pipe
# preprocessor pipeline
Preprocessor = ColumnTransformer([
  ("numerical", num_pipe(scaling="minmax"), ['SibSp', 'Parch']),
  ("categorical", cat_pipe(encoder='onehot'), ['Pclass', 'Sex', 'Embarked', 'Fare', 'Age', 'alone'])
])<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>from jcopml.tuning import grid_search_params as gsp
gsp.knn_params
pipeline = Pipeline([
  ("prep", Preprocessor),
  ("algo", KNeighborsClassifier())
])


#parameter tunning
params = {
  "algo__n_neighbors": range(1, 51, 2),
  "algo__weights": ["uniform", "distance"],
  "algo__p": [1, 2],  
}

model = GridSearchCV(pipeline, gsp.knn_params, cv=3, n_jobs=-1, verbose=1)
model.fit(X_train, y_train)

# evaluate model
pd.DataFrame(model.cv_results_).sort_values(by='rank_test_score')
model.best_params_
model.score(X_test, y_test), model.score(X_train, y_train), model.best_score_
from luwiji.knn import illustration
illustration.simple_model_advantage<jupyter_output><empty_output><jupyter_text>buat ngetest model yang paling simple, nah kita bisa pake random guesing sama treshold. ini tuh model yang paling ancur<jupyter_code>from sklearn.metrics import accuracy_score
randomPredTrain = np.random.randint(0, 2, size=y_train.shape)
randomPredTest = np.random.randint(0, 2, size=y_test.shape)
train_acc = accuracy_score(y_train, randomPredTrain)
test_acc = accuracy_score(y_test, randomPredTest)
print(f"Random prediction accuracy: {train_acc:.2f}% train, {test_acc:.2f}% test")
pred_train = X_train.Sex.map({"male":0, "female":1})
pred_test = X_test.Sex.map({"male":0, "female":1})
train = accuracy_score(y_train, pred_train)
test = accuracy_score(y_test, pred_test)
print(train, test)
import numpy as np
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer

from jcopml.pipeline import num_pipe, cat_pipe
from jcopml.utils import save_model, load_model
from jcopml.plot import plot_missing_value
from jcopml.feature_importance import mean_score_decrease

df = pd.read_csv("input_directory", index_col="input_nama_index", parse_dates=True)
df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16189719130895602, 'fraction_numerical': 0.023052464228934817, 'mean_word_length': 7.77906976744186, 'pattern_counts': {'":': 7, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '648130', 'license': 'MIT', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1464, 'n_words': 281, 'path': '04 - KNN & Scikit-learn/part4-EDA&FeatureEngineering.ipynb', 'repo_name': 'FransiscoReadyPermana/supervised_learning', 'size': '3774'}