moving files
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
cancer = load_breast_cancer()
|
||||
import pandas as pd
|
||||
|
||||
cancerpd = pd.DataFrame(cancer.data, columns=cancer.feature_names)
|
||||
|
||||
|
||||
import seaborn as sns
|
||||
correlation_matrix = cancerpd.corr().round(1)
|
||||
# use the heatmap function from seaborn to plot the correlation matrix
|
||||
# annot = True to print the values inside the square
|
||||
sns.heatmap(data=correlation_matrix, annot=True)
|
||||
EigValues, EigVectors = np.linalg.eig(correlation_matrix)
|
||||
print(EigValues)
|
||||
|
||||
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
|
||||
print(X_train.shape)
|
||||
print(X_test.shape)
|
||||
|
||||
logreg = LogisticRegression()
|
||||
logreg.fit(X_train, y_train)
|
||||
print("Train set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_train,y_train)))
|
||||
|
||||
from sklearn.preprocessing import MinMaxScaler, StandardScaler
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
print("Train set accuracy scaled data: {:.2f}".format(logreg.score(X_train_scaled,y_train)))
|
||||
|
||||
#thereafter we do a PCA with Scikit-learn
|
||||
from sklearn.decomposition import PCA
|
||||
pca = PCA(n_components = 2)
|
||||
X2D_train = pca.fit_transform(X_train_scaled)
|
||||
|
||||
logreg.fit(X2D_train,y_train)
|
||||
print("Train set accuracy scaled and PCA data: {:.2f}".format(logreg.score(X2D_train,y_train)))
|
||||
@@ -1,31 +0,0 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from IPython.display import display
|
||||
np.random.seed(100)
|
||||
# setting up a 10 x 5 matrix
|
||||
rows = 10
|
||||
cols = 5
|
||||
X = np.random.randn(rows,cols)
|
||||
df = pd.DataFrame(X)
|
||||
# Pandas does the centering for us
|
||||
df = df -df.mean()
|
||||
display(df)
|
||||
|
||||
# we center it ourselves
|
||||
X_centered = X - X.mean(axis=0)
|
||||
print(X_centered-df)
|
||||
#Now we do an SVD
|
||||
U, s, V = np.linalg.svd(X_centered)
|
||||
c1 = V.T[:, 0]
|
||||
c2 = V.T[:, 1]
|
||||
W2 = V.T[:, :2]
|
||||
X2D = X_centered.dot(W2)
|
||||
print(X2D)
|
||||
#thereafter we do a PCA with Scikit-learn
|
||||
from sklearn.decomposition import PCA
|
||||
pca = PCA(n_components = 2)
|
||||
X2D = pca.fit_transform(X)
|
||||
print(X2D)
|
||||
|
||||
print(pca.components_.T[:, 0])
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.svm import SVC
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
cancer = load_breast_cancer()
|
||||
|
||||
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
|
||||
print(X_train.shape)
|
||||
print(X_test.shape)
|
||||
|
||||
logreg = LogisticRegression()
|
||||
logreg.fit(X_train, y_train)
|
||||
|
||||
#svm = SVC(C=100)
|
||||
#svm.fit(X_train, y_train)
|
||||
print("Test set accuracy: {:.2f}".format(logreg.score(X_test,y_test)))
|
||||
|
||||
from sklearn.preprocessing import MinMaxScaler, StandardScaler
|
||||
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
print("Feature min values before scaling:\n {}".format(X_train.min(axis=0)))
|
||||
print("Feature max values before scaling:\n {}".format(X_train.max(axis=0)))
|
||||
|
||||
print("Feature min values before scaling:\n {}".format(X_train_scaled.min(axis=0)))
|
||||
print("Feature max values before scaling:\n {}".format(X_train_scaled.max(axis=0)))
|
||||
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
#svm.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
||||
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
#svm.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
||||
@@ -1,40 +0,0 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
cancer = load_breast_cancer()
|
||||
|
||||
fig, axes = plt.subplots(15,2,figsize=(10,20))
|
||||
malignant = cancer.data[cancer.target == 0]
|
||||
benign = cancer.data[cancer.target == 1]
|
||||
ax = axes.ravel()
|
||||
|
||||
for i in range(30):
|
||||
_, bins = np.histogram(cancer.data[:,i], bins =50)
|
||||
ax[i].hist(malignant[:,i], bins = bins, alpha = 0.5)
|
||||
ax[i].hist(benign[:,i], bins = bins, alpha = 0.5)
|
||||
ax[i].set_title(cancer.feature_names[i])
|
||||
ax[i].set_yticks(())
|
||||
ax[0].set_xlabel("Feature magnitude")
|
||||
ax[0].set_ylabel("Frequency")
|
||||
ax[0].legend(["Malignant", "Benign"], loc ="best")
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
|
||||
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
|
||||
print(X_train.shape)
|
||||
print(X_test.shape)
|
||||
|
||||
logreg = LogisticRegression()
|
||||
logreg.fit(X_train, y_train)
|
||||
print("Test set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
|
||||
|
||||
from sklearn.preprocessing import MinMaxScaler, StandardScaler
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
||||
@@ -1,44 +0,0 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
cancer = load_breast_cancer()
|
||||
|
||||
fig, axes = plt.subplots(15,2,figsize=(10,20))
|
||||
malignant = cancer.data[cancer.target == 0]
|
||||
benign = cancer.data[cancer.target == 1]
|
||||
ax = axes.ravel()
|
||||
|
||||
for i in range(30):
|
||||
_, bins = np.histogram(cancer.data[:,i], bins =50)
|
||||
ax[i].hist(malignant[:,i], bins = bins, alpha = 0.5)
|
||||
ax[i].hist(benign[:,i], bins = bins, alpha = 0.5)
|
||||
ax[i].set_title(cancer.feature_names[i])
|
||||
ax[i].set_yticks(())
|
||||
ax[0].set_xlabel("Feature magnitude")
|
||||
ax[0].set_ylabel("Frequency")
|
||||
ax[0].legend(["Malignant", "Benign"], loc ="best")
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
|
||||
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
|
||||
|
||||
logreg = LogisticRegression()
|
||||
logreg.fit(X_train, y_train)
|
||||
print("Test set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
|
||||
|
||||
|
||||
|
||||
beta = np.random.randn(2,1)
|
||||
|
||||
eta = 0.1
|
||||
Niterations = 100
|
||||
|
||||
for iter in range(Niterations):
|
||||
gradients = 2.0/m*xb.T @ (xb @ (beta)-y)+2*lmbda*beta
|
||||
beta -= eta*gradients
|
||||
|
||||
print(beta)
|
||||
ypredict = xb @ beta
|
||||
ypredict2 = xb @ beta_linreg
|
||||
@@ -1,24 +0,0 @@
|
||||
# Importing various packages
|
||||
import numpy as np
|
||||
n = 100
|
||||
# define two vectors
|
||||
x = np.random.random(size=n)
|
||||
y = 4+3*x+np.random.normal(size=n)
|
||||
#scaling the x and y vectors
|
||||
x = x - np.mean(x)
|
||||
y = y - np.mean(y)
|
||||
variance_x = np.sum(x@x)/n
|
||||
variance_y = np.sum(y@y)/n
|
||||
print(variance_x)
|
||||
print(variance_y)
|
||||
cov_xy = np.sum(x@y)/n
|
||||
cov_xx = np.sum(x@x)/n
|
||||
cov_yy = np.sum(y@y)/n
|
||||
C = np.zeros((2,2))
|
||||
C[0,0]= cov_xx/variance_x
|
||||
C[1,1]= cov_yy/variance_y
|
||||
C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
|
||||
C[1,0]= C[0,1]
|
||||
print(C)
|
||||
Eigvals, Eigvecs = np.linalg.eig(C)
|
||||
print(Eigvals)
|
||||
@@ -1,43 +0,0 @@
|
||||
# Common imports
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
|
||||
def FrankeFunction(x,y):
|
||||
term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
|
||||
term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
|
||||
term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
|
||||
term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
|
||||
return term1 + term2 + term3 + term4
|
||||
|
||||
|
||||
def create_X(x, y, n ):
|
||||
if len(x.shape) > 1:
|
||||
x = np.ravel(x)
|
||||
y = np.ravel(y)
|
||||
|
||||
N = len(x)
|
||||
l = int((n+1)*(n+2)/2) # Number of elements in beta
|
||||
X = np.ones((N,l))
|
||||
|
||||
for i in range(1,n+1):
|
||||
q = int((i)*(i+1)/2)
|
||||
for k in range(i+1):
|
||||
X[:,q+k] = (x**(i-k))*(y**k)
|
||||
|
||||
return X
|
||||
|
||||
|
||||
# Making meshgrid of datapoints and compute Franke's function
|
||||
n = 4
|
||||
N = 1000
|
||||
x = np.sort(np.random.uniform(0, 1, N))
|
||||
y = np.sort(np.random.uniform(0, 1, N))
|
||||
z = FrankeFunction(x, y)
|
||||
X = create_X(x, y, n=n)
|
||||
|
||||
Xpd = pd.DataFrame(X)
|
||||
Xpd = Xpd - Xpd.mean()
|
||||
correlation_matrix = Xpd.cov()
|
||||
print(correlation_matrix)
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
# Common imports
|
||||
import numpy as np
|
||||
from sklearn.neural_network import MLPRegressor
|
||||
from sklearn.metrics import accuracy_score
|
||||
import seaborn as sns
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def FrankeFunction(x,y):
|
||||
term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
|
||||
term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
|
||||
term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
|
||||
term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
|
||||
return term1 + term2 + term3 + term4
|
||||
|
||||
|
||||
def create_X(x, y, n ):
|
||||
if len(x.shape) > 1:
|
||||
x = np.ravel(x)
|
||||
y = np.ravel(y)
|
||||
|
||||
N = len(x)
|
||||
l = int((n+1)*(n+2)/2) # Number of elements in beta
|
||||
X = np.ones((N,l))
|
||||
|
||||
for i in range(1,n+1):
|
||||
q = int((i)*(i+1)/2)
|
||||
for k in range(i+1):
|
||||
X[:,q+k] = (x**(i-k))*(y**k)
|
||||
|
||||
return X
|
||||
|
||||
|
||||
# Making meshgrid of datapoints and compute Franke's function
|
||||
n = 4
|
||||
N = 100
|
||||
x = np.sort(np.random.uniform(0, 1, N))
|
||||
y = np.sort(np.random.uniform(0, 1, N))
|
||||
z = FrankeFunction(x, y)
|
||||
X = create_X(x, y, n=n)
|
||||
|
||||
# only training data, no advanced splitting
|
||||
X_train = X
|
||||
Y_train = z
|
||||
# only one simple layer with 100 neurons
|
||||
n_hidden_neurons = 100
|
||||
epochs = 100
|
||||
# store models for later use
|
||||
eta_vals = np.logspace(-5, 1, 7)
|
||||
lmbd_vals = np.logspace(-5, 1, 7)
|
||||
# store the models for later use
|
||||
DNN_scikit = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)
|
||||
train_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
|
||||
sns.set()
|
||||
for i, eta in enumerate(eta_vals):
|
||||
for j, lmbd in enumerate(lmbd_vals):
|
||||
dnn = MLPRegressor(hidden_layer_sizes=(n_hidden_neurons), activation='logistic',
|
||||
alpha=lmbd, learning_rate_init=eta, max_iter=epochs)
|
||||
dnn.fit(X_train, Y_train)
|
||||
DNN_scikit[i][j] = dnn
|
||||
train_accuracy[i][j] = dnn.score(X_train, Y_train)
|
||||
|
||||
fig, ax = plt.subplots(figsize = (10, 10))
|
||||
sns.heatmap(train_accuracy, annot=True, ax=ax, cmap="viridis")
|
||||
ax.set_title("Training Accuracy")
|
||||
ax.set_ylabel("$\eta$")
|
||||
ax.set_xlabel("$\lambda$")
|
||||
plt.show()
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
# Importing various packages
|
||||
import numpy as np
|
||||
n = 10
|
||||
x = np.random.normal(size=n)
|
||||
x = x - np.mean(x)
|
||||
y = 4+3*x+np.random.normal(size=n)
|
||||
y = y - np.mean(y)
|
||||
X = (np.vstack((x, y))).T
|
||||
print(X)
|
||||
import pandas as pd
|
||||
Xpd = pd.DataFrame(X)
|
||||
print(Xpd)
|
||||
correlation_matrix = Xpd.corr()
|
||||
print(correlation_matrix)
|
||||
|
||||
|
||||
|
||||
variance_x = np.sum(x@x)/n
|
||||
variance_y = np.sum(y@y)/n
|
||||
cov_xy = np.sum(x@y)/n
|
||||
cov_xx = np.sum(x@x)/n
|
||||
cov_yy = np.sum(y@y)/n
|
||||
C = np.zeros((2,2))
|
||||
C[0,0]= cov_xx/variance_x
|
||||
C[1,1]= cov_yy/variance_y
|
||||
C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
|
||||
C[1,0]= C[0,1]
|
||||
print(C)
|
||||
Reference in New Issue
Block a user