<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np

import warnings

import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_csv('https://raw.githubusercontent.com/Tech-i-s/techis-ds-wiki/master/Step%204-1%20Model%20Optimisation/02_Measuring%20model%20performance/iris.csv?token=<KEY>')
df.head()
# converting the species labels into numeric
from sklearn import preprocessing
# label encoder object knows how to understand word labels
label_encoder = preprocessing.LabelEncoder()

# encode the labels in the species column
df['species'] = label_encoder.fit_transform(df['species'])
df.head()
# constructing the correlation matrix
corr = df.corr()
corr
plt.figure(figsize = (6,6))
sns.heatmap(corr, annot=True, cmap='cubehelix_r', square=True)
plt.show()
abs(corr['species'])[:-1].sort_values(ascending = False)
# Splitting Data set into train and validation

from sklearn.model_selection import train_test_split
train, test = train_test_split(df, test_size=0.30)
#considering only highly correlated features for training and validation
train_x = train.drop(['species'], axis = 1)
train_y = train['species']             

test_x = test.drop(['species'],axis = 1)
test_y = test['species']
print('Dimension of train_x :',train_x.shape)
print('Dimension of train_y :',train_y.shape)
print('Dimension of test_x :',test_x.shape)
print('Dimension of test_y :',test_y.shape)
#Using Decision Tree Classifier for model building

from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier(random_state=0,max_depth=3)
clf.fit(train_x, train_y)
# Predict Species for test data
pred = clf.predict(test_x)
pred
# validation of built model
from sklearn.metrics import accuracy_score
print('The accuracy of the Decision Tree is {}%'.format(round(accuracy_score(pred,test_y)*100,2)))
# create a confusion matrix
from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(test_y, pred) # pass in y_test and then the predicted y

plt.figure(figsize=(6,5))
sns.heatmap(conf_mat, annot=True)
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
from sklearn.metrics import classification_report 

print ('Confusion Matrix :')
print(conf_mat) 
print ("*"*50)
print ('Report :' )
print (classification_report(test_y, pred)) 
print ("*"*50)
# log_loss
from sklearn.metrics import log_loss
pred_prob = clf.predict_proba(test_x)
logloss = log_loss(test_y, pred_prob)
print('log_loss:', round(logloss, 3))
test = pd.read_csv('https://raw.githubusercontent.com/Tech-i-s/techis-ds-wiki/master/Step%204-1%20Model%20Optimisation/02_Measuring%20model%20performance/test.csv?token=<KEY>')
train = pd.read_csv('https://raw.githubusercontent.com/Tech-i-s/techis-ds-wiki/master/Step%204-1%20Model%20Optimisation/02_Measuring%20model%20performance/train.csv?token=<KEY>')
test.head()
train.head()
df = pd.concat([train, test], ignore_index=True)
df.head()
df.shape
#Printing Dimension of datasets
print("Dimension of train dataset", train.shape)
print("Dimension of test dataset", test.shape)
print("Dimension of data dataset", df.shape)
df = df.select_dtypes(include = ['float64', 'int64'])
df.head()
df.info()
df.describe()
miss_val = df.isna().sum()
miss_val
x = miss_val[miss_val>0].sort_values(ascending = False)
x
columns = list(x.index)
columns
# iterating over the columns and filling them with the average of that particular column
for column in columns:
  df[column] = df[column].fillna(df[column].mean())
#Spliting dataframe into train and test by manually as intially we have two dataframes train and test.
df_train = df[:1460]
df_test = df[1460:]
data_corr = df_train.corr()
data_corr
#considering only the features which have absolute correlation value greater tha 0.60

data_cor = data_corr['SalePrice'][:-1]
data_cor[abs(data_cor)>=0.60].sort_values(ascending=False)
x = data_corr['SalePrice']
x = x[abs(x)>=0.60].sort_values(ascending=False)
columns = list(x.index)
columns
# df_test.drop(['SalePrice'],axis = 1, inplace = True)
df_train = df_train[columns]
df_test = df_test[columns[1:]]
corr = df_train.corr()
plt.figure(figsize=(5,5))
sns.heatmap(corr, annot=True, cmap='cubehelix_r',square=True) 
plt.show()
from sklearn.model_selection import train_test_split
train, val = train_test_split(df_train, test_size = .25)
#considering only the highly correlated features for training and validation
train_x = train.drop(['SalePrice'], axis = 1)
train_y = train['SalePrice']             # output of the training data

val_x = val.drop(['SalePrice'], axis = 1)
val_y = val['SalePrice']
print('Dimension of train_x dataframe:',train_x.shape)
print('Dimension of train_y dataframe:',train_y.shape)
print('Dimension of val_x dataframe:',val_x.shape)
print('Dimension of val_y dataframe:',val_y.shape)
from sklearn.linear_model import LinearRegression
ln_reg = LinearRegression()
ln_reg.fit(train_x, train_y)
df_pred = ln_reg.predict(val_x)
# MSE (mean squared error)
from sklearn.metrics import mean_squared_error

MSE = mean_squared_error(df_pred, val_y)
print(f'Mean Square Error of linear regression is {round(MSE, 2)}')
# root mean squared error
import math

RMSE = math.sqrt(mean_squared_error(df_pred, val_y))
print(f'Root Mean Square Error of Linear regression:{round(RMSE,2)}')
# Mean ablsolute Error
from sklearn.metrics import mean_absolute_error

MAE = math.sqrt(mean_absolute_error(df_pred, val_y))
print(f"Mean Absolute Error of Linear Regression:{round(MAE, 2)}")
# Mean Absolute Percentage Error
MAPE = np.mean(np.abs((df_pred-val_y)/val_y))*100
print(f'Mean absolute Percentage Error of Linear Regression:{round(MAPE, 2)}')
# R_Squared Score 
from sklearn.metrics import r2_score
r2 = r2_score(df_pred, val_y)
print(f'R_Squared Score:{round(r2, 3)}')
# Adjusted R_squared score
def adj_r2(x, y):
  N = x.shape[0]
  p = x.shape[1]
  adjusted_r2 = 1 - (1-r2)*(N-1)/(N-p-1)
  return adjusted_r2

adjusted_r2 = adj_r2(val_x, val_y)
print(f'Adjusted r2_score:{round(adjusted_r2, 3)}')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12571709555810523, 'fraction_numerical': 0.019832814292738896, 'mean_word_length': 5.772475027746948, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 11, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '649168', 'license': 'MIT', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2148, 'n_words': 506, 'path': 'PerformanceMeasurement/Mahfuzur_PerformanceMeasurement.ipynb', 'repo_name': 'mahfuz978/Model-Optimization', 'size': '6173'}