<jupyter_start><jupyter_text>Problem description:![Capture.PNG](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0kAAAFXCAYAAAB3OgWCAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAPLDSURBVHhe7P2Hf1TV3r8PP//G8/x+3/t73+d4zpHeqwKiUu0gdrEgAiKCCjYsWFDsXSG9994TSoCEUNJ778nMZPpMer2etScDBEQFhZx47s91Xu/XIbusvdbae+K6svbe8/9BEARBEARBEARBuIBIkiAIgiAIgiAIwhhEkgRBEARBEARBEMYgkiQIgiAIgiAIgjAGkSRBEARBEARBEIQxiCQJgiAIgiAIgiCMQSRJEARBEARBEARhDCJJgiAIgiAIgiAIYxBJEgRBEARBEARBGINIkiAIgiAIgiAIwhhEkgRBEARBEARBEMYgkiQIgiAIgiAIgjAGkSRBEARBEARBEIQxiCQJgiAIgiAIgiCMQSRJEARBEARBEARhDH9Kkhrq6zl25ChHMg5LJJKrzPHMzCsul0gkEolEIpFcfSrKy+nr63ObyfXlT0nSoYMHuXXRYhbMmSuRSK4yD61/kMULFl5xnUQikUgkEonk6vLRBx9gMpncZnJ9+VOSlJeXj6+3D16eXhKJ5CoTEhxMUECgRCKRSCQSieRP5HRODj09PW4zub78KUnq6uqmw2iio8MokUiuMmaLRX12uuju7pZIJBKJRCKR/MH09/czMjLiNpPry5+SpL6+fpydXRKJ5BrS3dNzwz7QgiAIgiAIwp9HJEkiGeeIJAmCI<KEY>)<jupyter_code>#Data Import, function to ignore feature and missing values

import numpy as np
import pandas as pd
import io
import requests
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data"
s=requests.get(url).content

features = ['symboling','normalized_losses','make','fuel_type','aspiration',
            'num_of_doors','body_style','drive_wheels','engine_location',
            'wheel_base','length','width','height','curb_weight',
            'engine_type','num_of_cylinders','engine_size','fuel_system',
            'bore','stroke','compression_ratio','horsepower','peak_rpm',
            'city_mpg','highway_mpg','price']

def load_data():
    "" "Returns dataset replaced with null values('?') to np.NaN """
    data=pd.read_csv(io.StringIO(s.decode('utf-8')),names=features)
    data = data.replace('?', np.NaN)
    return data

def apply_specified_condition(data):
    """
    Missing values: denoted by quotation marks (‘?’). Skip data samples with missing values in the target.
    Features to ignore: ‘symboling’
    """
    not_null_inTarget = data[data.normalized_losses.notnull()]
    Imp_feature = not_null_inTarget.drop("symboling", axis=1)
    return Imp_feature

#Exploration of data:
data = apply_specified_condition(load_data())
data.head()

data.info()
data.describe()
# Null check in dataset apart from target
data[np.any(data[data.columns[2:]].isnull(), axis=1)]
# Kernal density estimation

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

def plot_target_feature(data):
    plt.figure()
    sns.distplot(pd.to_numeric(data.normalized_losses), rug=True, kde=True)
    plt.title("Normalized Losses")
    plt.show()

plot_target_feature(data)
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import StandardScaler, LabelBinarizer
import pandas as pd
from sklearn.impute import SimpleImputer

num_features = ['num_of_doors', 'wheel_base', 'length', 'width', 'height',
                'curb_weight', 'num_of_cylinders', 'engine_size', 'bore', 'stroke', 'compression_ratio',
                'horsepower', 'peak_rpm', 'city_mpg', 'highway_mpg', 'price']
cat_features = ['make', 'fuel_type', 'aspiration', 'body_style', 'drive_wheels', 'engine_location',
               'engine_type', 'fuel_system']


class DataFrameSelector(BaseEstimator, TransformerMixin):

    def __init__(self, attribute_names):
        self.attribute_names = attribute_names

    def fit(self, X, y=None):
        return self

    def transform(self, X, y=None):
        return X.loc[:, self.attribute_names].values


class ConvertWordToNum(BaseEstimator, TransformerMixin):

    def __init__(self):
        pass

    def fit(self, X, y=None):
        return self

    def transform(self, X, y=None):
        X = X.copy()
        mapping_num_of_doors = pd.Series([2, 4], index=['two', 'four'])
        mapping_num_of_cylinders = pd.Series([2, 3, 4, 5, 6, 8, 12], index=['two', 'three', 'four', 'five', 'six',
                                                                            'eight', 'twelve'])
        X.loc[:, 'num_of_doors'] = X.loc[:, 'num_of_doors'].map(mapping_num_of_doors)
        X.loc[:, 'num_of_cylinders'] = X.loc[:, 'num_of_cylinders'].map(mapping_num_of_cylinders)

        return X


class DtypeConverter(BaseEstimator, TransformerMixin):

    def __init__(self):
        pass

    def fit(self, X, y=None):
        return self

    def transform(self, X, y=None):

        for col in X.columns:
            if col in num_features:
                X.loc[:, col] = pd.to_numeric(X.loc[:, col])

        return X


class Categorizer(BaseEstimator, TransformerMixin):

    def __init__(self):
        pass

    def fit(self, X, y=None):
        return self

    def transform(self, X, y=None):
        X = X.copy()

        for col in X.columns:
            X.loc[:, col] = X.loc[:, col].astype('category')

        return X


num_pipeline = Pipeline([
    ('WordToNum', ConvertWordToNum()),
    ('DtypeCV', DtypeConverter()),
    ('selector', DataFrameSelector(num_features)),
    ('Imputer', SimpleImputer(strategy="median")),
    ('StdScaler', StandardScaler()),
])


def full_pipeline_encoder(X, X_train, X_test, y_train, y_test):
    X_train_num = pd.DataFrame(num_pipeline.fit_transform(X_train), index=X_train.index,
                               columns=X_train[num_features].columns)
    X_test_num = pd.DataFrame(num_pipeline.transform(X_test), index=X_test.index,
                              columns=X_test[num_features].columns)

    X_OHE = pd.get_dummies(X[cat_features])
    X_train_ohe = X_OHE.loc[X_train.index]
    X_test_ohe = X_OHE.loc[X_test.index]

    X_train = pd.concat([X_train_num, X_train_ohe], axis=1)
    X_test = pd.concat([X_test_num, X_test_ohe], axis=1)

    return X_train, X_test, y_train, y_test
# Data Preparation

from sklearn.model_selection import train_test_split
random_state = 1

# -------------------- [Data Loading] --------------------
def split_X_y(data):
    y = pd.to_numeric(data["normalized_losses"])
    X = data.drop("normalized_losses", axis=1)
    return X, y

X, y = split_X_y(data)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=random_state)

