ML External Xerox
ML External Xerox
import pandas as pd
ML EXTERNAL ANSWERS 2. import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
1.import pandas as pd
from sklearn.linear_model import LinearRegression # Create a DataFrame with the provided data # Create a DataFrame with the provided data
import numpy as np data = { data = {
'area': [3600, 4000, 4200, 4600, 5000, 5200], 'Gender': ['Male', 'Male', 'Female', 'Female', 'Male', 'Male', 'Female', 'Female'],
# Create a DataFrame with the provided data 'bedroom': [3, 3, 2, 3, 4, 5], 'Age': [19, 35, 26, 35, 45, 30, 30, 50],
data = { 'age': [21, 15, 19, 30, 34, 35], 'Estimated_Salary': [19000, 20000, 50000, 35000, 75000, 30000, 45000, 60000],
'Experience': [1, 2, 3, 4, 5], 'price': [550000, 565000, 610000, 680000, 725000, 810000] 'Purchased': [0, 0, 1, 0, 1, 0, 0, 1]
'Salary': [39343, 43525, 60150, 56957, 93940] } }
}
df = pd.DataFrame(data) df = pd.DataFrame(data)
df = pd.DataFrame(data)
# Split the data into features (X) and target variable (y) # Convert categorical columns to numerical using LabelEncoder
# Split the data into features (X) and target variable (y) X = df[['area', 'bedroom', 'age']] # Features label_encoder = LabelEncoder()
X = df[['Experience']] # Feature (Experience) y = df['price'] # Target variable df['Gender'] = label_encoder.fit_transform(df['Gender'])
y = df['Salary'] # Target variable
# Initialize the Linear Regression model # Split the data into features (X) and target variable (y)
# Initialize the Linear Regression model model = LinearRegression() X = df[['Gender', 'Age', 'Estimated_Salary']] # Features
model = LinearRegression() y = df['Purchased'] # Target variable
# Fit the model with the data
# Fit the model with the data model.fit(X, y) # Initialize the Decision Tree Classifier
model.fit(X, y) clf = DecisionTreeClassifier()
# Predict the price for a new set of features
# Predict the salary for new experience levels new_data = { # Fit the classifier with the data
new_experience = np.array([[6], [7]]) # Predictions for experience level 6 and 7 'area': [4500], clf.fit(X, y)
predicted_salary = model.predict(new_experience) 'bedroom': [4],
'age': [25] # Predict the 'Purchased' for a new data point
for exp, salary in zip(new_experience.flatten(), predicted_salary): } new_data_point = [[label_encoder.transform(['Female'])[0], 35, 50000]] # Example for Female, 35
print(f"Predicted salary for experience {exp}: ${salary:.2f}”) years old, estimated salary 50000
new_df = pd.DataFrame(new_data) predicted_purchase = clf.predict(new_data_point)
OUTPUT: Predicted salary for experience 6: $95570.80 predicted_price = model.predict(new_df)
Predicted salary for experience 7 :$107833.40 if predicted_purchase[0] == 1:
print(f"Predicted price for the new house: ${predicted_price[0]:,.2f}”) print("The predicted purchase decision is 'Yes'.")
else:
OUTPUT: Predicted price for the new house: $672,757.74 print("The predicted purchase decision is 'No'.")
# Output data # Split the data into features (X) and target variable (y) # Initialize the KNN classifier
y = np.array([[0], X = df[['age']] # Feature (Age) knn = KNeighborsClassifier(n_neighbors=3) # You can adjust the number of neighbors (K) here
[1], y = df['insurance'] # Target variable
[1], # Fit the classifier with the training data
[0]]) # Initialize the Logistic Regression model knn.fit(X_train, y_train)
model = LogisticRegression()
# Seed for random number generation # Make predictions on the test set
np.random.seed(42) # Fit the model with the data predictions = knn.predict(X_test)
model.fit(X, y)
# Initialize weights and biases randomly # Calculate accuracy
input_neurons = 2 # Predict the insurance for a new age accuracy = accuracy_score(y_test, predictions)
hidden_neurons = 4 new_age = [[30]] # Prediction for a 30-year-old print(f"Accuracy: {accuracy}")
output_neurons = 1 predicted_insurance = model.predict(new_age)
OUTPUT: Accuracy: 1.0
weights_input_hidden = np.random.uniform(size=(input_neurons, hidden_neurons)) if predicted_insurance[0] == 1:
biases_hidden = np.random.uniform(size=(1, hidden_neurons)) print("The predicted insurance claim is 'Yes'.")
else:
weights_hidden_output = np.random.uniform(size=(hidden_neurons, output_neurons)) print("The predicted insurance claim is 'No'.")
biases_output = np.random.uniform(size=(1, output_neurons))
# Training the network OUTPUT: The predicted insurance claim is ‘No'.
learning_rate = 0.1
epochs = 10000
for epoch in range(epochs):
# Forward propagation
hidden_layer_input = np.dot(X, weights_input_hidden) + biases_hidden
hidden_layer_output = sigmoid(hidden_layer_input)
# Backpropagation
error = y - predicted_output
d_predicted_output = error * sigmoid_derivative(predicted_output)
error_hidden_layer = d_predicted_output.dot(weights_hidden_output.T)
d_hidden_layer = error_hidden_layer * sigmoid_derivative(hidden_layer_output)
# Visualize clusters
plt.scatter(df['Score'], df['Income'], c=df['Cluster'], cmap='viridis')
plt.xlabel('Score')
plt.ylabel('Income')
plt.title('K-Means Clustering')
plt.show()
Caption