Rajeek8 12
Rajeek8 12
Aim:
To write an python program on data and text clustering using k means clustering.
Algorithm:
Step 5: Assign each data point to their closest centroid, which will form the predefined K
clusters.
Step 6: Calculate the variance and place a new centroid of each cluster.
Step 7: Repeat the third steps, which means reassign each datapoint to the new closest
centroid of each cluster.
Implementation:
import numpy as np
import pandas as pd
dataset.head()
y_predict= kmeans.fit_predict(x)
mtp.scatter(x[y_predict == 0, 0], x[y_predict == 0, 1], s = 100, c = 'blue', label = 'Cluster 1') #for first
cluster
mtp.scatter(x[y_predict == 1, 0], x[y_predict == 1, 1], s = 100, c = 'green', label = 'Cluster 2') #for
second cluster
mtp.scatter(x[y_predict== 2, 0], x[y_predict == 2, 1], s = 100, c = 'red', label = 'Cluster 3') #for third
cluster
mtp.scatter(x[y_predict == 3, 0], x[y_predict == 3, 1], s = 100, c = 'cyan', label = 'Cluster 4') #for
fourth cluster
mtp.scatter(x[y_predict == 4, 0], x[y_predict == 4, 1], s = 100, c = 'magenta', label = 'Cluster 5') #for
fifth cluster
mtp.title('K-Means Cluster')
mtp.xlabel('Market spend')
mtp.ylabel('admin')
mtp.legend()
mtp.show()
Aim:
To write a python programming for Data and Text clustering using Gaussian Mixture
models.
Algorithm:
Step4: Initialize the mean, the covariance matrix and the mixing coefficients by some random
values.
Step6: Again, estimate all the parameters using the current ck values.
Implementation:
Bagging:
import numpy as np
import pandas as pd
OUTPUT:
scaler = StandardScaler()
import pandas as pd
numerical_features = raw_df.select_dtypes(include=['number']).columns
numerical_df = raw_df[numerical_features]
scaler = StandardScaler()
scaled_df = scaler.fit_transform(numerical_df)
raw_df[numerical_features] = scaled_df
pca = PCA(n_components = 2)
X_principal = pca.fit_transform(numerical_df)
X_principal = pd.DataFrame(X_principal)
X_principal.head(2)
gmm=GaussianMixture(n_components = 3)
gmm.fit(X_principal)
alpha = 0.6)
plt.show()
dx=np.argsort(arr)[:X]
return arr[dx]
n_clusters = np.arange(2, 8)
sils = []
sils_err = []
iterations = 20
for n in n_clusters:
for _ in range(iterations):
labels = gmm.predict(X_principal)
tmp_sil.append(sil)
sils.append(val)
sils_err.append(err)
plt.xticks(n_clusters)
plt.xlabel("N. of clusters")
plt.ylabel("Score")
plt.show()
OUTPUT:
Result:
Thus, the python program on data and text clustering using Gaussian mixture model has been verified
and executed successfully.
Aim:
To write a python programming for Dimensionality reductional algorithms using
Image processing Applications.
Algorithm:
BEGIN:
END
Implementation:
import numpy as np
import pandas as pd
X=np.load('/content/X.npy')
Y=np.load('/content/Y.npy')
X.shape
plt.imshow(X[0])
random_state=1)
clf.fit(X_train, y_train)
y_hat = clf.predict(X_test)
pca_dims = PCA()
cumsum = np.cumsum(pca_dims.explained_variance_ratio_)
print(d)
pca = PCA(n_components=d)
X_reduced = pca.fit_transform(X_train)
X_recovered = pca.inverse_transform(X_reduced)
f = plt.figure()
f.add_subplot(1,2, 1)
plt.title("original")
plt.imshow(X_train[0].reshape((64,64)))
f.add_subplot(1,2, 2)
plt.title("PCA compressed")
plt.imshow(X_recovered[0].reshape((64,64)))
plt.show(block=True)
20, 20))
clf_reduced.fit(X_reduced, y_train)
X_test_reduced = pca.transform(X_test)
y_hat_reduced = clf_reduced.predict(X_test_reduced)
Result:
Thus, the implementation of python programming for Dimensionality reduction algorithms using
Image processing Applications has been completed and verified successfully.
Aim:
To write a python programming for Implementation of Sampling methods.
Algorithm:
BEGIN:
Step 4: Run iterative simulations by generating enough possible values for independent
Variables.
END
Implementation:
%matplotlib inline
import numpy as np
import numpy.linalg as LA
sig_inv = LA.inv(sig)
x = np.linspace(-3, 3, 1000)
X = np.array(np.meshgrid(x, x)).transpose(1, 2, 0)
plt.xlabel('$x_1$')
import numpy as np
x0 = [0, 0]
xt = x0
b = 0.8
samples = []
for i in range(100000):
xt = [x1_t, x2_t]
samples.append(xt)
burn_in = 1000
plt.show()
%matplotlib inline
import numpy as np
Z = 10.0261955464
y_vals = P(x_vals)
plt.figure(1)
plt.show()
f_x = lambda x: x
true_expected_fx = 10.02686647165
true_expected_gx = -1.15088010640
a, b = -4, 8
uniform_prob = 1./(b - a)
plt.figure(2)
plt.xlim(-4, 10)
plt.ylim(-1, 3.5)
plt.show()
expected_g_x = 0.
n_samples = 100000
den = 0.
for i in range(n_samples):
sample = np.random.uniform(a, b)
den += importance
expected_f_x /= den
expected_g_x /= den
expected_f_x *= Z
expected_g_x *= Z
Aim:
To write a python programming for Implementing the application of Hidden Markov
model for weather prediction.
Algorithm:
BEGIN:
END
Implementation:
import numpy as np
T=3
for t in range(T):
if t == 0:
else:
print(forecast)
P_hidden_init = P_transition[1, :]
alpha = np.zeros((3,))
for i in range(3):
alpha[i] = P_emission[i, 0] * P_init[i] # Indented this line to be part of the 'for' loop
P_evidence = np.sum(alpha)
for i in range(3):
most_likely_weather = state_names[np.argmax(posteriors)]
alpha = np.zeros((3,))
for i in range(3):
alpha[i] = P_emission[i, 0] * P_init[i] # Indented this line to be part of the 'for' loop
final_state = np.argmax(alpha)
most_likely_weather = state_names[final_state]
E = [0, 1, 2, 2, 1, 0]
T = len(E)
for i in range(3):
for j in range(3):
backpointers[t - 1, j] = np.argmax(prob_transitions)
hidden_states = [np.argmax(trellis[-1])]
hidden_states.append(backpointers[i, hidden_states[-1]])
hidden_states.reverse()
Result:
Thus, the implementation of python programming for Implementing the application of Hidden
Markov Model for weather prediction has been completed and verified successfully..