<jupyter_start><jupyter_text>What if Data is imabalanced1. As a part of this task you will observe how linear models work in case of data imbalanced2. observe how hyper plane is changs according to change in your learning rate.3. below we have created 4 random datasets which are linearly separable and having class imbalance4. in the first dataset the ratio between positive and negative is 100 : 2, in the 2nd data its 100:20, in the 3rd data its 100:40 and in 4th one its 100:80<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns

from sklearn.datasets import make_classification

from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
data1 = make_classification(n_samples=102,n_features=2,n_informative=2,n_redundant=0,n_classes=2,n_clusters_per_class=1,weights=[0.98],class_sep=1.5,random_state=57)
df1 = pd.DataFrame(data1[0])
df1['y'] = data1[1]
sns.scatterplot(x=df1[0],y=df1[1],hue=df1['y'])
data = make_classification(n_samples=120,n_features=2,n_informative=2,n_redundant=0,n_classes=2,n_clusters_per_class=1,weights=[0.8333],class_sep=1.55,random_state=10)
df = pd.DataFrame(data[0])
df['y'] = data[1]
sns.scatterplot(x=df[0],y=df[1],hue=df['y'])<jupyter_output><empty_output><jupyter_text>`I the above case we can create an dataset of two classes but, fail to provide consistent format, as we can see the class 2 in the first data set is on the east side but in the second case it is on west side. So basically this method will not work.`<jupyter_code>np.random.seed(9)
def sample_generator(one,m1,v1,two,m2,v2):
    x1 = np.random.normal(m1,v1,(one,2))
    y1 = np.zeros((one,1))
    data1 = np.concatenate((x1,y1),axis=1)

    x2 = np.random.normal(m2,v2,(two,2))
    y2 = np.ones((two,1))
    data2 = np.concatenate((x2,y2),axis=1)

    data = np.concatenate((data1,data2),axis=0)
    df = pd.DataFrame(data,columns=['f1','f2','y']).astype({'y': 'int32'})
    return df
plt.rcParams["figure.figsize"] = (25,4)
df1 = sample_generator(100,0,0.05,2,0.13,0.02)
plt.subplot(141)
sns.scatterplot(data=df1,x='f1',y='f2',hue='y')

df2 = sample_generator(100,0,0.05,20,0.13,0.02)
plt.subplot(142)
sns.scatterplot(data=df2,x='f1',y='f2',hue='y')

df3 = sample_generator(100,0,0.05,40,0.13,0.02)
plt.subplot(143)
sns.scatterplot(data=df3,x='f1',y='f2',hue='y')

df4 = sample_generator(100,0,0.05,80,0.13,0.02)
plt.subplot(144)
sns.scatterplot(data=df4,x='f1',y='f2',hue='y')

plt.show()<jupyter_output><empty_output><jupyter_text>Task 1: Applying SVM  RBF<jupyter_code>plt.rcParams["figure.figsize"] = (25,14)
f,axs = plt.subplots(3,4)
i=0
for c in [0.001, 1, 100]:
    j=0
    for df in [df1,df2,df3,df4]:
        svc = SVC(C=c,kernel='rbf').fit(df[['f1','f2']],df['y'])

        # create a mesh to plot in
        x_min, x_max = df.f1.min() - 0.05, df.f1.max() + 0.05
        y_min, y_max = df.f2.min() - 0.05, df.f2.max() + 0.05
        xx2, yy2 = np.meshgrid(np.arange(x_min, x_max, .02),np.arange(y_min, y_max, .02))
        Z = svc.predict(np.c_[xx2.ravel(), yy2.ravel()])
        Z = Z.reshape(xx2.shape)
        ax = axs[i][j]
        ax.contourf(xx2, yy2, Z, cmap=plt.cm.coolwarm, alpha=0.2)
        ax.scatter(df.f1, df.f2, c=df.y, cmap=plt.cm.coolwarm, s=25)
        ax.axis([x_min, x_max,y_min, y_max])
        j=j+1
    i=i+1
plt.show()<jupyter_output><empty_output><jupyter_text>Linear SVM    with axes bounded<jupyter_code>plt.rcParams["figure.figsize"] = (25,14)
f,axs = plt.subplots(3,4)
i=0
for c in [0.001, 1, 100]:
    j=0
    for df in [df1,df2,df3,df4]:
        svc = SVC(C=c,kernel='linear').fit(df[['f1','f2']],df['y'])

        # create a mesh to plot in
        x_min, x_max = df.f1.min() - 0.05, df.f1.max() + 0.05
        y_min, y_max = df.f2.min() - 0.05, df.f2.max() + 0.05
        xx2, yy2 = np.meshgrid(np.arange(x_min, x_max, .02),np.arange(y_min, y_max, .02))
        Z = svc.predict(np.c_[xx2.ravel(), yy2.ravel()])
        Z = Z.reshape(xx2.shape)

        #Plotting Line
        w = svc.coef_[0]
        a = -w[0] / w[1]
        xx = np.linspace(x_min, x_max)
        yy = a * xx - (svc.intercept_[0]) /w[1]

        #Plotting
        ax = axs[i][j]
        ax.contourf(xx2, yy2, Z, cmap=plt.cm.coolwarm, alpha=0.2)
        ax.scatter(df.f1, df.f2, c=df.y, cmap=plt.cm.coolwarm, s=25)
        ax.axis([x_min, x_max,y_min, y_max])
        ax.plot(xx,yy,c='y')
        j=j+1
    i=i+1
