我正在尝试使用10倍交叉验证来估计逻辑回归.
#import libraries
library(car); library(caret); library(e1071); library(verification)
#data import and preparation
data(Chile)
chile <- na.omit(Chile) #remove "na's"
chile <- chile[chile$vote == "Y" | chile$vote == "N" , ] #only "Y" and "N" required
chile$vote <- factor(chile$vote) #required to remove unwanted levels
chile$income <- factor(chile$income) # treat income as a factor
Run Code Online (Sandbox Code Playgroud)
目标是估计一个glm模型,该模型预测投票"Y"或"N"的结果取决于相关的解释变量,并且基于最终模型,计算混淆矩阵和ROC曲线以掌握不同阈值水平的模型行为.
型号选择导致:
res.chileIII <- glm(vote ~
sex +
education +
statusquo ,
family = binomial(),
data = chile)
#prediction
chile.pred <- predict.glm(res.chileIII, type = "response")
Run Code Online (Sandbox Code Playgroud)
产生:
> head(chile.pred)
1 2 …
Run Code Online (Sandbox Code Playgroud) 我有一个情感分析的任务,使用本该即时通讯语料库的意见有5类(very neg
,neg
,neu
,pos
,very pos
),从1到5,所以我做的分类如下:
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
tfidf_vect= TfidfVectorizer(use_idf=True, smooth_idf=True,
sublinear_tf=False, ngram_range=(2,2))
from sklearn.cross_validation import train_test_split, cross_val_score
import pandas as pd
df = pd.read_csv('/corpus.csv',
header=0, sep=',', names=['id', 'content', 'label'])
X = tfidf_vect.fit_transform(df['content'].values)
y = df['label'].values
from sklearn import cross_validation
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X,
y, test_size=0.33)
from sklearn.svm import SVC
svm_1 = SVC(kernel='linear')
svm_1.fit(X, y)
svm_1_prediction = svm_1.predict(X_test)
Run Code Online (Sandbox Code Playgroud)
然后根据指标我获得了以下混淆矩阵和分类报告,如下:
print '\nClasification report:\n', classification_report(y_test, …
Run Code Online (Sandbox Code Playgroud) 我使用sklearn包中的confusion_matrix()方法为我的分类器计算了一个混淆矩阵.混淆矩阵的对角元素表示预测标签等于真实标签的点的数量,而非对角线元素是由分类器错误标记的元素.
我想将我的混淆矩阵归一化,使其仅包含0到1之间的数字.我想从矩阵中读取正确分类的样本的百分比.
我发现了几种方法如何标准化矩阵(行和列标准化),但我对数学知之甚少,并且不确定这是否是正确的方法.有人可以帮忙吗?
我有一个用sklearn.metrics.confusion_matrix
.
现在,我想用 来绘制它sklearn.metrics.plot_confusion_matrix
,但第一个参数是经过训练的分类器,如文档中所指定。问题是我没有分类器;结果是通过手动计算获得的。
是否仍然可以通过 scikit-learn 在一行中绘制混淆矩阵,还是必须使用 matplotlib 自己编写代码?
我正在尝试运行下面的代码。
from sklearn.metrics import plot_confusion_matrix
Run Code Online (Sandbox Code Playgroud)
我收到以下错误。
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-66-ebfa5cfc2bb8> in <module>
----> 1 from sklearn.metrics import plot_confusion_matrix
ImportError: cannot import name 'plot_confusion_matrix' from 'sklearn.metrics' (C:\Program Files\Anaconda3\lib\site-packages\sklearn\metrics\__init__.py)
Run Code Online (Sandbox Code Playgroud) 我正在使用此页面执行逻辑回归.我的代码如下.
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
mylogit <- glm(admit ~ gre, data = mydata, family = "binomial")
summary(mylogit)
prob=predict(mylogit,type=c("response"))
mydata$prob=prob
Run Code Online (Sandbox Code Playgroud)
运行此代码后,mydata dataframe有两列 - 'admit'和'prob'.这两列不应该足以获得ROC曲线吗?
如何获得ROC曲线.
其次,通过嘲笑mydata,似乎模型正在预测可能性admit=1
.
那是对的吗?
如何找出模型预测的特定事件?
谢谢
更新:似乎以下三个命令非常有用.它们提供了最大精度的截止点,然后有助于获得ROC曲线.
coords(g, "best")
mydata$prediction=ifelse(prob>=0.3126844,1,0)
confusionMatrix(mydata$prediction,mydata$admit
Run Code Online (Sandbox Code Playgroud) 如何从Naive Bayes模型计算每个班级的准确度,精确度和召回率?我正在使用嵌入式数据集:iris和package tree以及针对Naive Bayes的e1071包.混淆矩阵如下:
prediction setosa versicolor virginica
setosa 29 0 0
versicolor 0 20 2
virginica 0 3 21
Run Code Online (Sandbox Code Playgroud)
PS:请注意,我使用75个条目作为训练集,其他用于测试:
iris.train <- c(sample(1:150, 75)) # have selected 75 randomly
Run Code Online (Sandbox Code Playgroud) 我得到一个
Classification metrics can't handle a mix of multilabel-indicator and multiclass targets
Run Code Online (Sandbox Code Playgroud)
当我尝试使用混淆矩阵时出错。
我正在做我的第一个深度学习项目。我是新手。我正在使用由 keras 提供的 mnist 数据集。我已经成功地训练和测试了我的模型。
但是,当我尝试使用 scikit learn 混淆矩阵时,我收到了上述错误。我已经搜索了一个答案,虽然有关于这个错误的答案,但没有一个对我有用。从我在网上找到的内容来看,它可能与损失函数有关(我categorical_crossentropy
在我的代码中使用了)。我尝试将其更改为,sparse_categorical_crossentropy
但这只是给了我
Error when checking target: expected dense_2 to have shape (1,) but got array with shape (10,)
Run Code Online (Sandbox Code Playgroud)
当我fit()
在模型上运行该函数时。
这是代码。(为简洁起见,我省略了进口)
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(28 * 28,)))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 …
Run Code Online (Sandbox Code Playgroud) 我有两个混淆矩阵,计算值为真阳性(tp),假阳性(fp),真阴性(tn)和假阴性(fn),对应两种不同的方法.我想把它们表示为
我相信facet grid或facet wrap可以做到这一点,但我发现很难开始.这是与method1和method2相对应的两个混淆矩阵的数据
dframe<-structure(list(label = structure(c(4L, 2L, 1L, 3L, 4L, 2L, 1L,
3L), .Label = c("fn", "fp", "tn", "tp"), class = "factor"), value = c(9,
0, 3, 1716, 6, 3, 6, 1713), method = structure(c(1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L), .Label = c("method1", "method2"), class = "factor")), .Names = c("label",
"value", "method"), row.names = c(NA, -8L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud) 我正在使用model.fit_generator来训练并获得我的二进制(两个类)模型的结果,因为我直接从我的文件夹中提供输入图像.如何在这种情况下得到混淆矩阵(TP,TN,FP,FN),因为我通常使用confusion_matrix
命令sklearn.metrics
来获取它,这需要predicted
和actual
标签.但在这里我没有两者.可能是我可以从predict=model.predict_generator(validation_generator)
命令计算预测标签.但我不知道我的模型是如何从我的图像中获取输入标签的.我的输入文件夹的一般结构是:
train/
class1/
img1.jpg
img2.jpg
........
class2/
IMG1.jpg
IMG2.jpg
test/
class1/
img1.jpg
img2.jpg
........
class2/
IMG1.jpg
IMG2.jpg
........
Run Code Online (Sandbox Code Playgroud)
我的代码的一些块是:
train_generator = train_datagen.flow_from_directory('train',
target_size=(50, 50), batch_size=batch_size,
class_mode='binary',color_mode='grayscale')
validation_generator = test_datagen.flow_from_directory('test',
target_size=(50, 50),batch_size=batch_size,
class_mode='binary',color_mode='grayscale')
model.fit_generator(
train_generator,steps_per_epoch=250 ,epochs=40,
validation_data=validation_generator,
validation_steps=21 )
Run Code Online (Sandbox Code Playgroud)
所以上面的代码自动接受两个类输入,但我不知道它认为是哪个类0和哪个类1.
confusion-matrix ×10
scikit-learn ×5
python ×4
r ×4
keras ×2
ggplot2 ×1
glm ×1
matrix ×1
naivebayes ×1
nlp ×1
r-caret ×1
regression ×1
roc ×1
svm ×1