-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_confusion_matrix2.py
More file actions
94 lines (80 loc) · 3.31 KB
/
Copy pathtrain_confusion_matrix2.py
File metadata and controls
94 lines (80 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.model_selection import GridSearchCV
import os
# Features and labels for the training and test sets
base_dir = "UCI HAR Dataset"
train_dir = os.path.join(base_dir, "train")
test_dir = os.path.join(base_dir, "test")
X_train = pd.read_csv(os.path.join(train_dir, "X_train.txt"), sep=r'\s+', header=None)
y_train = pd.read_csv(os.path.join(train_dir, "y_train.txt"), sep=r'\s+', header=None)
X_test = pd.read_csv(os.path.join(test_dir, "X_test.txt"), sep=r'\s+', header=None)
y_test = pd.read_csv(os.path.join(test_dir, "y_test.txt"), sep=r'\s+', header=None)
# Activity label mapping (number → activity name)
activity_labels = pd.read_csv(
os.path.join(base_dir, "activity_labels.txt"),
sep=r'\s+',
header=None,
names=["label", "activity"]
)
# Feature names (add column names to X_train/X_test)
features = pd.read_csv(
os.path.join(base_dir, "features.txt"),
sep=r'\s+',
header=None,
names=["feature_idx", "feature_name"]
)
X_train.columns = features["feature_name"]
X_test.columns = features["feature_name"]
# 1. Feature Selection (Extract core features: containing'mean()', 'std()' and 'angle')
core_features = X_train.columns[X_train.columns.str.contains(r'mean\(\)|std\(\)|angle')]
X_train_selected = X_train[core_features]
X_test_selected = X_test[core_features]
# Label conversion (number → activity name for visualization)
y_train_name = y_train.merge(activity_labels, left_on=0, right_on='label')['activity']
y_test_name = y_test.merge(activity_labels, left_on=0, right_on='label')['activity']
# 2. Data Preprocessing and Model Training
# Standardization
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train_selected)
X_test_scaled = scaler.transform(X_test_selected)
# Define the Random Forest model and parameter grid
model = RandomForestClassifier(random_state=42)
param_grid = {
'n_estimators': [150, 200],
'max_depth': [None, 30, 40],
'min_samples_split': [2, 5]
}
# Grid Search
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=3, n_jobs=-1)
grid_search.fit(X_train_scaled, y_train[0])
# Use the model with the best parameters for prediction
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test_scaled)
# 3. Error Visualization
# Confusion Matrix
cm = confusion_matrix(y_test[0], y_pred)
plt.figure(figsize=(10, 8))
plt.rcParams['font.family'] ='sans-serif' # Set the font family to sans-serif (a common and universal font)
plt.rcParams['font.size'] = 12
sns.heatmap(
cm,
annot=True,
fmt='d',
cmap='Blues',
xticklabels=activity_labels['activity'],
yticklabels=activity_labels['activity']
)
plt.title("Confusion matrix")
plt.xlabel("Predicted Activity")
plt.ylabel("True Activity")
plt.show()
# Classification Report (including error metrics)
print("Classification Report (Precision/Recall/F1-score):")
print(classification_report(y_test_name, y_pred, target_names=activity_labels['activity']))
# Output the best parameters
print("Best parameters:", grid_search.best_params_)