欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

AttributeError: 'numpy.ndarray' object has no attribute 'value_counts'

发布时间:2023/12/20 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 AttributeError: 'numpy.ndarray' object has no attribute 'value_counts' 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

《python机器学习及实践》第二章第一个代码运行报错:

 AttributeError: 'numpy.ndarray' object has no attribute 'value_counts' 

# coding: utf-8# In[1]:# 导入pandas与numpy工具包。 import pandas as pd import numpy as np# 创建特征列表。 column_names = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape', 'Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromatin', 'Normal Nucleoli', 'Mitoses', 'Class']# 使用pandas.read_csv函数从互联网读取指定数据。 data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data', names = column_names )# 将?替换为标准缺失值表示。 data = data.replace(to_replace='?', value=np.nan) # 丢弃带有缺失值的数据(只要有一个维度有缺失)。 data = data.dropna(how='any')# 输出data的数据量和维度。 data.shape# In[2]:# 使用sklearn.cross_valiation里的train_test_split模块用于分割数据。 from sklearn.cross_validation import train_test_split# 随机采样25%的数据用于测试,剩下的75%用于构建训练集合。 X_train, X_test, y_train, y_test = train_test_split(data[column_names[1:10]], data[column_names[10]], test_size=0.25, random_state=33) print "data[column_names[10]]",data[column_names[10]] print "y_train",y_train print "####################################################"# In[3]: print "y_train类型",type(y_train) print "y_test类型",type(y_test)# 查验训练样本的数量和类别分布。 y_train=pd.Series(y_train) y_train.value_counts()# In[4]:# 查验测试样本的数量和类别分布。 y_test=pd.Series(y_test) y_test.value_counts()# In[5]:# 从sklearn.preprocessing里导入StandardScaler。 from sklearn.preprocessing import StandardScaler # 从sklearn.linear_model里导入LogisticRegression与SGDClassifier。 from sklearn.linear_model import LogisticRegression from sklearn.linear_model import SGDClassifier# 标准化数据,保证每个维度的特征数据方差为1,均值为0。使得预测结果不会被某些维度过大的特征值而主导。 ss = StandardScaler() X_train = ss.fit_transform(X_train) X_test = ss.transform(X_test)# In[6]:# 初始化LogisticRegression与SGDClassifier。 lr = LogisticRegression() sgdc = SGDClassifier()# 调用LogisticRegression中的fit函数/模块用来训练模型参数。 lr.fit(X_train, y_train) # 使用训练好的模型lr对X_test进行预测,结果储存在变量lr_y_predict中。 lr_y_predict = lr.predict(X_test)# 调用SGDClassifier中的fit函数/模块用来训练模型参数。 sgdc.fit(X_train, y_train) # 使用训练好的模型sgdc对X_test进行预测,结果储存在变量sgdc_y_predict中。 sgdc_y_predict = sgdc.predict(X_test)# In[7]:# 从sklearn.metrics里导入classification_report模块。 from sklearn.metrics import classification_report# 使用逻辑斯蒂回归模型自带的评分函数score获得模型在测试集上的准确性结果。 print 'Accuracy of LR Classifier:', lr.score(X_test, y_test) # 利用classification_report模块获得LogisticRegression其他三个指标的结果。 print classification_report(y_test, lr_y_predict, target_names=['Benign', 'Malignant'])# In[8]:# 使用随机梯度下降模型自带的评分函数score获得模型在测试集上的准确性结果。 print 'Accuarcy of SGD Classifier:', sgdc.score(X_test, y_test) # 利用classification_report模块获得SGDClassifier其他三个指标的结果。 print classification_report(y_test, sgdc_y_predict, target_names=['Benign', 'Malignant'])# In[ ]:
在上面加入以下语句,用来数据格式的转化,即可解决该错误 y_test=pd.Series(y_test)

总结

以上是生活随笔为你收集整理的AttributeError: 'numpy.ndarray' object has no attribute 'value_counts'的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。