<jupyter_start><jupyter_text>Bagging using SVM for IMDB movie ratingsThe dataset is obtained from https://www.kaggle.com/karrrimba/movie-metadatacsv/home<jupyter_code># Importing libraries 
from random import seed
from random import randrange
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import statistics as stat
from sklearn import svm
from sklearn.metrics import accuracy_score
# Load a CSV file
df = pd.read_csv("movie_metadata.csv")
print(df.shape)
print(len(df[round(df['imdb_score'])==1.0]))
print(len(df[round(df['imdb_score'])==2.0]))
print(len(df[round(df['imdb_score'])==3.0]))
print(len(df[round(df['imdb_score'])==9.0]))
df=df[round(df['imdb_score'])!=1.0]
df=df[round(df['imdb_score'])!=2.0]
df=df[round(df['imdb_score'])!=3.0]
df=df[round(df['imdb_score'])!=9.0]
df.shape
df.columns
df = df.drop(['movie_title','actor_3_name','content_rating','director_name','genres','language','country','title_year','actor_2_name','actor_1_name','plot_keywords','movie_imdb_link'], axis = 1)
df.head()
df =df.dropna()
print(df.shape)
columnsToEncode = list(df.select_dtypes(include=['category','object']))
le = LabelEncoder()
for feature in columnsToEncode:
    try:
        df[feature] = le.fit_transform(df[feature])
    except:
        print('Error encoding ' + feature)
df.head()
X=df
y=round(X['imdb_score'])
#y.apply(np.round)
X = X.drop(['imdb_score'], axis = 1)
scaler=StandardScaler()
X = scaler.fit_transform(X)
y = np.array(y).astype(int)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3, random_state =1)
clf = svm.SVC()
clf.fit(X_train, y_train)  
clf.predict(X_test)


prediction = clf.predict(X_test)
print(accuracy_score(y_test,prediction))
# Create a random subsample from the dataset with replacement
def subsample(X_train,y_train,ratio):
    X_sample = list()
    y_sample = list()
    n_sample = round(len(X_train) * ratio)

    while len(X_sample) < n_sample:
        index = randrange(len(X_train))
        X_sample.append(X_train[index])
        y_sample.append(y_train[index])
    X_sample_np = np.asarray(X_sample)
    y_sample_np = np.asarray(y_sample) 
    return (X_sample_np,y_sample_np)
 
# Make a prediction with a list of bagged trees
def bagging_predict_soft_voting(models, row):
    #Lets find out what each model predicts 
    pred = list()
    for i in range(len(models)):
        pred.append(models[i].predict_proba(row.reshape(1,-1)))
    finalprob=np.zeros((5,))
    for i in range(len(pred)):
        finalprob=finalprob+pred[i][0]
    final_class = finalprob.argmax(axis=-1) 
    return final_class
 
# Bootstrap Aggregation Algorithm
def bagging(X_train,y_train,X_test,sample_size,n_estimators):
    models = list()
    for i in range(n_estimators):
        X_sample_np,y_sample_np = subsample(X_train,y_train,sample_size)
        model = svm.SVC(probability=True)
        model.fit(X_sample_np, y_sample_np)  
        models.append(model)
    predictions = [bagging_predict_soft_voting(models, row) for row in X_test]
    return(predictions)
predictions=bagging(X_train,y_train,X_test,0.7,1)

total=np.sum([y_test[i]==(predictions[i]+4) for i in range(len(predictions))])

print("Accuracy:",total,"/",len(predictions),"* 100 =","{0:.3f}".format(total/len(predictions)*100),"%")
predictions=bagging(X_train,y_train,X_test,0.7,2)

total=np.sum([y_test[i]==(predictions[i]+4) for i in range(len(predictions))])

print("Accuracy:",total,"/",len(predictions),"* 100 =","{0:.3f}".format(total/len(predictions)*100),"%")
predictions=bagging(X_train,y_train,X_test,0.7,5)

total=np.sum([y_test[i]==(predictions[i]+4) for i in range(len(predictions))])

print("Accuracy:",total,"/",len(predictions),"* 100 =","{0:.3f}".format(total/len(predictions)*100),"%")
predictions=bagging(X_train,y_train,X_test,0.7,10)

total=np.sum([y_test[i]==(predictions[i]+4) for i in range(len(predictions))])

print("Accuracy:",total,"/",len(predictions),"* 100 =","{0:.3f}".format(total/len(predictions)*100),"%")
predictions=bagging(X_train,y_train,X_test,0.7,15)

total=np.sum([y_test[i]==(predictions[i]+4) for i in range(len(predictions))])

print("Accuracy:",total,"/",len(predictions),"* 100 =","{0:.3f}".format(total/len(predictions)*100),"%")
from sklearn.ensemble import BaggingClassifier
model = BaggingClassifier(svm.SVC(),n_estimators=20,random_state=1)
model.fit(X_train, y_train)
model.score(X_test,y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.171189058018972, 'fraction_numerical': 0.020516214427531435, 'mean_word_length': 6.684745762711865, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '505385', 'license': 'MIT', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1726, 'n_words': 271, 'path': 'Ensembling methods/Bagging using SVM for IMDB movie ratings.ipynb', 'repo_name': 'vivekrathi14/Machine-Learning-with-Scikit-Learn-Python-3.x', 'size': '4533'}