当前位置: 首页 > news >正文

【SVM smote】MAP - Charting Student Math Misunderstandings

针对数据不平衡问题,用调整类别权重的方式来处理数据不平衡问题,同时使用支持向量机(SVM)模型进行训练。

我们通过使用 SMOTE(Synthetic Minority Over-sampling Technique)进行过采样,增加少数类别的样本。。

import pandas as pd
import string
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay
import matplotlib.pyplot as plt
from imblearn.over_sampling import SMOTE# Step 1: Load the dataset
file_path = '/content/train.csv'  # 修改为实际文件路径
data = pd.read_csv(file_path)# Step 2: Clean the student explanation text (remove punctuation and lower case)
def clean_text(text):text = text.lower()  # Convert to lower casetext = ''.join([char for char in text if char not in string.punctuation])  # Remove punctuationreturn text# Apply the cleaning function to the 'StudentExplanation' column
data['cleaned_explanation'] = data['StudentExplanation'].apply(clean_text)# Step 3: Feature extraction using TF-IDF
vectorizer = TfidfVectorizer(stop_words='english', max_features=5000)
X = vectorizer.fit_transform(data['cleaned_explanation'])# Step 4: Prepare labels (Misconception column)
# We will predict if the explanation contains a misconception or not
data['Misconception'] = data['Misconception'].fillna('No_Misconception')# Convert labels to binary: 'No_Misconception' -> 0, any other label -> 1
y = data['Misconception'].apply(lambda x: 0 if x == 'No_Misconception' else 1)# Step 5: Split 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)# Step 16: Apply SMOTE for over-sampling the minority class
smote = SMOTE(random_state=42)
X_train_resampled, y_train_resampled = smote.fit_resample(X_train, y_train)# Step 7: Train an SVM model with the resampled data
svm_model = SVC(kernel='linear', class_weight='balanced', random_state=42)
svm_model.fit(X_train_resampled, y_train_resampled)# Step 8: Make predictions
y_pred_svm = svm_model.predict(X_test)# Step 9: Evaluate the model
print(classification_report(y_test, y_pred_svm))# Step 10: Plot confusion matrix
cm_weighted = confusion_matrix(y_test, y_pred_svm)# Use ConfusionMatrixDisplay to display the confusion matrix
disp = ConfusionMatrixDisplay(confusion_matrix=cm_weighted, display_labels=['No Misconception', 'Misconception'])
disp.plot(cmap=plt.cm.Blues)
plt.title('SVM Model with Balanced Class Weight Confusion Matrix')
plt.show()
 precision    recall  f1-score   support0       0.91      0.75      0.82      52771       0.56      0.81      0.66      2063accuracy                           0.77      7340macro avg       0.73      0.78      0.74      7340
weighted avg       0.81      0.77      0.78      7340

在这里插入图片描述

http://www.dtcms.com/a/288208.html

相关文章:

  • P1816 忠诚 题解
  • leetcode_53 最大子数组和
  • MySQL(145)如何升级MySQL版本?
  • 研华PCI-1285/1285E 系列------(一概述)
  • VIT速览
  • 用 Numpy 手动实现矩阵卷积运算
  • 汽车ECU控制器通信架构
  • 车载诊断架构 --- 故障码DTC严重等级定义
  • LVS部署模式NAT集群案例
  • WSL在 Windows 上使用 Linux 工具链和开发环境
  • 慕尼黑工业大学具身机器人实时环境探索!FindAnything:基于开放词汇对象中心映射的机器人任意环境认知与导航
  • FLASH:GPU 集群全连接通信的近最优极速调度
  • Keil编译文件格式转换全解析
  • 5 基于STM32单片机的绝缘检测系统设计(STM32代码编写+手机APP设计+PCB设计+Proteus仿真)
  • QT窗口(5)-对话框
  • 基于朴素贝叶斯的姓名性别预测系统
  • 如何构建未来的人-AI-环境智能教育生态系统
  • Java并发8--并发安全容器详解
  • 关于Vuex
  • uhd_find_devices有serial但是GNU Radio显示find no devices
  • Vue rem回顾
  • YOLOv8中添加SENet注意力机制
  • XSS-Labs 各关卡测试过程
  • 统计学习方法
  • 如何解决 ext4 文件系统的元数据损坏问题
  • 【深度强化学习】MIP-DQN 实现案例(完整Python代码)
  • [spring6: IntroductionAdvisor IntroductionInterceptor]-源码分析
  • C++编程学习(第11天)
  • Patch-wise Structural:一种引入局部统计特性的时序预测损失函数
  • eNSP综合实验(DNCP、NAT、TELET、HTTP、DNS)