<jupyter_start><jupyter_text>**Hi - Welcome to the Decision trees  exercise.**** This is a reference notebook for the tasks given in exercise section.The first half of this notebook is meant for data preprocessing, it's not require but heavily encouraged to go over them and understand what is going on.The main task of the assignment is in the second half of the notebookRun cells below which import all required libraries** Importing Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Importing datasetAnd viewing first and last rows<jupyter_code>dataset = pd.read_csv("/content/Social_Network_Ads.csv")
dataset.head()

dataset.tail()<jupyter_output><empty_output><jupyter_text>Performing Analysis and Checking For null values<jupyter_code>dataset.shape<jupyter_output><empty_output><jupyter_text>We are having three columns named Age, EstimatedSalary and purchased. Age is the age of viewer viewing adds and EstimatedSalary is their salaries. Purchase(0) signifies the viwer did not purchased it , while purchase(1) signifies he/she purchased it.<jupyter_code>#lets check-out for null values
dataset.isnull().sum()<jupyter_output><empty_output><jupyter_text>**Luckily in our dataset we dont have any null values(empty). ** Please note that in most of the projects you will get values which you need to address and remove first, before moving forward.<jupyter_code>#let's view the statistical information regarding the dataset
dataset.describe()
sns.countplot(dataset["Purchased"])<jupyter_output>/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>We can see around 1/3 of total peoples are actually purchasing adds. Let's Try analyzing data by visualization<jupyter_code>plt.figure(figsize=(20,10))
dataset.groupby(['Age'])['Purchased'].count().plot.bar()
#plt.ylabel('Avg Salary')
#plt.title("Top 10 Highest Paying Jobs",fontsize=20)
plt.show()<jupyter_output><empty_output><jupyter_text>Proceed with any different visual data analysis in the next two cells (Optional but encouraged)<jupyter_code><jupyter_output><empty_output><jupyter_text>After you have done visualisation and analysis Try to write some concluding points you understood from data,like which among the two factors does our output most depend, is there any particular trend or not,etc.  **Splitting dataset into dependent and independent variable.** Here age and estimated salary are the two independent variable(x) and Purchased(y) is dependent variable based on two factors age and estimated salary.<jupyter_code>y= dataset.pop("Purchased")
x= dataset<jupyter_output><empty_output><jupyter_text>Now let split dataset into training testing for model.<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.25, random_state=3)


print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)<jupyter_output>(300, 2)
(100, 2)
(300,)
(100,)<jupyter_text>Feature ScalingBefore feeding and fitting we'll normalise the range of the independent data  or features of data,for better results. **Task 1** your task here is to use ant type of feature scaling methods from sklearn, and standardise or normalise the input features for both training as well as testing data.  And print them to see the changes when there was'nt any scaling.<jupyter_code><jupyter_output><empty_output><jupyter_text>Building model<jupyter_code>from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
y_pred
y_test
round(np.mean(y_pred), 2)<jupyter_output><empty_output><jupyter_text>Model Evaluation: **Task 2**Your next task is to check the accuracy of our model, on both training as well as testing data. try to implement score or accuracy function(method), also find out cross validation score of model.  Checking results visually<jupyter_code>from matplotlib.colors import ListedColormap
X_set, y_set = sc.inverse_transform(X_train), y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step = 0.25),
                     np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))
plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(), X2.ravel()]).T)).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Decision Tree Classification (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()<jupyter_output>*c* argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with *x* & *y*.  Please use the *color* keyword-argument or provide a 2-D array with a single row if you intend to specify the same RGB or RGBA value for all points.
*c* argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with *x* & *y*.  Please use the *color* keyword-argument or provide a 2-D array with a single row if you intend to specify the same RGB or RGBA value for all points.<jupyter_text>**Task 3**Your third and final task is to, use the same method as in above cell and try to visulaise the result for training set.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09378644011327669, 'fraction_numerical': 0.01365983674829252, 'mean_word_length': 5.989522700814901, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '340454', 'license': 'MIT', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1790, 'n_words': 667, 'path': '011/exercise/socialads (1).ipynb', 'repo_name': 'anshu-cloud/99-ML-Learning-Projects', 'size': '6003'}