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
import numpy as np | |
from sklearn.decomposition import TruncatedSVD | |
A = np.array([[-1, 2, 0], [2, 0, -2], [0, -2, 1]]) | |
print("Original Matrix:") | |
print(A) | |
svd = TruncatedSVD(n_components = 2) | |
A_transf = svd.fit_transform(A) |
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
import numpy as np | |
from numpy.linalg import svd | |
# define your matrix as a 2D numpy array | |
A = np.array([[4, 0], [3, -5]]) | |
U, S, VT = svd(A) | |
print("Left Singular Vectors:") | |
print(U) |
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
# import required libraries | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import cv2 | |
from skimage.color import rgb2gray | |
from scipy import ndimage | |
# read the image | |
img = cv2.imread('1.jpeg') |
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
# create document term matrix for your data | |
# you can use TfidfVectorizer instead of CountVectorizer as well | |
from sklearn.feature_extraction.text import CountVectorizer | |
cvec = CountVectorizer() | |
docTermMat = cvec.fit_transform(data['text'].values) | |
# truncated SVD to preserve 20 topics | |
from sklearn.decomposition import TruncatedSVD | |
lsa = TruncatedSVD(n_components = 20, n_iter = 500) | |
lsa.fit(docTermMat) |
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 TruncatedSVD | |
// say you want to reduce to 2 features | |
svd = TruncatedSVD(n_features = 2) | |
//obtain the transformed data | |
data_transformed = svd.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.decomposition import PCA | |
// say you want to reduce to 2 features | |
pca = PCA(n_components = 2) | |
// obtain transformed data | |
data_transformed = pca.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
# import dataset | |
import pandas as pd | |
data = pd.read_csv('mtcars.csv') | |
# remove string and categorical variables | |
cat_var = ['model', 'cyl', 'vs', 'am', 'gear', 'carb'] | |
data = data.drop(cat_var, axis = 1) | |
# scale the variables to prevent coefficients from becoming too large or too small | |
from sklearn.preprocessing import MinMaxScaler |
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
import statsmodels.api as sm | |
model = sm.OLS(y, X[:, 4]).fit() | |
model.summary() |
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.datasets import make_regression | |
X, y = make_regression(n_samples = 20, n_features = 6, random_state = 2, noise = 0.5) |
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.tree import DecisionTreeClassifier | |
clf_gini = DecisionTreeClassifier(criterion = 'gini', random_state = 33) | |
clf_gini.fit(X, Y) |