AIML Lab 7 8 9 10
AIML Lab 7 8 9 10
import numpy as np
data = load_iris()
X = data.data # Features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
labels = agg_clust.fit_predict(X_scaled)
plt.figure(figsize=(10, 7))
dendrogram(linked, labels=data.target_names[labels])
plt.ylabel('Distance')
plt.show()
Output :
8. Write a program to implement Support Vector Machine algorithm
import numpy as np
data = load_iris()
X = data.data # Features
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
# Step 7: Visualize the decision boundary (Only using the first two features for simplicity)
plt.figure(figsize=(8, 6))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.colorbar()
plt.show()
Output:
9. Write a program to implement clustering using random forest algorithm using
an appropriate dataset.
import numpy as np
data = load_iris()
X = data.data # Features
rf_model.fit(X_train, y_train)
# Step 4: Make predictions on the test set
y_pred = rf_model.predict(X_test)
# Step 6: Visualize the clusters (use the first two features for simplicity)
plt.figure(figsize=(8, 6))
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.colorbar(label='Predicted Cluster')
plt.show()
OUTPUT:
url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv'
plt.figure(figsize=(10,6))
plt.plot(data)
plt.xlabel('Date')
plt.ylabel('Number of Passengers')
plt.show()
plot_acf(train, lags=40)
plot_pacf(train, lags=40)
plt.show()
# Step 5: Fit the ARIMA model
# ARIMA(p, d, q) where p is the number of lag observations included in the model (AR), d is the number
of times the raw observations are differenced,
model = ARIMA(train, order=(5, 1, 0)) # (p=5, d=1, q=0) - Adjust these parameters based on your ACF
and PACF plots
model_fit = model.fit()
forecast = model_fit.forecast(steps=len(test))
plt.figure(figsize=(10,6))
plt.plot(train, label='Train')
plt.plot(test, label='Test')
plt.xlabel('Date')
plt.ylabel('Number of Passengers')
plt.legend()
plt.show()