This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.preprocessing import scale | |
# standardizing each column of data | |
col_standardized = scale(data) | |
# Column means (round to nearest thousandth) | |
col_means = col_standardized.mean(axis=0).round(decimals=3) | |
# Column standard deviations | |
col_stds = col_standardized.std(axis=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.preprocessing import MinMaxScaler | |
zeroOne_scaler = MinMaxScaler() | |
transformed = zeroOne_scaler.fit_transform(data) | |
# try user defined range | |
custom_scaler = MinMaxScaler(feature_range=(-1,1)) | |
transformed = custom_scaler.fit_transform(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.preprocessing import RobustScaler | |
robust_scaler = RobustScaler() | |
transformed = robust_scaler.fit_transform(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.impute import SimpleImputer | |
# perform data imputation using mean values from each column | |
imp_mean = SimpleImputer() | |
transformed = imp_mean.fit_transform(data) | |
imp_median = SimpleImputer(strategy='median') | |
transformed = imp_median.fit_transform(data) | |
imp_frequent = SimpleImputer(strategy='most_frequent') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.decomposition import PCA | |
# assume we have 10 columns in our dataset | |
# the default is m-1, which is 9 in this example | |
pca_obj = PCA() | |
pc = pca_obj.fit_transform(data).round(3) | |
pca_obj = PCA(n_components=7) | |
pc = pca_obj.fit_transform(data).round(3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solve_knapsack(profits, weights, capacity): | |
return knapsack_recursive(profits, weights, capacity, 0) | |
def knapsack_recursive(profits, weights, capacity, currentIndex): | |
# base checks | |
if capacity <= 0 or currentIndex >= len(profits): | |
return 0 | |
# recursive call after choosing the element at the currentIndex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solve_knapsack(profits, weights, capacity): | |
# create a two dimensional array for Memoization, each element is initialized to '-1' | |
dp = [[-1 for x in range(capacity+1)] for y in range(len(profits))] | |
return knapsack_recursive(dp, profits, weights, capacity, 0) | |
def knapsack_recursive(dp, profits, weights, capacity, currentIndex): | |
# base checks | |
if capacity <= 0 or currentIndex >= len(profits): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solve_knapsack(profits, weights, capacity): | |
n = len(profits) | |
if capacity <= 0 or n == 0 or len(weights) != n: | |
return 0 | |
dp = [[0 for x in range(capacity+1)] for y in range(n)] | |
# populate the capacity = 0 columns, with '0' capacity we have '0' profit | |
for i in range(0, n): | |
dp[i][0] = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def can_partition(num): | |
s = sum(num) | |
# if 's' is a an odd number, we can't have two subsets with same total | |
if s % 2 != 0: | |
return False | |
# we are trying to find a subset of given numbers that has a total sum of 's/2'. | |
s = int(s / 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for each number 'i' | |
create a new set which INCLUDES number 'i' if it does not exceed 'S', and recursively | |
process the remaining numbers | |
create a new set WITHOUT number 'i', and recursively process the remaining numbers | |
return true if any of the above two sets has a sum equal to 'S', otherwise return false |
OlderNewer