AttributeError: 'numpy.ndarray' object has no attribute 'value_counts'
2017-06-19 23:31
2356 查看
《python机器学习及实践》第二章第一个代码运行报错:
AttributeError: 'numpy.ndarray' object has no attribute 'value_counts'
在上面加入以下语句,用来数据格式的转化,即可解决该错误
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)
相关文章推荐
- Python 保存数组至.mat文件,报错:AttributeError: 'numpy.ndarray' object has no attribute 'items'
- AttributeError: 'numpy.ndarray' object has no attribute 'index'
- 关于AttributeError:'numpy.ndarray' object has no attribute 'index'
- 'numpy.ndarray' object has no attribute 'values'
- AttributeError: 'module' object has no attribute 'imsave'
- AttributeError: '_csv.reader' object has no attribute 'next' 我在使用pyhon3.4运行以下代码时报错:AttributeError:
- python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
- python图像处理问题AttributeError: 'NoneType' object has no attribute 'shape'
- Python AttributeError: ‘module’ object has no attribute ‘A’
- AttributeError: module object has no attribute “Series”. Code works in Python
- AttributeError: 'dict' object has no attribute 'iteritems'
- python jieba 结巴分词报错 AttributeError: 'module' object has no attribute 'cut'
- [Python][转]Exception AttributeError: "'NoneType' object has no attribute
- python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
- django学习记录-- 添加haystack搜索框架时报错:AttributeError: 'BlogIndex' object has no attribute 'fields'
- python opencv:AttributeError: 'module' object has no attribute 'cv' FOURCC
- Python 使用sqlite3报错 AttributeError: 'module' object has no attribute 'connect'
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- 关于Srapy框架运行的错误AttributeError: 'module' object has no attribute 'OP_NO_TLSv1_1'