我正在使用 SKLearn 对我的数据运行 SVC。
from sklearn import svm
svc = svm.SVC(kernel='linear', C=C).fit(X, y)
Run Code Online (Sandbox Code Playgroud)
我想知道如何从决策边界获得 X 中每个数据点的距离?
我有一个具有唯一标识符和其他功能的数据集。看起来像这样
Run Code Online (Sandbox Code Playgroud)ID LenA TypeA LenB TypeB Diff Score Response 123-456 51 M 101 L 50 0.2 0 234-567 46 S 49 S 3 0.9 1 345-678 87 M 70 M 17 0.7 0
我把它分成训练和测试数据。我试图从训练数据训练的分类器中将测试数据分为两类。我想要训练和测试数据集中的标识符,以便我可以将预测映射回 IDs。
有没有一种方法可以像我们在 Azure ML Studio 或 SAS 中所做的那样,将标识符列分配为 ID 或非预测变量?
我正在使用DecisionTreeClassifier来自 Scikit-Learn 的。这是我的分类器代码。
from sklearn import tree
clf = tree.DecisionTreeClassifier()
clf = clf.fit(traindata, trainlabels)
Run Code Online (Sandbox Code Playgroud)
如果我只是将 ID 包含到 中traindata,则代码会引发错误:
ValueError: invalid literal for float(): 123-456
我想使用 keras 构建一个 RNN 模型来对句子进行分类。
我尝试了以下代码:
docs = []
with open('all_dga.txt', 'r') as f:
for line in f.readlines():
dga_domain, _ = line.split(' ')
docs.append(dga_domain)
t = Tokenizer()
t.fit_on_texts(docs)
encoded_docs = t.texts_to_matrix(docs, mode='count')
print(encoded_docs)
Run Code Online (Sandbox Code Playgroud)
但得到了一个内存错误。似乎我无法将所有数据加载到内存中。这是输出:
Traceback (most recent call last):
File "test.py", line 11, in <module>
encoded_docs = t.texts_to_matrix(docs, mode='count')
File "/home/yurzho/anaconda3/envs/deepdga/lib/python3.6/site-packages/keras/preprocessing/text.py", line 273, in texts_to_matrix
return self.sequences_to_matrix(sequences, mode=mode)
File "/home/yurzho/anaconda3/envs/deepdga/lib/python3.6/site-packages/keras/preprocessing/text.py", line 303, in sequences_to_matrix
x = np.zeros((len(sequences), num_words))
MemoryError
Run Code Online (Sandbox Code Playgroud)
如果有人熟悉 keras,请告诉我如何预处理数据集。
提前致谢!
我正在尝试使用 tensorflow 实现一个简单的神经网络。这是一个二元分类问题。X_train 的形状:(batch_size, 70) 和 Y_train: (batch_size, 2)。我正在使用 csv 读取数据。这是我的代码。我在 python 3.6.0 上运行它。
import numpy as np
import csv
import tensorflow as tf
with open('criminal_train.csv') as fp:
reader = csv.reader(fp, delimiter=',', quotechar='"')
train_data = np.array([row for row in reader])
data_X = train_data[1:, 1:-1]
data_Y = train_data[1:, -1:]
with open('criminal_test.csv') as fp:
reader = csv.reader(fp, delimiter=',', quotechar='"')
test_data = np.array([row for row in reader])
predict_data = test_data[1:, 1:]
#Spltting the training data in 80:20
split = int(data_X.shape[0]*0.8)
X_train = data_X[:split, …Run Code Online (Sandbox Code Playgroud) 我尝试使用H2O为二分类问题创建了一些机器学习模型,测试结果相当不错。但后来我检查并发现了一些奇怪的东西。出于好奇,我试图打印模型对测试集的预测。而且我发现我的模型实际上一直在预测 0(负),但是 AUC 在 0.65 左右,精度不是 0.0。然后我尝试使用 Scikit-learn 来比较指标分数,并且(正如预期的那样)它们是不同的。Scikit 学习产生了 0.0 精度和 0.5 AUC 分数,我认为这是正确的。这是我使用的代码:
model = h2o.load_model(model_path)
predictions = model.predict(Test_data).as_data_frame()
# H2O version to print the AUC score
auc = model.model_performance(Test_data).auc()
# Python version to print the AUC score
auc_sklearn = sklearn.metrics.roc_auc_score(y_true, predictions['predict'].tolist())
Run Code Online (Sandbox Code Playgroud)
任何想法?提前致谢!
我有一个关于混淆矩阵的问题。
根据混淆矩阵的定义,它用于评估分类器输出的质量。
所以当你把数据拆分成训练集、测试集和验证集时,每个训练和测试都会给你一个不同的混淆矩阵。如果我想把它们加在一起我应该怎么做?
考虑我的以下截图代码:
X, Y = np.array(data[features]), np.array(data['target'])
logo = LeaveOneGroupOut()
group = data['id'].values
k_fold = KFold(n_splits=5)
scores =[]
per_person_true_y = []
per_person_pred_y = []
classifier_logistic = LogisticRegression()
for train, test in logo.split(X, Y, group):
x_train, x_test = X[train], X[test]
y_train, y_test = Y[train], Y[test]
classifier_logistic.fit(x_train, y_train.ravel())
y_predict = classifier_logistic.predict(x_test)
scores.append(metrics.accuracy_score(y_test,classifier_logistic.predict(x_test)))
per_person_true_y.append(y_test)
per_person_pred_y.append(y_predict)
plot.confusion_matrix( np.array(per_person_true_y),np.array(per_person_pred_y))
plt.show()
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误:
TypeError: unhashable type: 'numpy.ndarray'
Run Code Online (Sandbox Code Playgroud)
感谢您的评论。
我目前正在研究随机森林分类器,使用 gridsearch 来获得最佳参数
所以当我得到我的参数时,它们在我的 var 中:
params = {'bootstrap': 'True',
'criterion': 'entropy',
'max_depth': 'None',
'max_features': '3',
'min_samples_leaf': '4',
'min_samples_split': '3'}
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
clf = RandomForestClassifier(params)
但是这里params代替了n_estimators所以我有一些错误,例如:
ValueError: n_estimators must be an integer, got <class 'dict'>.
Run Code Online (Sandbox Code Playgroud) 我是 ML 的新手,只是触及了它的表面,所以如果我的问题没有意义,我深表歉意。
我有一些物体的连续测量序列(捕获它的重量、大小、温度……)和一个确定物体属性的离散列(整数的有限范围,比如 0,1,2)。这是我想预测的专栏。
有问题的数据确实是一个序列,因为属性列的值可能会根据围绕它的上下文而变化,并且序列本身也可能有一些循环属性。简而言之:数据的顺序对我很重要。
一个小例子如下表所示

请注意,有两行包含相同的数据,但在“属性”字段中具有不同的值。这个想法是属性字段的值可能取决于前面的行,因此行的顺序很重要。
我的问题是,我应该使用什么样的方法/工具/技术来解决这个问题?
我知道分类算法,但不知何故我认为它们不适用于这里,因为有问题的数据是连续的,我不想忽略这个属性。
我尝试使用 Keras LSTM 并假装 Property 列也是连续的。然而,我以这种方式获得的预测通常只是一个常数十进制值,在这种情况下没有意义。
解决此类问题的最佳方法是什么?
我正在使用 CNN 训练服装分类算法。我有大约 60000 张图像用于 10 个类的训练(训练和验证按 80:20 分割)。分离 10000 张图像进行测试。
训练准确度会随着时间的推移而提高,但验证准确度保持不变。训练损失也减少,但验证损失保持不变。

img_width, img_height = 28, 28
batch_size = 32
samples_per_epoch = 20000
validation_steps = 300
nb_filters1 = 32
nb_filters2 = 64
nb_filters3 = 128
conv1_size = 3
conv2_size = 2
pool_size = 2
classes_num = 10
epochs = 300
#learning_rate = 0.001
learning_rate = 0.01
decay_rate = learning_rate / epochs
momentum = 0.8
sgd = SGD(lr=learning_rate, momentum=momentum, decay=decay_rate,
nesterov=True)
model = Sequential()
model.add(
Convolution2D(nb_filters1, conv1_size, conv1_size, border_mode="same",
input_shape=(img_width, img_height, …Run Code Online (Sandbox Code Playgroud) validation classification image-processing deep-learning keras
我有一个包含 6000 个观察值的数据集;其示例如下:
job_id job_title job_sector
30018141 Secondary Teaching Assistant Education
30006499 Legal Sales Assistant / Executive Sales
28661197 Private Client Practitioner Legal
28585608 Senior hydropower mechanical project manager Engineering
28583146 Warehouse Stock Checker - Temp / Immediate Start Transport & Logistics
28542478 Security Architect Contract IT & Telecoms
Run Code Online (Sandbox Code Playgroud)
目标是根据职位预测每一行的职位部门。
首先,我对该job_title列应用了一些预处理:
def preprocess(document):
lemmatizer = WordNetLemmatizer()
stemmer_1 = PorterStemmer()
stemmer_2 = LancasterStemmer()
stemmer_3 = SnowballStemmer(language='english')
# Remove all the special characters
document = re.sub(r'\W', ' ', document)
# …Run Code Online (Sandbox Code Playgroud) classification ×10
python ×8
scikit-learn ×4
keras ×3
tensorflow ×2
arguments ×1
doc2vec ×1
gensim ×1
h2o ×1
lstm ×1
nlp ×1
numpy ×1
python-3.x ×1
random ×1
rnn ×1
svc ×1
svm ×1
validation ×1
valueerror ×1