plt.show()<jupyter_output><empty_output><jupyter_text>Linear SVM     without bounded axis<jupyter_code>plt.rcParams["figure.figsize"] = (25,14)
f,axs = plt.subplots(3,4)
i=0
for c in [0.001, 1, 100]:
    j=0
    for df in [df1,df2,df3,df4]:
        svc = SVC(C=c,kernel='linear').fit(df[['f1','f2']],df['y'])

        # create a mesh to plot in
        x_min, x_max = df.f1.min() - 0.1, df.f1.max() + 0.15
        y_min, y_max = df.f2.min() - 0.5, df.f2.max() + 0.1

        #Plotting Line
        w = svc.coef_[0]
        a = -w[0] / w[1]
        xx = np.linspace(x_min, x_max)
        yy = a * xx - (svc.intercept_[0]) /w[1]

        #prediction
        xx2, yy2 = np.meshgrid(np.arange(x_min, x_max, .02),np.arange(y_min, y_max+(yy.max()*1.1), .02))
        Z = svc.predict(np.c_[xx2.ravel(), yy2.ravel()])
        Z = Z.reshape(xx2.shape)

        #Plotting
        ax = axs[i][j]
        ax.contourf(xx2, yy2, Z, cmap=plt.cm.coolwarm, alpha=0.2)
        ax.scatter(df.f1, df.f2, c=df.y, cmap=plt.cm.coolwarm, s=25)
        # ax.axis([x_min, x_max,y_min, y_max])
        ax.plot(xx,yy,c='y')
        j=j+1
    i=i+1
plt.show()<jupyter_output><empty_output><jupyter_text>Task 2: Applying LR    with bounded axis<jupyter_code>plt.rcParams["figure.figsize"] = (25,14)
f,axs = plt.subplots(3,4)
i=0
for c in [0.001, 1, 100]:
    j=0
    for df in [df1,df2,df3,df4]:
        logiclf = LogisticRegression(C=c,fit_intercept=True,random_state=99,n_jobs=-1).fit(df[['f1','f2']],df['y'])

        # create a mesh to plot in
        x_min, x_max = df.f1.min() - 0.05, df.f1.max() + 0.05
        y_min, y_max = df.f2.min() - 0.05, df.f2.max() + 0.05
        xx2, yy2 = np.meshgrid(np.arange(x_min, x_max, .02),np.arange(y_min, y_max, .02))
        Z = logiclf.predict(np.c_[xx2.ravel(), yy2.ravel()])
        Z = Z.reshape(xx2.shape)

        #Plotting Line
        w = logiclf.coef_[0]
        a = -w[0] / w[1]
        xx = np.linspace(x_min, x_max)
        yy = a * xx - (logiclf.intercept_[0]) /w[1]

        #Plotting
        ax = axs[i][j]
        ax.contourf(xx2, yy2, Z, cmap=plt.cm.coolwarm, alpha=0.2)
        ax.scatter(df.f1, df.f2, c=df.y, cmap=plt.cm.coolwarm, s=25)
        ax.axis([x_min, x_max,y_min, y_max])
        ax.plot(xx,yy,c='y')
        j=j+1
    i=i+1
plt.show()<jupyter_output><empty_output><jupyter_text>LR    without bounded axis<jupyter_code>plt.rcParams["figure.figsize"] = (25,18)
f,axs = plt.subplots(4,4)
i=0
for c in [0.001, 1,100, 100000000]: # the last causes the lambda to almost become zero hence no regularization
    j=0
    for df in [df1,df2,df3,df4]:
        logiclf = LogisticRegression(C=c,fit_intercept=True,random_state=99,n_jobs=-1).fit(df[['f1','f2']],df['y'])

        # create a mesh to plot in
        x_min, x_max = df.f1.min() - 0.1, df.f1.max() + 0.15
        y_min, y_max = df.f2.min() - 0.5, df.f2.max() + 0.1

        #Plotting Line
        w = logiclf.coef_[0]
        a = -w[0] / w[1]
        xx = np.linspace(x_min, x_max)
        yy = a * xx - (logiclf.intercept_[0]) /w[1]

        #prediction
        xx2, yy2 = np.meshgrid(np.arange(x_min, x_max, .02),np.arange(y_min, y_max+(yy.max()*1.1), .02))
        Z = logiclf.predict(np.c_[xx2.ravel(), yy2.ravel()])
        Z = Z.reshape(xx2.shape)

        #Plotting
        ax = axs[i][j]
        ax.contourf(xx2, yy2, Z, cmap=plt.cm.coolwarm, alpha=0.2)
        ax.scatter(df.f1, df.f2, c=df.y, cmap=plt.cm.coolwarm, s=25)
        # ax.axis([x_min, x_max,y_min, y_max])
        ax.plot(xx,yy,c='y')
        j=j+1
    i=i+1
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18095357276936552, 'fraction_numerical': 0.06494806657489675, 'mean_word_length': 3.1603331598125974, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '246146', 'license': 'Unlicense', 'n_tokens_mistral': 3832, 'n_tokens_neox': 3563, 'n_words': 644, 'path': 'ML Notebooks/Behaviour of linear models/Unbalanced-Dataset.ipynb', 'repo_name': 'Sahil-Chavan/ML_Playground', 'size': '7991'}