<jupyter_start><jupyter_text>1. Load the data from “college.csv” that has attributes collected about private and public colleges for a particular year. We will try to predict the private/public status of the college from other attributes<jupyter_code>data = pd.read_csv('College.csv')
data.head()
data.info()
data.shape
data.describe().transpose()<jupyter_output><empty_output><jupyter_text>2. Use LabelEncoder to encode the target variable in to numerical form and split the data such that 20% of the data is set aside for testing.<jupyter_code>encoder = LabelEncoder()
data['Private'] = encoder.fit_transform(data['Private'])
data['Private'].value_counts()<jupyter_output><empty_output><jupyter_text>3. Fit a linear svm from scikit learn and observe the accuracy. [Hint: Use Linear SVC]<jupyter_code>x = data.drop(['Private'],axis=1)
y = data['Private']

x_train, x_test , y_train, y_test = train_test_split(x,y,train_size=0.8)

print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
linear_svc = LinearSVC()

linear_svc = linear_svc.fit(x_train,y_train)
y_predict = linear_svc.predict(x_test)

print('Accuracy:',accuracy_score(y_test,y_predict))
print('classification report:', classification_report(y_test,y_predict))
print('confusion_matrix:', confusion_matrix(y_test,y_predict))<jupyter_output>Accuracy: 0.8974358974358975
classification report:               precision    recall  f1-score   support

           0       0.93      0.64      0.76        39
           1       0.89      0.98      0.93       117

    accuracy                           0.90       156
   macro avg       0.91      0.81      0.85       156
weighted avg       0.90      0.90      0.89       156

confusion_matrix: [[ 25  14]
 [  2 115]]<jupyter_text>4. Preprocess the data using StandardScalar and fit the same model again and observe the change in accuracy. [Hint: Refer to scikitlearn’s preprocessing methods]<jupyter_code>x.columns
scaler = StandardScaler()
x_scale = scaler.fit_transform(x)
x_scale = pd.DataFrame(x_scale,columns=['Apps', 'Accept', 'Enroll', 'Top10perc', 'Top25perc', 'F.Undergrad',
       'P.Undergrad', 'Outstate', 'Room.Board', 'Books', 'Personal', 'PhD',
       'Terminal', 'S.F.Ratio', 'perc.alumni', 'Expend', 'Grad.Rate'])
x_scale.head()
x_train, x_test , y_train, y_test = train_test_split(x_scale,y,train_size=0.8)

print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
linear_svc = LinearSVC()

linear_svc = linear_svc.fit(x_train,y_train)
y_predict = linear_svc.predict(x_test)

print('Accuracy:',accuracy_score(y_test,y_predict))
print('classification report:', classification_report(y_test,y_predict))
print('confusion_matrix:', confusion_matrix(y_test,y_predict))
# The accuracy of the model has increased significantly from 0.89 to 0.94 after scaling the data. Thus in support vector classifier
# scaling the attributes plays an important role<jupyter_output><empty_output><jupyter_text>5.Use scikit learn’s gridsearch to select the best hyperparameter for a non-linear SVM,identify the model with best score and its parameters.[Hint: Refer to model_selection module of Scikit learn]<jupyter_code># BAse model without hyper parameter tuning:

SVC_base = SVC()

SVC_base = SVC_base.fit(x_train,y_train)
y_predict = SVC_base.predict(x_test)

print('Accuracy:',accuracy_score(y_test,y_predict))
print('classification report:', classification_report(y_test,y_predict))
print('confusion_matrix:', confusion_matrix(y_test,y_predict))
param ={'C': [0.1, 1, 100, 1000],
        'gamma': [0.0001, 0.001, 0.005, 0.1, 1, 3, 5],
        'kernel':['poly', 'rbf', 'sigmoid']
        }

grid = GridSearchCV(estimator=SVC(), param_grid = param , cv=3, scoring='accuracy', verbose=1, n_jobs=-1)

grid_result = grid.fit(x_train, y_train)
best_params = grid_result.best_params_
pred = grid_result.predict(x_test)
cm = confusion_matrix(y_test, pred)
# best parameters:
best_params
pred
cm
# Building the svc model with best hyper parameters:
svc = SVC(C=1,gamma=0.1,kernel='rbf')

svc = svc.fit(x_train,y_train)
y_predict = svc.predict(x_test)

print('Accuracy:',accuracy_score(y_test,y_predict))
print('classification report:', classification_report(y_test,y_predict))
print('confusion_matrix:', confusion_matrix(y_test,y_predict))
# Hyper parameter tuning increases the accuracy of the model<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1190312999771533, 'fraction_numerical': 0.03129997715330135, 'mean_word_length': 5.2542857142857144, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '300879', 'license': 'Apache-2.0', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1501, 'n_words': 353, 'path': 'Module 9/Module_9-Case+study+3.ipynb', 'repo_name': 'Hemanthvijaykumar/Edurekha_Python-For-Datascience', 'size': '4377'}