<jupyter_start><jupyter_text>Step-1 Exploring and Preparing the Data<jupyter_code>import pandas as pd
# Read the csv file
credit = pd.read_csv("./creditData.csv")
credit.head(4)
credit.isnull().sum()<jupyter_output><empty_output><jupyter_text>Q1. What is your suggestion if you see any NA values? 1 . The easiest way is to ignore the tuples or rows with missing values this can only be done when the missing values is less than 2% of the data. We can do this only when we can afford to loose a bit of data. 2 . The second way is to do the imputation , which is replacing the data with mean or median of the other variables. we can use libraries like mtcars to do this.3 . The other way is to do the interpolation, This can be done only when we the data points have linear relationship.  Q2. Compute the percentage of both classes similar to what you did in lab 1 and see if the distribution of both classes preserved for both training and testing data<jupyter_code>from sklearn.model_selection import train_test_split
y = credit['Creditability']
X = credit.drop(['Creditability'],axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25,
random_state=123)

print("Credit test")
y_test.value_counts()/250
print("Credit train")
y_train.value_counts()/750<jupyter_output>Credit train<jupyter_text>Ans. From the prop tables above we can see that the distribution is uniform for test as well as train datasets  Step 2: Training a Model on the Data<jupyter_code>from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb = gnb.fit(X_train, y_train)
y_pred = gnb.fit(X_train, y_train).predict(X_test)
# Evaluating the model performance and calculating accuracy

from sklearn.metrics import confusion_matrix 
from sklearn.metrics import accuracy_score

print(confusion_matrix(y_test, y_pred)) 
print("Accuracy Score is")
print(accuracy_score(y_test, y_pred)*100)<jupyter_output>[[ 60  25]
 [ 34 131]]
Accuracy Score is
76.4<jupyter_text>PART-2 Improving Perfomance  Step 1: Exploring and preparing the data<jupyter_code>import matplotlib.pyplot as plt
import numpy as np

corr = X_train.corr()
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(corr,cmap='coolwarm', vmin=-1, vmax=1)
fig.colorbar(cax)
ticks = np.arange(0,len(X_train.columns),1)
ax.set_xticks(ticks)
plt.xticks(rotation=90)
ax.set_yticks(ticks)
ax.set_xticklabels(X_train.columns)
ax.set_yticklabels(X_train.columns)
plt.show()
corr_matrix = X.corr().abs()
# Select upper triangle of correlation matrix
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape),
k=1).astype(np.bool))
# Find index of feature columns with correlation greater than 0.60
to_drop = [column for column in upper.columns if any(upper[column] > 0.6)]
Xnew = X.drop(to_drop, axis=1)
# creating the model with new values

from sklearn.model_selection import train_test_split
y = credit['Creditability']
X = credit.drop(['Creditability'],axis=1)
X_train, X_test, y_train, y_test = train_test_split(Xnew, y, test_size=0.25,
random_state=123)

from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb = gnb.fit(X_train, y_train)
y_pred = gnb.fit(X_train, y_train).predict(X_test)

from sklearn.metrics import confusion_matrix 
from sklearn.metrics import accuracy_score

print(confusion_matrix(y_test, y_pred)) 
print("Accuracy Score is")
print(accuracy_score(y_test, y_pred)*100)<jupyter_output>[[ 65  20]
 [ 37 128]]
Accuracy Score is
77.2<jupyter_text>Q3. What is the accuracy this time? Be sure to include in your results report whether or not, after all this work, the performance of your Naïve Bayes Classifier was improved.  The Accuracy here is 77.2 compared to the previous value of 76.4  Part 3. Support Vector Machine  Step 1: Collecting the Data<jupyter_code>import pandas as pd
input_file = "./letterdata.csv"
letters = pd.read_csv(input_file)
letters.head(5)
from sklearn.model_selection import train_test_split
y = letters['letter']
X = letters.drop(['letter'],axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1,
random_state=123)

print(X_train.shape)
print(y_train.shape)
from sklearn import svm
# Design the model
clf = svm.SVC(kernel='linear')
#clf = svm.LinearSVC(C=1)
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
confusion_matrix(y_test, y_predict)
print(accuracy_score(y_test, y_predict)*100)<jupyter_output>86.15<jupyter_text>Q4. We may be able to do better than this by changing the Kernels. Try Polynomial and RBF kernels to improve the result.<jupyter_code># Polynomial Kernal 

from sklearn import svm
# Design the model
clf = svm.SVC(kernel='poly')
#clf = svm.LinearSVC(C=1)
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
confusion_matrix(y_test, y_predict)
print(accuracy_score(y_test, y_predict)*100)
# RBF Kernel

from sklearn import svm
# Design the model
clf = svm.SVC(kernel='rbf')
#clf = svm.LinearSVC(C=1)
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
confusion_matrix(y_test, y_predict)
print(accuracy_score(y_test, y_predict)*100)<jupyter_output>93.10000000000001<jupyter_text>The accuracy has improved in Polynomial to 95.25 and to 93.1 in RBF  Part 4 , News Popularity   PART - 1 NAIVE BAYES<jupyter_code># Applying Naive Bayes and SVM to News Popularity DataSet 

