<jupyter_start><jupyter_text>Regression<jupyter_code>from sklearn.tree import DecisionTreeRegressor
tree_reg = DecisionTreeRegressor(max_depth=2)
tree_reg.fit(X, y)
export_graphviz(
    tree_reg, 
    out_file=os.path.join(IMAGES_PATH, "iris_tree_reg_2.dot"),
    feature_names=iris.feature_names[2:],
    class_names=iris.target_names,
    rounded=True,
    filled=True
)<jupyter_output><empty_output><jupyter_text>Excercise:  Ans.7<jupyter_code>from sklearn.datasets import make_moons
moons = make_moons(n_samples=1000, noise=0.4)
type(moons)
X = moons[0]
y = moons[1]
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)
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
pipe = Pipeline([
    ("scaler", StandardScaler()),
    ("dt_clf", DecisionTreeClassifier()),
])
params = {
    "dt_clf__max_depth": np.random.randint(3, 10, size=5),
    "dt_clf__max_leaf_nodes": np.random.randint(5, 10, size=4),
    "dt_clf__presort": [True, False],
}
best_model = GridSearchCV(estimator=pipe, param_grid=params, cv=5, scoring='accuracy',                                   verbose=1, return_train_score=True)
best_model.fit(X_train, y_train)
best_model.best_estimator_
predictions = best_model.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, predictions)
accuracy<jupyter_output><empty_output><jupyter_text>Ans.8 Random Forest Classifier<jupyter_code>from sklearn.model_selection import ShuffleSplit
splitter = ShuffleSplit(n_splits=1000, train_size=0.1, test_size=0.9)
predictions = np.zeros(shape=(1000, X_test.shape[0]))
acc_scores = np.zeros(shape=(1000))
predictions.shape
acc_scores.shape
indices = splitter.split(X)
zip(np.arange(0, 1000), indices)
for i, index in zip(np.arange(0, 1000), indices):
    
    # train_index = index[0]
    # test_index = index[1]
    X_train, y_train = X[index[0]], y[index[0]]
    
    # fitting the data on the model having best hyperparameters 
    # from the above questions
    dt_clf = best_model.best_estimator_
    dt_clf.fit(X_train, y_train)
    

    # predicting using test set (the original one used in above ques)
    predictions[i, :] = dt_clf.predict(X_test)
    acc_scores[i] = accuracy_score(y_test, predictions[i, :])
predictions[0]
acc_scores[:10]
acc_scores.mean(), acc_scores.max(), acc_scores.min()
from scipy.stats import mode
final_predictions = np.array(mode(predictions, axis=0))
final_predictions.shape
final_accuracy = accuracy_score(y_test, final_predictions[0][0])
final_accuracy<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11551401869158878, 'fraction_numerical': 0.023177570093457944, 'mean_word_length': 5.861538461538461, 'pattern_counts': {'":': 3, '<': 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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '413771', 'license': 'MIT', 'n_tokens_mistral': 1019, 'n_tokens_neox': 976, 'n_words': 177, 'path': 'notebooks/Decision_trees.ipynb', 'repo_name': 'prikmm/AI-Concepts', 'size': '2675'}