基于CSV数据训练测试模型

基于语音进行情绪识别

数据类型为csv文件,其中包含847个特征列。类别有三类:happy,surprised,angry
数据集可以在kaggle上下载
链接地址:https://www.kaggle.com/ashishbansal23/emotion-recognition

基本包的导入

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

这里使用了三个机器学习模型:SVCRandomForestClassifierLinearRegression进行实验。GridSearchCV用来循环查找最好的参数。

读csv文件

nameemotion列删除,其他的数据作为特征Xemotion列作为标签y

df = pd.read_csv('E:/emotion_recognition/project1/ANAD_Normalized.csv')
X=df.drop(['name', 'Emotion '], axis=1) # features
y=df['Emotion ']     # labels
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.25, random_state=1) # 划分数据集

选择模型

1. SVC模型
grid={'C':[1,5,50],
     'gamma':[0.1,1,10,100]}
model=GridSearchCV(SVC(), grid)
model.fit(X_train, y_train)
pred=model.predict(X_test)
print(classification_report(y_test, pred))

运行结果:

                precision    recall  f1-score   support

       angry       0.97      0.98      0.97       177
       happy       0.94      0.97      0.95       134
   surprised       0.96      0.77      0.86        35

    accuracy                           0.95       346
   macro avg       0.96      0.91      0.93       346
weighted avg       0.95      0.95      0.95       346

利用model.best_params_可以输出最佳的参数

{'C': 5, 'gamma': 0.1}
2. RandomForestClassifier模型
grid={'n_estimators':[10,50,100,300]}
model=GridSearchCV(RandomForestClassifier(), grid)
model.fit(X_train,y_train)
print(model.best_params_)

打印最佳参数

{'n_estimators': 100}

模型评估

pred=model.predict(X_test)
print(classification_report(y_test, pred2))

打印分类评估指标报告

                precision    recall  f1-score   support

       angry       0.94      0.99      0.97       177
       happy       0.95      0.96      0.95       134
   surprised       0.92      0.63      0.75        35

    accuracy                           0.94       346
   macro avg       0.94      0.86      0.89       346
weighted avg       0.94      0.94      0.94       346
3. LogisticRegression模型
model = LogisticRegression(solver='liblinear',multi_class='auto')
model.fit(X_train,y_train)
pred=model.predict(X_test)
# accuracy_score(y_test, pred)
print(classification_report(y_test, pred))

运行结果

                precision    recall  f1-score   support

       angry       0.97      0.95      0.96       177
       happy       0.93      0.98      0.95       134
   surprised       0.87      0.77      0.82        35

    accuracy                           0.95       346
   macro avg       0.92      0.90      0.91       346
weighted avg       0.94      0.95      0.94       346
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值