机器学习不同分类模型精度对比实现报错求助
分类模型精度对比项目问题解决
项目说明
该项目用于练习分类模型技能,实现决策树、Logistic Regression、SVM等不同算法的精度对比,分析各模型的表现特性。
原问题代码
import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn import svm from sklearn import metrics def main(): x, y = loadData() x_train, x_test, y_train, y_test = trainTest(x,y) logisticAccuracy = logisticRegressionModel(x_train, x_test, y_train, y_test) dectreeAccuracy = decisionTreeModel(x_train, x_test, y_train, y_test) svmAccuracy = svmModel(x_train, x_test, y_train, y_test) printComparisonResults(logisticAccuracy,dectreeAccuracy,svmAccuracy) def loadData(): df = pd.read_csv('D:\Tutorials\Resources\creditcard.csv') x = df[['Time','V1','V2','V3','V5','V6','V7','V8','V9','V10','V11','V12',\ 'V13','V14','V5','V16','V17','V18','V19','V20','V21','V22','V23',\ 'V24','V25','V26','V27', 'V28','Amount']] y = df['Class'] return x, y def trainTest(x,y): x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.25, random_state=0) return x_train, x_test, y_train, y_test # LogisticRegression model def logisticRegressionModel(x_train, x_test, y_train, y_test): logistic_reg = LogisticRegression().fit(x_train, y_train) y_pred = logistic_reg.predict(x_test) #evaluate accuracy of our model acc = metrics.accuracy_score(y_test, y_pred) return acc def decisionTreeModel(x_train, x_test, y_train, y_test): dec_tree = DecisionTreeClassifier() dec_tree = dec_tree.fit(x_train, y_train) y_pred = dec_tree.predict(x_test) #evaluate model acc = metrics.accuracy_score(y_test, y_pred) return acc def svmModel(x_train, x_test, y_train, y_test): svm_model = svm.SVC(kernel='linear') svm_model = svm_model.fit(x_train, y_train) y_pred = svm_model.predict(y_test) #evaluate model acc = metrics.accuracy_score(y_test, y_pred) return acc def printComparisonResults(logisticAccuracy,dectreeAccuracy,svmAccuracy): print("Printing Results") lr = "Logistic Regression" dt = "Decision Tree" sv = "SVM is Accurate" if logisticAccuracy > (dectreeAccuracy and svmAccuracy): print(lr, "is accurate than", dt, "and", sv) elif dectreeAccuracy > (logisticAccuracy and svmAccuracy): print(dt, "is accurate than", lr, "and", sv) else: print(sv, "is accurate than", lr, "and", dt) print("Done") if name == 'main': main()
首次运行报错
ValueError: Expected 2D array, got 1D array instead: array=[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
错误修改尝试与二次报错
尝试修改特征数组为x = np.array(df[[...]].reshape((1,-1))后,出现新报错:
ValueError: Found input variables with inconsistent numbers of samples: [1, 2470]
问题原因与修复方案
核心错误1:SVM预测参数传错
svmModel函数中调用predict方法时,错误传入了1维的标签数组y_test,应该传入测试集特征x_test,直接导致模型收到1维数组报错。
错误2:特征reshape逻辑错误
原特征数组本身是符合模型要求的2维结构(样本数*特征数),强行reshape为(1,-1)会把所有样本压缩为1个样本,导致特征样本数和标签样本数不匹配,触发第二个报错,直接删掉这行修改代码即可。
其他需要修复的问题:
- 特征选择错误:
loadData函数中特征列重复选择了V5,删除重复的V5即可 - 逻辑回归收敛问题:
LogisticRegression初始化时添加max_iter=1000参数,避免训练不收敛报警告 - 精度比较逻辑错误:原判断
logisticAccuracy > (dectreeAccuracy and svmAccuracy)逻辑错误,正确写法为logisticAccuracy > dectreeAccuracy and logisticAccuracy > svmAccuracy - 程序入口错误:原入口判断应为
if __name__ == '__main__':,缺少双下划线会导致程序无法正常启动
修复后的关键代码片段
# 修正SVM预测参数 def svmModel(x_train, x_test, y_train, y_test): svm_model = svm.SVC(kernel='linear') svm_model = svm_model.fit(x_train, y_train) y_pred = svm_model.predict(x_test) # 这里修改为传入x_test acc = metrics.accuracy_score(y_test, y_pred) return acc # 修正逻辑回归初始化 def logisticRegressionModel(x_train, x_test, y_train, y_test): logistic_reg = LogisticRegression(max_iter=1000).fit(x_train, y_train) y_pred = logistic_reg.predict(x_test) acc = metrics.accuracy_score(y_test, y_pred) return acc # 修正精度比较逻辑 def printComparisonResults(logisticAccuracy,dectreeAccuracy,svmAccuracy): print("Printing Results") lr = "Logistic Regression" dt = "Decision Tree" sv = "SVM" if logisticAccuracy > dectreeAccuracy and logisticAccuracy > svmAccuracy: print(lr, "is more accurate than", dt, "and", sv) elif dectreeAccuracy > logisticAccuracy and dectreeAccuracy > svmAccuracy: print(dt, "is more accurate than", lr, "and", sv) else: print(sv, "is more accurate than", lr, "and", dt) print("Done") # 修正程序入口 if __name__ == '__main__': main()
内容的提问来源于stack exchange,提问作者Charles
相关产品推荐
相关产品推荐

