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

【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.xdnf.cn/news/15884.html

相关文章:

  • Custom SRP - Custom Render Pipeline
  • RabbitMQ01——基础概念、docker配置rabbitmq、内部执行流程、五种消息类型、测试第一种消息类型
  • RabbitMQ—事务与消息分发
  • 软考 系统架构设计师系列知识点之杂项集萃(113)
  • AJAX概述
  • c++ 基本语法易错与技巧总结
  • 零基础学习性能测试-linux服务器监控:内存监控
  • fastjson2 下划线字段转驼峰对象
  • 【RK3576】【Android14】分区划分
  • 石子问题(区间dp)
  • 从Prompt到结构建模:如何以数据驱动重构日本语言学校体系?以国际日本语学院为例
  • Linux:lvs集群技术
  • LVS四种工作模式深度解析
  • 千线万网,电路之行——LVS检查的内核逻辑
  • Python day18
  • 统计EfficientNet-B7的参数个数。
  • 华为擎云L420安装LocalSend
  • 单元测试学习+AI辅助单测
  • 【图像处理基石】什么是小波变换?
  • 美国VPS服务器Linux内核参数调优的实践与验证
  • iOS 通知机制及底层原理
  • 突破 MySQL 性能瓶颈:死锁分析 + 慢查询诊断 + 海量数据比对实战
  • 【设计模式C#】状态模式(用于解决解耦多种状态之间的交互)
  • 中间件安全攻防全解:从Tomcat到Weblogic反序列化漏洞介绍
  • 使用DataGrip连接安装在Linux上的Redis
  • FreeRTOS—列表和列表项
  • 相机参数的格式与作用
  • Vue3 学习教程,从入门到精通,Vue 3 声明式渲染语法指南(10)
  • 快速上手AI整合包!GPT-SoVITS-v2打包教程,解锁AIStarter应用市场潜力
  • DC-DC降压转换5.5V/3A高效率低静态同步降压转换具有自适应关断功能