神经网络,python报错:AttributeError: 'DataFrame' object has no attribute 'ravel'
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了神经网络,python报错:AttributeError: 'DataFrame' object has no attribute 'ravel'相关的知识,希望对你有一定的参考价值。
利用已有的库,写的一段神经网络的代码:
import pandas as pd
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
X=pd.read_csv('a_data.csv',usecols=(0,1,2,3,4,5,6,7,8,))
y=pd.read_csv('a_data.csv',usecols=(9,),dtype=str)
X_train,X_test,y_train,y_test=train_test_split(X,y)
scaler=StandardScaler()
scaler.fit(X_train)
StandardScaler(copy=True,with_mean=True,with_std=True)
X_train=scaler.transform(X_train)
X_test=scaler.transform(X_test)
mlp=MLPClassifier(hidden_layer_sizes=(11),activation="logistic",solver='adam',alpha=0.0001,
batch_size='auto',learning_rate='constant',learning_rate_init=0.1,
power_t=0.5,max_iter=1000,shuffle=True,random_state=None,tol=0.001,
verbose=False,warm_start=False,momentum=0.9,nesterovs_momentum=True,
early_stopping=False,validation_fraction=0.1,beta_1=0.9,beta_2=0.999,
epsilon=1e-08)
mlp.fit(X_train,y_train.ravel())
predictions = mlp.predict(X_test)
注:原先在mlp.fit(X_train,y_train.ravel())中我没有添加.ravel(),但是python提示: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)。添加ravel之后便出现如上问题。求高手帮忙看看。
这样试试,因为你的y不是一维向量。
我建议你先看看数据追问
这是我的部分数据的截图,因为是神经网络,由9个输入产生1个输出,前面9列是输入,最后1列是输出。根据这些数据,能帮我看看我的代码有哪些问题吗?
注:我刚才照你说的,改为y_train.values.ravel(),运行后无报错,但是提示“Process finished with exit code 0。这是什么问题?
你y的datatype 为什么是str 你试试 a
Process finished with exit code 0 是程序正常运行并完成
datatype是str的原因我也不清楚,不过改了y_train.values.ravel(),刚刚再试了,出结果了!谢谢啦!
本回答被提问者和网友采纳Python:(人工智能识别手写数字)使用卷积神经网络代码多个报错及相应解决方法
整个卷积神经网络代码(入门:识别手写数字)如下:
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as Data
import torchvision
import matplotlib.pyplot as plt
EPOCH = 1
BATCH_SIZE = 50
LR = 0.001
DOWNLOAD_MNIST = False
train_data = torchvision.datasets.MNIST(
root='./mnist',
train=True,
transform=torchvision.transforms.ToTensor(),
download=DOWNLOAD_MNIST
)
# print(train_data.train_data.size())
# print(train_data.train_labels.size())
# plt.imshow(train_data.train_data[0].numpy(),cmap='gray')
# plt.title('%i' % train_data.train_labels[0])
# plt.show()
train_loader = Data.DataLoader(dataset=train_data,batch_size=BATCH_SIZE,shuffle=True,num_workers=2)
test_data = torchvision.datasets.MNIST(root='./mnist',train=False)
test_x = Variable(torch.unsqueeze(test_data.test_data,dim=1),volatile=True).type(torch.FloatTensor)[:2000]/255.
test_y = test_data.test_labels[:2000]
class CNN(nn.Module):
def __init__(self):
super(CNN,self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(
in_channels=1,
out_channels=16,
kernel_size=5,
stride=1,
padding=2, # (5-1)/2
),
nn.ReLU,
nn.MaxPool2d(kernel_size=2),
)
self.conv2 = nn.Sequential(
nn.Conv2d(16,32,5,1,2),
nn.ReLU,
nn.MaxPool2d(2)
)
self.out = nn.Linear(32*7*7,10)
def forward(self,x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size(0),-1)
output = self.out(x)
return output
cnn = CNN()
# for(b_x,b_y) in train_loader:
optimizer = torch.optim.Adam(cnn.parameters(),lr=LR)
loss_func = nn.CrossEntropyLoss()
for epoch in range(EPOCH):
for step,(x,y) in enumerate(train_loader):
b_x = Variable(x)
b_y = Variable(y)
output = cnn(b_x)
loss = loss_func(output,b_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step % 50 == 0:
test_output = cnn(test_x)
pred_y = torch.max(test_output,1)[1].data.squeeze()
accuracy = sum(pred_y == test_y) / test_y.size(0)
print('Epoch:',epoch,'| train loss: %.4f' % loss.data[0],'| test accuracy: %.2f' % accuracy)
test_output = cnn(test_x[:10])
pred_y = torch.max(test_output,1)[1].data.numpy().squeeze()
print(pred_y,'prediction number')
print(test_y[:10].numpy(),'real number')
第一次使用的时候记得将DOWNLOAD_MNIST改成True。
第一个报错:
TypeError: torch.nn.modules.activation.ReLU is not a Module subclass
解决方法:应当将代码中nn.ReLU改为nn.ReLU()。
第二个报错:
RuntimeError: DataLoader worker (pid(s) 8772, 8773) exited unexpectedly
解决方法:将 train_loader = Data.DataLoader(dataset=train_data,batch_size=BATCH_SIZE,shuffle=True,num_workers=2) 中的num_workers=2改为0。
第三个报错:
IndexError: invalid index of a 0-dim tensor. Use
tensor.item()
in Python ortensor.item<T>()
in C++ to convert a 0-dim tensor to a number
解决方法:将 print(‘Epoch:’,epoch,’| train loss: %.4f’ % loss.data[0],’| test accuracy: %.2f’ % accuracy) 中的loss.data[0]改为:loss.item() 。
修改后的代码:
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as Data
import torchvision
import matplotlib.pyplot as plt
EPOCH = 1
BATCH_SIZE = 50
LR = 0.001
DOWNLOAD_MNIST = False
train_data = torchvision.datasets.MNIST(
root='./mnist',
train=True,
transform=torchvision.transforms.ToTensor(),
download=DOWNLOAD_MNIST
)
# print(train_data.train_data.size())
# print(train_data.train_labels.size())
# plt.imshow(train_data.train_data[0].numpy(),cmap='gray')
# plt.title('%i' % train_data.train_labels[0])
# plt.show()
train_loader = Data.DataLoader(dataset=train_data,batch_size=BATCH_SIZE,shuffle=True,num_workers=0)
test_data = torchvision.datasets.MNIST(root='./mnist',train=False)
test_x = Variable(torch.unsqueeze(test_data.test_data,dim=1),volatile=True).type(torch.FloatTensor)[:2000]/255.
test_y = test_data.test_labels[:2000]
class CNN(nn.Module):
def __init__(self):
super(CNN,self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(
in_channels=1,
out_channels=16,
kernel_size=5,
stride=1,
padding=2, # (5-1)/2
),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
self.conv2 = nn.Sequential(
nn.Conv2d(16,32,5,1,2),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.out = nn.Linear(32*7*7,10)
def forward(self,x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size(0),-1)
output = self.out(x)
return output
cnn = CNN()
# for(b_x,b_y) in train_loader:
optimizer = torch.optim.Adam(cnn.parameters(),lr=LR)
loss_func = nn.CrossEntropyLoss()
for epoch in range(EPOCH):
for step,(x,y) in enumerate(train_loader):
b_x = Variable(x)
b_y = Variable(y)
output = cnn(b_x)
loss = loss_func(output,b_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step % 50 == 0:
test_output = cnn(test_x)
pred_y = torch.max(test_output,1)[1].data.squeeze()
accuracy = sum(pred_y == test_y) / test_y.size(0)
print('Epoch:',epoch,'| train loss: %.4f' % loss.item(),'| test accuracy: %.2f' % accuracy)
test_output = cnn(test_x[:10])
pred_y = torch.max(test_output,1)[1].data.numpy().squeeze()
print(pred_y,'prediction number')
print(test_y[:10].numpy(),'real number')
以上是关于神经网络,python报错:AttributeError: 'DataFrame' object has no attribute 'ravel'的主要内容,如果未能解决你的问题,请参考以下文章
Python:(人工智能识别手写数字)使用卷积神经网络代码多个报错及相应解决方法
神经网络,python报错:AttributeError: 'DataFrame' object has no attribute 'ravel'
Python网络编程TCP服务器端问题,如图,为啥没报错也没输出?代码哪里有问题,求大神
在 Python 中导入历史标准普尔 500 指数数据 [关闭]