AI
AI
# ============================
# Part 1: Load and Clean Data
# ============================
# Load the dataset
df = pd.read_csv("amazon.csv")
# Display first few rows and column names to check if 'amount' exists
print(df.head())
print("\nColumns in the dataset:", df.columns)
# =============================
# Part 2: Data Visualization
# =============================
plt.figure(figsize=(8, 5))
sns.histplot(df['amount'], bins=30, kde=True)
plt.title("Distribution of Sales Amount")
plt.xlabel("Amount")
plt.ylabel("Frequency")
plt.show()
# =============================
# Part 3: Modeling & Evaluation
# =============================
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Evaluation function
def evaluate_model(name, y_true, y_pred):
r2 = r2_score(y_true, y_pred)
rmse = np.sqrt(mean_squared_error(y_true, y_pred))
return [name, round(r2, 4), round(rmse, 4)]
# Collect results
results = []
results.append(evaluate_model("Linear Regression", y_test, y_pred_lr))
results.append(evaluate_model("Random Forest", y_test, y_pred_rf))
# Convert to DataFrame
results_df = pd.DataFrame(results, columns=["Model", "R2 Score", "RMSE"])
print("\nModel Evaluation Results:")
print(results_df)
# =============================
# Part 4: Save Report
# =============================
results_df.to_csv("model_evaluation_report.csv", index=False)
print("\nReport saved as model_evaluation_report.csv")
---------------------supervised2--------------------------
# Import necessary libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
# Models
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
cat_pipeline = Pipeline([
('imputer', SimpleImputer(strategy='most_frequent')),
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
# Combine pipelines
preprocessor = ColumnTransformer([
('num', num_pipeline, num_cols),
('cat', cat_pipeline, cat_cols)
])
X = df.drop('EZE', axis=1)
y = df['EZE']
# 1. Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# 3. Cross-validation
print("\nCross-validation results (5-fold):")
X_processed = preprocessor.fit_transform(X) # Preprocess all data for CV
for name, model in models.items():
cv_scores = cross_val_score(
Pipeline([
('preprocessor', preprocessor),
('classifier', model)
]),
X, y, cv=5, scoring='accuracy'
)
print(f"{name} - Mean CV Accuracy: {cv_scores.mean():.4f} (±
{cv_scores.std():.4f})")
-------------------------------------
supervised2.1-----------------------------------------
# Import necessary libraries
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
# 1. Load dataset
df = pd.read_csv('Report.csv')
print("First 5 rows of data:")
print(df.head())
# Categorical columns
cat_cols = X.select_dtypes(include=['object']).columns
for col in cat_cols:
X[col].fillna(X[col].mode()[0], inplace=True)
# 6. Split data
X_train, X_test, y_train, y_test = train_test_split(X_encoded, y, test_size=0.2,
random_state=42)
# Training evaluation
train_pred = model.predict(X_train)
print(f"\n{name} - Training Accuracy: {accuracy_score(y_train,
train_pred):.4f}")
# Test evaluation
test_pred = model.predict(X_test)
print(f"{name} - Test Accuracy: {accuracy_score(y_test, test_pred):.4f}")
print("Confusion Matrix:")
print(confusion_matrix(y_test, test_pred))
print("Classification Report:")
print(classification_report(y_test, test_pred))
# Cross-validation
cv_scores = cross_val_score(model, X_encoded, y, cv=5)
print(f"5-Fold CV Accuracy: {cv_scores.mean():.4f} (±{cv_scores.std():.4f})")
-------------------supervised3-------------------------
# Import necessary libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler, LabelEncoder, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
# For models
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
# Let's artificially modify the dataset to include missing values and categorical
variables
# for demonstration purposes
df.iloc[2:5, 0] = np.nan # Add missing values to first column
df.iloc[10:15, 2] = np.nan # Add missing values to third column
df['category'] = np.random.choice(['A', 'B', 'C'], size=len(df)) # Add categorical
column
print("Original dataset:")
print(df.head())
print("\nMissing values per column:")
print(df.isnull().sum())
print("\nAfter standardization:")
print(df.head())
---bins csp---------
# Step 5: Constraints
# Each item must go in exactly one bin -> already ensured by variable domain (0-2)
def solve_patients_beds():
model = cp_model.CpModel()
# Solve
solver = cp_model.CpSolver()
status = solver.Solve(model)
if __name__ == "__main__":
solve_patients_beds()
--- Teachers and Classes CSP
def solve_teachers_classes():
model = cp_model.CpModel()
# Teacher preferences/qualifications
preferences = {
"T1": ["C1", "C3"],
"T2": ["C1", "C3"], # T2 cannot teach C2
"T3": ["C2", "C1", "C3"] # T3 prefers C2
}
# Solve
solver = cp_model.CpSolver()
status = solver.Solve(model)
if __name__ == "__main__":
solve_teachers_classes()
--------------------------------------------
patients---------------------------------------
from ortools.sat.python import cp_model
def solve_seating_arrangement():
# Create the model
model = cp_model.CpModel()
# Seats: 0 to 5
seats = range(6)
panelists = ['Amir', 'Bella', 'Charles', 'Diana', 'Ethan', 'Farah']
# Verify constraints
print("\nVerification:")
bella_seat = solver.Value(assignments['Bella'])
farah_seat = solver.Value(assignments['Farah'])
print(f"- Bella (S{bella_seat}) is left of Farah (S{farah_seat}):
{bella_seat < farah_seat}")
charles_seat = solver.Value(assignments['Charles'])
diana_seat = solver.Value(assignments['Diana'])
print(f"- Charles (S{charles_seat}) is next to Diana (S{diana_seat}):
{abs(charles_seat - diana_seat) == 1}")
amir_seat = solver.Value(assignments['Amir'])
print(f"- Amir is not at either end (S{amir_seat}): {amir_seat not in [0,
5]}")
ethan_seat = solver.Value(assignments['Ethan'])
print(f"- Ethan is in the middle (S{ethan_seat}): {ethan_seat in [2, 3]}")
if __name__ == '__main__':
solve_seating_arrangement()
--------------------------------------------------------------------------
patients easy
from ortools.sat.python import cp_model
def solve_patients_beds():
model = cp_model.CpModel()
# Step 1: Define bed types and how many patients each can hold
bed_types = ["ICU", "GeneralCare", "Private", "EmergencyCare"]
bed_capacities = {
"ICU": 2,
"GeneralCare": 3,
"Private": 1,
"EmergencyCare": 2
}
# Step 3: Define conflict pairs (patients who can't share the same bed type)
conflicts = [("A", "B"), ("C", "D")]
# Step 4: Convert bed types to numbers (e.g., "ICU" -> 0)
bed_to_index = {bed: i for i, bed in enumerate(bed_types)}
index_to_bed = {i: bed for bed, i in bed_to_index.items()}
# Step 7: Add conflict constraints (conflicting patients can't have the same
bed)
for p1, p2 in conflicts:
model.Add(patient_vars[p1] != patient_vars[p2])
------------------------------------------------------------------------
a
seats = {
'Amir': model.NewIntVar(0, 5, 'Amir'),
'Bella': model.NewIntVar(0, 5, 'Bella'),
'Charles': model.NewIntVar(0, 5, 'Charles'),
'Diana': model.NewIntVar(0, 5, 'Diana'),
'Ethan': model.NewIntVar(0, 5, 'Ethan'),
'Farah': model.NewIntVar(0, 5, 'Farah')
}
b
from ortools.sat.python import cp_model
def solve_seating():
model = cp_model.CpModel()
# Panelists
panelists = ['Amir', 'Bella', 'Charles', 'Diana', 'Ethan', 'Farah']
# Print results
if status in (cp_model.FEASIBLE, cp_model.OPTIMAL):
print("Solution Found:\n")
seat_to_person = [''] * 6
for person, var in seats.items():
seat_to_person[solver.Value(var)] = person
for i, person in enumerate(seat_to_person):
print(f"Seat {i}: {person}")
print(f"\nSolver status: {'OPTIMAL' if status == cp_model.OPTIMAL else
'FEASIBLE'}")
else:
print("No solution found.")