You are on page 1of 6

Program1:Decission Tree Regression

Sample Code:
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt

# Generating synthetic data for regression


X, y = make_regression(n_samples=100, n_features=1, noise=0.2, random_state=42)

# Splitting the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Creating a decision tree regressor


tree_regressor = DecisionTreeRegressor(max_depth=3) # You can adjust the depth or
other hyperparameters

# Training the model


tree_regressor.fit(X_train, y_train)

# Predicting on the test set


y_pred = tree_regressor.predict(X_test)

# Calculating performance metrics


mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error: {mse:.2f}")


print(f"R-squared Score: {r2:.2f}")

# Plotting the results


plt.scatter(X_test, y_test, color='black')
plt.scatter(X_test, y_pred, color='blue', linewidth=3)
plt.title('Decision Tree Regressor')
plt.xlabel('X')
plt.ylabel('y')
plt.legend(['Actual', 'Predicted'], loc='upper left')
plt.show()

Out Put:
Mean Squared Error: 41.11
R-squared Score: 0.97
Program 2:RandomForestClassifier
Source Code:
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report

# Generating synthetic data for classification


X, y = make_classification(n_samples=1000, n_features=10, n_classes=2, random_state=42)

# Splitting the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Creating a Random Forest Classifier


random_forest = RandomForestClassifier(n_estimators=100, random_state=42) # You can
adjust n_estimators and other parameters

# Training the model


random_forest.fit(X_train, y_train)

# Predicting on the test set


y_pred = random_forest.predict(X_test)

# Calculating accuracy and printing classification report


accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")

print("Classification Report:")
print(classification_report(y_test, y_pred))
Out Put:
Accuracy: 0.88
Classification Report:
precision recall f1-score support

0 0.84 0.90 0.87 89


1 0.91 0.86 0.89 111

accuracy 0.88 200


macro avg 0.88 0.88 0.88 200
weighted avg 0.88 0.88 0.88 200
Program 3:Logistic Regression
Source code:
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score, classification_report

# Generating a synthetic dataset (you can replace this with your dataset)
X, y = make_classification(n_samples=1000, n_features=5, n_classes=2, random_state=42)

# Splitting the dataset into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Creating a Logistic Regression model


logreg = LogisticRegression()

# Training the model


logreg.fit(X_train, y_train)

# Making predictions
y_pred = logreg.predict(X_test)

# Evaluating the model


accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")

# Printing classification report


print("Classification Report:")
print(classification_report(y_test, y_pred))
Out put:
Accuracy: 0.88
Classification Report:
precision recall f1-score support

0 0.85 0.92 0.88 97


1 0.92 0.84 0.88 103

accuracy 0.88 200


macro avg 0.88 0.88 0.88 200
weighted avg 0.88 0.88 0.88 200

You might also like