<jupyter_start><jupyter_text>Intro to Machine Learning   Part-1 **[Intro to Machine Learning Part1 Medium Article](https://medium.com/@aryanc55/intro-to-machine-learning-part-1-byaryan-e76b4b4248bb?sk=6b9a4217f4933d7c646515ad99bcbceb).**I hope you have already read **[What is Machine Learning](https://medium.com/@aryanc55/what-is-machine-learning-60ec74f85d7).**<jupyter_code>import pandas as pd
# save filepath to variable for easier access
melbourne_file_path = '../hitchhikersGuideToMachineLearning/melbourne-housing-market/Melbourne_housing_FULL.csv'
# read the data and store data in DataFrame titled melbourne_data
melbourne_data = pd.read_csv(melbourne_file_path) 
# print a summary of the data in Melbourne data
melbourne_data.describe()

# The Melbourne data has some missing values (some houses for which some variables weren't recorded.)
# We'll learn to handle missing values in a later tutorial.  
# Your Iowa data doesn't have missing values in the columns you use. 
# So we will take the simplest option for now, and drop houses from our data. 
# Don't worry about this much for now, though the code is:

# dropna drops missing values (think of na as "not available")
melbourne_data = melbourne_data.dropna(axis=0)<jupyter_output><empty_output><jupyter_text>Step 1: Specify Prediction TargetSelect the target variable, which corresponds to the sales price. Save this to a new variable called `y`. You'll need to print a list of the columns to find the name of the column you need.<jupyter_code># print the list of columns in the dataset to find the name of the prediction target
melbourne_data.columns
y = melbourne_data.Price<jupyter_output><empty_output><jupyter_text>Step 2: Create XNow you will create a DataFrame called `X` holding the predictive features.Since you want only some columns from the original data, you'll first create a list with the names of the columns you want in `X`.You'll use just the following columns in the list (you can copy and paste the whole list to save some typing, though you'll still need to add quotes):       * Rooms    * Distance     * Postcode     * Bedroom2     * Bathroom    * Landsize     * BuildingArea     * YearBuilt        After you've created that list of features, use it to create the DataFrame that you'll use to fit the model.<jupyter_code># Create the list of features below
feature_names = ['Rooms','Distance', 'Postcode', 'Bedroom2', 'Bathroom',
       'Landsize', 'BuildingArea', 'YearBuilt']

# Select data corresponding to features in feature_names
X = melbourne_data[feature_names]<jupyter_output><empty_output><jupyter_text>Review DataBefore building a model, take a quick look at **X** to verify it looks sensible<jupyter_code># Review data
# print description or statistics from X
X.describe()
# print the top few lines
X.head()<jupyter_output><empty_output><jupyter_text>Step 3: Specify and Fit ModelCreate a `DecisionTreeRegressor` and save it iowa_model. Ensure you've done the relevant import from sklearn to run this command.Then fit the model you just created using the data in `X` and `y` that you saved above.<jupyter_code>from sklearn.tree import DecisionTreeRegressor
#specify the model. 
#For model reproducibility, set a numeric value for random_state when specifying the model
iowa_model = DecisionTreeRegressor(random_state=786)

# Fit the model
iowa_model.fit(X,y)<jupyter_output><empty_output><jupyter_text>Step 4: Make PredictionsMake predictions with the model's `predict` command using `X` as the data. Save the results to a variable called `predictions`.<jupyter_code>predictions = iowa_model.predict(X)
print(predictions)<jupyter_output>[1035000. 1465000. 1600000. ...  888000.  705000. 1020000.]<jupyter_text>Think About Your ResultsUse the `head` method to compare the top few predictions to the actual home values (in `y`) for those same homes. Anything surprising?<jupyter_code># You can write code in this cell
y.head()<jupyter_output><empty_output><jupyter_text>It's natural to ask how accurate the model's predictions will be and how you can improve that. That will be you're next step. Keep GoingYou are ready for **[Intro to Machine Learning Part2](https://medium.com/@aryanc55/intro-to-machine-learning-part2-byaryan-4dd7f20f4a19?source=friends_link&sk=aec114377221595b9b0f46612441a49d).** Read this article and comeback to execute the codes!   Part2: Here we will discuss model validation , overfitting and one the most used model in competitive datascience  Step 1: Split Your DataUse the `train_test_split` function to split up your data.Give it the argument `random_state=1` so the `check` functions know what to expect when verifying your code.Recall, your features are loaded in the DataFrame **X** and your target is loaded in **y**.<jupyter_code># Import the train_test_split function and uncomment
from sklearn.model_selection import train_test_split

# fill in and uncomment
train_X, val_X, train_y, val_y = train_test_split(X,y,random_state = 1)<jupyter_output><empty_output><jupyter_text>Step 2: Specify and Fit the ModelCreate a `DecisionTreeRegressor` model and fit it to the relevant data.Set `random_state` to 1 again when creating the model.<jupyter_code># You imported DecisionTreeRegressor in your last exercise
# and that code has been copied to the setup code above. So, no need to
# import it again

# Specify the model
iowa_model = DecisionTreeRegressor(random_state=1)

# Fit iowa_model with the training data.
iowa_model.fit(train_X,train_y)<jupyter_output><empty_output><jupyter_text>Step 3: Make Predictions with Validation data<jupyter_code># Predict with all validation observations
val_predictions = iowa_model.predict(val_X)<jupyter_output><empty_output><jupyter_text>Inspect your predictions and actual values from validation data.<jupyter_code># print the top few validation predictions
print(val_predictions)
# print the top few actual prices from validation data
print(val_y.head())<jupyter_output>[ 625000.  635000. 2280000. ... 2260000.  740000. 1230000.]
17359     740000.0
17097     572000.0
5265     3225000.0
21286     626000.0
9450      850000.0
Name: Price, dtype: float64<jupyter_text>What do you notice that is different from what you saw with in-sample predictions (which are printed after the top code cell in this page).Do you remember why validation predictions differ from in-sample (or training) predictions? This is an important idea from the last lesson. Step 4: Calculate the Mean Absolute Error in Validation Data But here is twist!We will also calcukate it over the predicted and real value of preivious article model predictins!<jupyter_code>from sklearn.metrics import mean_absolute_error
mean_absolute_error(y, predictions)
from sklearn.metrics import mean_absolute_error
val_mae = mean_absolute_error(val_y,val_predictions)

# uncomment following line to see the validation_mae
print(val_mae)<jupyter_output>244035.32538253826<jupyter_text>Is that MAE good?  There isn't a general rule for what values are good that applies across applications. But you'll see how to use (and improve) this number in the next step. Keep Going You could write the function `get_mae` yourself. For now, we'll supply it. This is the same function you read about in the previous lesson. Just run the cell below.<jupyter_code>def get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y):
    model = DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes, random_state=0)
    model.fit(train_X, train_y)
    preds_val = model.predict(val_X)
    mae = mean_absolute_error(val_y, preds_val)
    return(mae)<jupyter_output><empty_output><jupyter_text>Step 1: Compare Different Tree SizesWrite a loop that tries the following values for *max_leaf_nodes* from a set of possible values.Call the *get_mae* function on each value of max_leaf_nodes. Store the output in some way that allows you to select the value of `max_leaf_nodes` that gives the most accurate model on your data.<jupyter_code>import numpy as np
candidate_max_leaf_nodes = [5, 25, 50, 100, 250, 500]
# Write loop to find the ideal tree size from candidate_max_leaf_nodes
leaf = []
for i in candidate_max_leaf_nodes:
        leaf.append(get_mae(i,train_X, val_X, train_y, val_y))
        print("Max leaf nodes: %d  \t\t Mean Absolute Error:  %d" % (i,get_mae(i,train_X, val_X, train_y, val_y)))
    

# Store the best value of max_leaf_nodes (it will be either 5, 25, 50, 100, 250 or 500)
best_tree_size = candidate_max_leaf_nodes[np.argmin(leaf)]<jupyter_output>Max leaf nodes: 5  		 Mean Absolute Error:  348324
Max leaf nodes: 25  		 Mean Absolute Error:  265095
Max leaf nodes: 50  		 Mean Absolute Error:  244247
Max leaf nodes: 100  		 Mean Absolute Error:  229963
Max leaf nodes: 250  		 Mean Absolute Error:  224545
Max leaf nodes: 500  		 Mean Absolute Error:  228240<jupyter_text>Step 2: Fit Model Using All DataYou know the best tree size. If you were going to deploy this model in practice, you would make it even more accurate by using all of the data and keeping that tree size.  That is, you don't need to hold out the validation data now that you've made all your modeling decisions.<jupyter_code># Fill in argument to make optimal size and uncomment
final_model = DecisionTreeRegressor(max_leaf_nodes=best_tree_size)

# fit the final model and uncomment the next two lines
final_model.fit(X, y)<jupyter_output><empty_output><jupyter_text>Step 1: Use a Random Forest<jupyter_code>from sklearn.ensemble import RandomForestRegressor

# Define the model. Set random_state to 1
rf_model = RandomForestRegressor()

# fit your model
rf_model.fit(train_X,train_y)

# Calculate the mean absolute error of your Random Forest model on the validation data
rf_val_predictions = rf_model.predict(val_X)
rf_val_mae = mean_absolute_error(rf_val_predictions, val_y)

print("Validation MAE for Random Forest Model: {}".format(rf_val_mae))<jupyter_output>Validation MAE for Random Forest Model: 178339.34209163772
{'fraction_non_alphanumeric': 0.06877773295683744, 'fraction_numerical': 0.035397337636143604, 'mean_word_length': 5.439610389610389, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 3, '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': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '344676', 'license': 'MIT', 'n_tokens_mistral': 3276, 'n_tokens_neox': 2945, 'n_words': 1166, 'path': 'IntroToML.ipynb', 'repo_name': 'aryanc55/MLIn10Minutes', 'size': '9916'}