0% found this document useful (0 votes)
7 views1 page

ML External Xerox

The document contains Python code for data analysis and machine learning tasks using libraries like Pandas and Scikit-learn. It includes examples of linear regression, decision tree classification, logistic regression, and K-means clustering on various datasets. The code demonstrates data preparation, model training, prediction, and visualization of results.

Uploaded by

siva M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

ML External Xerox

The document contains Python code for data analysis and machine learning tasks using libraries like Pandas and Scikit-learn. It includes examples of linear regression, decision tree classification, logistic regression, and K-means clustering on various datasets. The code demonstrates data preparation, model training, prediction, and visualization of results.

Uploaded by

siva M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

3.

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: The predicted purchase decision is ‘Yes'.

4.import numpy as np 5. import pandas as pd 6.from sklearn.datasets import load_iris


# Sigmoid activation function from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split
def sigmoid(x): from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier
return 1 / (1 + np.exp(-x)) from sklearn.metrics import accuracy_score from sklearn.metrics import accuracy_score
# Derivative of the sigmoid function
def sigmoid_derivative(x): # Create a DataFrame with the provided data # Load Iris dataset
return x * (1 - x) data = { iris = load_iris()
# Input data 'age': [22, 25, 47, 52, 46, 56, 55, 60, 62, 61, 18, 28, 27], X = iris.data # Features
X = np.array([[0, 0], 'insurance': [0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0] y = iris.target # Target variable
[0, 1], }
[1, 0], # Split the data into training and testing sets
[1, 1]]) df = pd.DataFrame(data) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 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)

output_layer_input = np.dot(hidden_layer_output, weights_hidden_output) + biases_output


predicted_output = sigmoid(output_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)

# Updating weights and biases


weights_hidden_output += hidden_layer_output.T.dot(d_predicted_output) * learning_rate
biases_output += np.sum(d_predicted_output, axis=0, keepdims=True) * learning_rate

weights_input_hidden += X.T.dot(d_hidden_layer) * learning_rate


biases_hidden += np.sum(d_hidden_layer, axis=0, keepdims=True) * learning_rate
# Output after training
print("Output after training:")
print(predicted_output)

7. from sklearn.datasets import load_iris 8. import pandas as pd


from sklearn.model_selection import train_test_split from sklearn.cluster import KMeans
from sklearn.svm import SVC import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
# Create a DataFrame with the provided data
# Load Iris dataset data = {
iris = load_iris() 'Gender': ['Male', 'Male', 'Female', 'Female', 'Female', 'Female', 'Male', 'Male', 'Female',
X = iris.data # Features 'Female', 'Male', 'Male'],
y = iris.target # Target variable 'Score': [19, 21, 20, 23, 31, 22, 35, 23, 67, 35, 58, 26],
'Income': [45000, 40000, 35000, 50000, 75000, 40000, 60000, 52000, 70000, 35000, 57000,
# Split the data into training and testing sets 45000]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) }

# Initialize the SVM classifier df = pd.DataFrame(data)


svm = SVC(kernel='linear') # You can use different kernels like 'linear', 'rbf', 'poly', etc.
# Map gender to numerical value (0 for Female, 1 for Male)
# Fit the classifier with the training data df['Gender'] = df['Gender'].map({'Female': 0, 'Male': 1})
svm.fit(X_train, y_train)
# Extract the features
# Make predictions on the test set X = df[['Score', 'Income']]
predictions = svm.predict(X_test)
# Initialize KMeans with the number of clusters
# Calculate accuracy kmeans = KMeans(n_clusters=3) # You can adjust the number of clusters
accuracy = accuracy_score(y_test, predictions)
print(f"Accuracy: {accuracy}") # Fit KMeans to the data
kmeans.fit(X)
OUTPUT: Accuracy:1.0
# Get cluster labels
df['Cluster'] = kmeans.labels_

# 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

You might also like