python - 神经网络中的维度问题
问题描述
这就是我所拥有的
import numpy as np
import pandas as pd
df = pd.read_csv('spam.csv')
import nltk
import string
from nltk.corpus import stopwords
def text_process(mess):
nopunc = [char for char in mess if char not in string.punctuation]
nopunc = ''.join(nopunc)
return [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]
df.head()
Label EmailText
0 ham Go until jurong point, crazy.. Available only ...
1 ham Ok lar... Joking wif u oni...
2 spam Free entry in 2 a wkly comp to win FA Cup fina...
3 ham U dun say so early hor... U c already then say...
df['EmailText'].head(5).apply(text_process)
from sklearn.feature_extraction.text import CountVectorizer
bow_transformer = CountVectorizer(analyzer=text_process).fit(df['EmailText'])
messages_bow = bow_transformer.transform(df['EmailText'])
sparsity = (100.0 * messages_bow.nnz / (messages_bow.shape[0] * messages_bow.shape[1]))
from sklearn.feature_extraction.text import TfidfTransformer
tfidf_transformer = TfidfTransformer().fit(messages_bow)
messages_tfidf = tfidf_transformer.transform(messages_bow)
X = messages_tfidf
y = df['Label']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
#Neural network
def sigmoid(x):
return 1.0 / 1.0 + np.exp(-x)
def derivative(x):
return x * (1.0 - x)
dim1 = len(X_train[0])
dim2 = 100
np.random.seed(1)
weight0 = 2 * np.random.random((dim1, dim2)) - 1
weight1 = 2 * np.random.random((dim2, 1)) - 1
for epoch in range(2000):
layer_0 = X_train
layer_1 = sigmoid(np.dot(layer_0,weight0))
layer_2 = sigmoid(np.dot(layer_1,weight1))
这是我遇到的错误,我无法继续
ValueError: shapes (4457,11304) and (1,100) not aligned: 11304 (dim 1) != 1 (dim 0)
我想知道如何设置维度(dim2),是什么决定了该值,由于我正在遵循的教程,我只是随机使用了“100”,但我认为值 dim2 代表了一些东西。请问谁能给我解释一下?
解决方案
推荐阅读
- excel - 比较表 1 的 B 列与表 2 的 B 列中的值;在工作表 2 中附加不匹配的值
- excel - 尝试更改范围时,RowIndex 的范围不起作用
- python - python selenium如何点击登录按钮
- python - 根据条件将一列的值赋予熊猫数据框中的另一列
- javascript - Javascript - 如果满足条件,则在特定时间内返回 True,然后返回 false
- amazon-web-services - 是否可以从 Lambda 动态调用 AWS CloudFormation 以临时创建资源?
- flutter - 颤振:我可以调整 CupertinoAlertDialog(iOS 样式)的宽度或高度吗?
- javascript - 如何接受在 NodeJs 或 Typescript 反序列化期间接受的属性的备用名称?
- installation - .apk 文件从我的网络服务器安装为 .zip
- android - 滚动视图上的Android回收器视图