weixin_44815085
A Peaceful Tree
采纳率75%
2019-09-07 00:02 阅读 8.7k

如何解决 cannot reshape array of size 10000 into shape (1000,6)

我把excel数据(纯数字)导入datamatrix,但是在np.array(x).reshape((len(x),6)) 的时候报错了,有哪位大佬可以解释一下吗

import numpy as np
import urllib.request
import pandas as pd
from pandas import DataFrame
import numpy as np
import pandas as pd
import xlrd
from sklearn import preprocessing
def excel_to_matrix(path):
    table = xlrd.open_workbook(path).sheets()[0]  # 获取第一个sheet表
    row = table.nrows  # 行数
    col = table.ncols  # 列数
    datamatrix = np.zeros((row, col))
    for x in range(col):
        cols = np.matrix(table.col_values(x))

        datamatrix[:, x] = cols
    return datamatrix


datafile = u'C:\\Users\\asus\\PycharmProjects\\2\\venv\\Lib\\附件2:数据.xls'
datamatrix=excel_to_matrix(datafile)
data=pd.DataFrame(datamatrix)

y=data[10]
data=data.drop(10,1)
x=data

from sklearn import preprocessing
x_MinMax=preprocessing.MinMaxScaler()
y_MinMax=preprocessing.MinMaxScaler()

y.as_matrix(y)
y=np.array(y).reshape((len(y),1))
x=np.array(x).reshape((len(x),6))
x=x_MinMax.fit_transform(x)
y=y_MinMax.fit_transform(y)
x.mean(axis=0)

import random
from sklearn.cross_validation import train_test_split
np.random.seed(2016)
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)

from sknn.mlp import Regressor,Layer #预测模型
w_train=x_train[:,0]
w_train[y_train==0]=1
w_train[y_train==1]=1.10
fit3=Regressor(layers=[Layer('Tanh',units=45),Layer('Tanh',units=18),
                       Layer('Tanh',units=18),
                       Layer('softmax')],
               learning_rate=0.02,
               random_state=2016,
               valid_size=0.25,
               dropout_rate=0.2,
               learning_momentum=0.30,
               batch_size=35,
               n_iter=10
               )
fit3.fit(x_train,y_train,w_train)

from sklearn.metrics import confusion_matrix
predict3_train=fit3.predict(x_train)
score3=fit3.score(x_train,y_train)
confu3=confusion_matrix(y_train,predict3_train)
print(confu3)
score_text3=fit3.score(x_test,y_test)
print(score_text3)
predict3_test=fit3.predict(x_test)
confu3_test=confusion_matrix(y_test,predict3_test)
print(confu3_test)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    weixin_40802676 无穷升高的卡农 2019-09-07 10:25

    因为你原来有10000个元素,你reshape只有6000
    图片说明

    点赞 评论 复制链接分享

相关推荐