# ------------------ [Data Preparation] ------------------
X_train_stdby, X_test_stdby, y_train_stdby, y_test_stdby = full_pipeline_encoder(X, X_train, X_test, y_train, y_test)
# DataFrame to Numpy
X_tr = X_train_stdby.to_numpy()
X_te = X_test_stdby.to_numpy()
y_tr = y_train_stdby.to_numpy()
y_te = y_test_stdby.to_numpy()

import numpy as np
import numpy.linalg as npl
import scipy.linalg as spl
import math
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [6, 6]

rms = lambda x: (sum(x**2)**0.5)/(len(x)**0.5)
N = len(X_tr)
a = np.vstack(np.ones(N))
X = np.hstack((a,X_tr))
theta = npl.lstsq(X,y_tr)[0]
plt.plot(np.matmul(X,theta).transpose())
plt.xlabel('Training_datapoint')
plt.ylabel('Normalised Loss')
plt.plot(y_tr)
plt.legend(['Predicted Normalised Loss', 'Actual Normalised Loss'], loc='upper right')
rms(np.matmul(X,theta)-y_tr)
plt.scatter(y_tr,np.matmul(X,theta), edgecolor='k')
plt.plot(range(200),range(200),'r:')
plt.xlabel('Actual Normalised Loss')
plt.ylabel('Predicted Normalised Loss')
plt.show()

import cvxpy as cp
import numpy as np
import matplotlib.pyplot as plt

def loss_fn(X, Y, beta):
    return cp.norm2(X @ beta - Y)**2

def regularizer(beta):
    return cp.norm1(beta)

def objective_fn(X, Y, beta, lambd):
    return loss_fn(X, Y, beta) + lambd * regularizer(beta)

def mse(X, Y, beta):
    return (1.0 / X.shape[0]) * loss_fn(X, Y, beta).value<jupyter_output><empty_output><jupyter_text>Selecting important features through lasso regression(sparsity pursuit).<jupyter_code>d=60 #(feature_column)
theta_cvx = cp.Variable(d);
loss = cp.norm2(X_tr*theta_cvx-y_tr);

lambd_values = np.logspace(-2, 3, 10)
train_errors = []
test_errors = []
beta_values = []
for v in lambd_values:
    lambd_values = v
    regl = lambd_values*cp.norm1(theta_cvx[1:16]);
    problem = cp.Minimize(loss+regl);
    prob = cp.Problem(problem)
    prob.solve()
    train_errors.append(mse(X_tr.astype(int), y_tr, theta_cvx))
    test_errors.append(mse(X_te, y_te, theta_cvx))
    beta_values.append(theta_cvx.value)

x = np.linspace(1,d)
print(theta_cvx.value)
plt.stem(theta_cvx.value)
plt.xlabel(r"$\lambda$", fontsize=16)
plt.ylabel(r"$\theta-magnitude$", fontsize=16)
plt.title("Sparsity_pursuit")
plt.show()


%matplotlib inline
%config InlineBackend.figure_format = 'svg'

def plot_train_test_errors(train_errors, test_errors, beta_values):
    plt.plot(beta_values, train_errors, label="Train error")
    plt.plot(beta_values, test_errors, label="Test error")
    plt.xscale("log")
    #plt.legend(loc="upper left")
    plt.xlabel(r"$\lambda$", fontsize=16)
    plt.title("Mean Squared Error (MSE)")
    plt.show()

plot_train_test_errors(train_errors, test_errors, beta_values)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1375335720680394, 'fraction_numerical': 0.01566696508504924, 'mean_word_length': 4.416363636363636, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '164036', 'license': 'MIT', 'n_tokens_mistral': 3570, 'n_tokens_neox': 3408, 'n_words': 575, 'path': 'Simple_regression_model.ipynb', 'repo_name': 'nitish-01/optimisation', 'size': '91150'}