# Read the csv file
import pandas as pd
news = pd.read_csv("./NewsPopularity.csv")
news.head(4)
# Check for missing data
news.isnull().sum()
# split dataset to 70-30 training and testing resp.
from sklearn.model_selection import train_test_split

popular = news.shares >= 1400
unpopular = news.shares < 1400
news.loc[popular,'shares'] = 1
news.loc[unpopular,'shares'] = 0

y = news['shares']

X = news[["n_tokens_title", "n_tokens_content", "n_unique_tokens", "n_non_stop_words", "num_hrefs", "num_imgs", "num_videos", "average_token_length", "num_keywords", "kw_max_max", "global_sentiment_polarity", "avg_positive_polarity", "title_subjectivity", "title_sentiment_polarity", "abs_title_subjectivity", "abs_title_sentiment_polarity", "shares"]].drop(['shares'],axis=1)

X_train, X_test, y_train, y_test = train_test_split(X , y, test_size=0.25,
random_state=123)

print(X_train.shape)
print(y_train.shape)

# Training a model
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb = gnb.fit(X_train, y_train)
y_pred = gnb.fit(X_train, y_train).predict(X_test)
# Evaluating the model performance and calculating accuracy
from sklearn.metrics import confusion_matrix 
from sklearn.metrics import accuracy_score

print(confusion_matrix(y_test, y_pred)) 

print(accuracy_score(y_test, y_pred)*100)<jupyter_output>[[4330  274]
 [4732  575]]
49.49046513974372<jupyter_text>PART-2 NAIVE BAYES ( IMPROVING PERFORMANCE )<jupyter_code>import matplotlib.pyplot as plt
import numpy as np

corr = X_train.corr()
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(corr,cmap='coolwarm', vmin=-1, vmax=1)
fig.colorbar(cax)
ticks = np.arange(0,len(X_train.columns),1)
ax.set_xticks(ticks)
plt.xticks(rotation=90)
ax.set_yticks(ticks)
ax.set_xticklabels(X_train.columns)
ax.set_yticklabels(X_train.columns)
plt.show()
X = news[["n_tokens_title", "n_tokens_content", "n_unique_tokens", "n_non_stop_words", "num_hrefs", "num_imgs", "num_videos", "average_token_length", "num_keywords", "kw_max_max", "global_sentiment_polarity", "avg_positive_polarity", "title_subjectivity", "title_sentiment_polarity", "abs_title_subjectivity", "abs_title_sentiment_polarity", "shares"]].drop(['shares'],axis=1)

corr_matrix = X.corr().abs()
# Select upper triangle of correlation matrix
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape),
k=1).astype(np.bool))
# Find index of feature columns with correlation greater than 0.60
to_drop = [column for column in upper.columns if any(upper[column] > 0.6)]
Xnew = X.drop(to_drop, axis=1)

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(Xnew, y, test_size=0.25,
random_state=123)

from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb = gnb.fit(X_train, y_train)
y_pred = gnb.fit(X_train, y_train).predict(X_test)

from sklearn.metrics import confusion_matrix 
from sklearn.metrics import accuracy_score

print(confusion_matrix(y_test, y_pred)) 
print("Accuracy Score is")
print(accuracy_score(y_test, y_pred)*100)<jupyter_output>[[3821  783]
 [4083 1224]]
Accuracy Score is
50.903037029563116<jupyter_text>PART 3 . Support Vector Machine on News Popularity DataSet<jupyter_code>from sklearn import svm
from sklearn.metrics import confusion_matrix 
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler(feature_range=(-1,1)).fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)


clf = svm.SVC(kernel='linear')
#clf = svm.LinearSVC(C=1)
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
confusion_matrix(y_test, y_predict)
print(accuracy_score(y_test, y_predict)*100)
# Polynomial kernel 

from sklearn import svm
from sklearn.metrics import confusion_matrix 
from sklearn.metrics import accuracy_score
# Design the model
clf = svm.SVC(kernel='poly')
#clf = svm.LinearSVC(C=1)
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
confusion_matrix(y_test, y_predict)
print(accuracy_score(y_test, y_predict)*100)
# RBF kernel 
from sklearn import svm
from sklearn.metrics import confusion_matrix 
from sklearn.metrics import accuracy_score

clf = svm.SVC(kernel='rbf')
#clf = svm.LinearSVC(C=1)
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
confusion_matrix(y_test, y_predict)
print(accuracy_score(y_test, y_predict)*100)<jupyter_output>57.22934113611139
{'fraction_non_alphanumeric': 0.10691301640024911, 'fraction_numerical': 0.02916753165870874, 'mean_word_length': 6.646825396825397, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '393941', 'license': 'MIT', 'n_tokens_mistral': 3727, 'n_tokens_neox': 3477, 'n_words': 890, 'path': 'lab-2#yash_suruneni.ipynb', 'repo_name': 'yashwanthkumarsuruneni/HU-ANLY-530-90-O', 'size': '9634'}