<jupyter_start><jupyter_text>Missing Value Imputation<jupyter_code>df["Time_taken"].fillna(value=df["Time_taken"].mean(), inplace = True)<jupyter_output><empty_output><jupyter_text>Dummy variable creation<jupyter_code>df = pd.get_dummies(df,columns = ["3D_available","Genre"],drop_first = True)
df.head()<jupyter_output><empty_output><jupyter_text>X-y split<jupyter_code>X = df.loc[:,df.columns!="Collection"]
type(X)
y = df["Collection"]
type(y)<jupyter_output><empty_output><jupyter_text>Test-Train Split<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)<jupyter_output><empty_output><jupyter_text>Traing Regression Tree<jupyter_code>from sklearn import tree
regtree = tree.DecisionTreeRegressor(max_depth = 3)
regtree.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Predict values using trained model<jupyter_code>y_train_pred = regtree.predict(X_train)
y_test_pred = regtree.predict(X_test)<jupyter_output><empty_output><jupyter_text>Model Performance<jupyter_code>from sklearn.metrics import mean_squared_error, r2_score
mean_squared_error(y_test, y_test_pred)
r2_score(y_train, y_train_pred)
r2_score(y_test, y_test_pred)<jupyter_output><empty_output><jupyter_text>Plotting a decision tree<jupyter_code>dot_data = tree.export_graphviz(regtree, out_file=None)
from IPython.display import Image
import pydotplus
graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11695906432748537, 'fraction_numerical': 0.005198180636777128, 'mean_word_length': 14.247524752475247, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '373008', 'license': 'MIT', 'n_tokens_mistral': 611, 'n_tokens_neox': 591, 'n_words': 69, 'path': 'estudos_python/Machine_Learning/Others/3. ST Academy - Decision Trees resource files/Python Code/Tree_Intro.ipynb', 'repo_name': 'robertoweller/python', 'size': '1539'}