0
\$\begingroup\$

I'm a new DS student, and I get the basic concept of Standardisation, whilst I was learning we used StandardScaler in some algorithms, and not in others on the same dataset, and I'm still confused as where and to use it.

I have other categorical features selected for the classifiers. Could you please review my code, and advise best practice for Linear, MultipleLinear, Logistic, KNN, and SVM in relation to Scaling?

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('Ass2Dataset.csv')

X = dataset.iloc[0:50, -4].values
y = dataset.iloc[0:50, -2].values

X = X.reshape(-1,1)
y = y.reshape(-1,1)

from sklearn.impute import SimpleImputer
imputer = SimpleImputer (missing_values=np.nan, strategy='mean')
imputer.fit (X[:])
(X[:]) = imputer.transform((X[:]))
imputer.fit (y[:])
(y[:]) = imputer.transform((y[:]))

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split (X, y, test_size = 0.3, random_state = 0)

## Scaling method used in LogisticRegression, KNN, and SVM algorithms.
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

## LinearRegression method
from sklearn.linear_model import LinearRegression
regressor = LinearRegression ()
regressor.fit (X_train, y_train)

## SVM classification method
from sklearn.svm import LinearSVC
classifier = LinearSVC (random_state = 0, dual=False)
classifier.fit(X_train, y_train)

##KNN method
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier (n_neighbors=5, metric = 'euclidean')

print (regressor.predict([[134000]]))
[[7864.97350536]]

Dataset for SL regression snippet: "Before imputation"

  print (X)
[130000. 130000. 152000. 109000. 136000. 136000. 136000.     nan 131000.
 131000. 108000. 108000. 164000. 164000. 164000. 209000. 209000. 209000.
  61000.  90000.  90000.  90000.  90000.  98000.  90000.  90000.  90000.
  98000. 122000. 156000.  92000.     nan  79000.  92000.  92000.  92000.
  92000. 110000. 110000. 110000. 110000.     nan 110000. 111000.  90000.
  90000. 119000. 258000. 258000. 326000.]
print (y)
[ 9109.  9694.  5344. 10030.  6650.  9863.  9064.  8508.  9591.  6653.
 11344. 11283.  4708.  7643.  4484.  8639.  7251.  5804.  9221.  5856.
  6458.  8993.  6208.    nan  7274.  6657.  6355.  9758.  7628.  7953.
  8565.  8601. 10938.  7032.  9147.  6670.    nan  9820.  8940.  9031.
  6514.  7967.  7972.  7187.  8023.    nan  7193.  8112.  7461.  5440.]

Dataset snippet for LR, KNN, SVM. "Before imputation"

  print (X)
  [['alfa-romero' 2 4 130000.0 9.0]
  ['alfa-romero' 2 4 130000.0 9.0]
  ['alfa-romero' 2 6 152000.0 9.0]
 ...
 ['volvo' 4 6 173000.0 nan]
 ['volvo' 4 6 145000.0 23.0]
 ['volvo' 4 4 141000.0 9.5]]
 print (y)
 ['yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes'
 'yes' 'yes' 'no' 'yes' 'no' 'no' 'yes' 'no' 'yes' 'yes' 'yes' 'yes' 'yes'
 'yes' 'yes' 'no' 'yes' 'no' 'no' 'yes' 'no' 'no' 'no' 'no' 'no' 'no' 'no'
 'no' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'no' 'yes' 'no' 'no' 'yes' 'no'
 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'no' 'yes' 'no' 'no' 'yes' 'no'
 'no' 'no' 'no' 'no' 'no' 'no' 'no' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes'
 'no' 'yes' 'no' 'no' 'yes' 'no' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes'
 'no' 'yes' 'no' 'no' 'yes' 'no' 'no' 'no' 'no' 'no' 'no' 'no' 'no' 'yes'
 'yes' 'yes' 'yes' 'yes' 'yes' 'no' 'yes' 'no' 'no' 'yes' 'no' 'yes' 'yes'
 'yes' 'yes' 'yes' 'yes' 'yes' 'no' 'yes' 'no' 'no' 'yes' 'no' 'no' 'no'
 'no' 'no' 'no' 'no' 'no' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'no' 'yes'
 'no' 'no' 'yes' 'no' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'yes' 'no' 'yes'
 'no' 'no' 'yes' 'no' 'no' 'no' 'no' 'no' 'no' 'no' 'no' 'yes' 'yes' 'yes'
 'yes' 'yes' 'yes' 'no' 'yes' 'no' 'no' 'yes' 'no' 'yes' 'yes' 'yes' 'yes'
 'yes' 'yes' 'yes' 'no' 'yes' 'no' 'no' 'yes' 'no' 'no' 'no' 'no' 'no'
 'no' 'no' 'no' 'yes' 'yes' 'yes' 'yes' 'yes']
\$\endgroup\$
1
  • \$\begingroup\$ I would recommend asking this over at Cross Validated. Code Review is more for feedback on the implementation of your analytics pipeline itself (meaning the Python code itself). For the specific preprocessing steps required for certain classifiers, you definitely want Cross Validated. \$\endgroup\$ Sep 16 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.