小编Lau*_*uza的帖子

使用 pytest 的“间接夹具”错误。怎么了?

 def fatorial(n):
    if n <= 1:
        return 1
    else:
        return n*fatorial(n - 1)


import pytest

@pytest.mark.parametrize("entrada","esperado",[
    (0,1),
    (1,1),
    (2,2),
    (3,6),
    (4,24),
    (5,120)
])

def testa_fatorial(entrada,esperado):
    assert fatorial(entrada)  == esperado
Run Code Online (Sandbox Code Playgroud)

错误:

 ERROR collecting Fatorial_pytest.py ____________________________________________________________________
In testa_fatorial: indirect fixture '(0, 1)' doesn't exist
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我得到了“间接夹具”。知道吗?我使用的是 python 3.7 和 windows 10 64 位。

python pytest python-3.x python-3.7

20
推荐指数
1
解决办法
6256
查看次数

使用Python计算HTTP头的Shannon熵.怎么做?

香农熵是:

香农

\r\n\r\n is the end of a HTPP header:
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

不完整的HTTP标头:

不完整的HTTP标头

我有一个PCAP格式的网络转储(dump.pcap),我试图 使用Python 计算HTTP协议中包含\r\n\r\n 和不包含数据包的数量的熵\r\n\r\n并比较它们.我用以下方法读取数据包:

import pyshark

pkts = pyshark.FileCapture('dump.pcap')
Run Code Online (Sandbox Code Playgroud)

我认为Ti在shannon公式中是我的转储文件的数据.

dump.pcap:https://uploadfiles.io/y5c7k

我已经计算了IP号的熵:

import numpy as np
import collections

sample_ips = [
    "131.084.001.031",
    "131.084.001.031",
    "131.284.001.031",
    "131.284.001.031",
    "131.284.001.000",
]

C = collections.Counter(sample_ips)
counts = np.array(list(C.values()),dtype=float)
#counts  = np.array(C.values(),dtype=float)
prob    = counts/counts.sum()
shannon_entropy = (-prob*np.log2(prob)).sum()
print (shannon_entropy)
Run Code Online (Sandbox Code Playgroud)

任何的想法?是否有可能计算与HTTP协议的分组的数目的熵\r\n\r\n 和没有\r\n\r\n在头或它是一个无义想法?

转储的几行:

wireshark的HTTP过滤器

 30 2017/246 11:20:00.304515    192.168.1.18    192.168.1.216   HTTP    339 GET / HTTP/1.1 


    GET / HTTP/1.1 …
Run Code Online (Sandbox Code Playgroud)

python entropy python-2.7 python-3.x

10
推荐指数
1
解决办法
765
查看次数

Keras中的ValueError:我怎样才能安装模型?

我正在尝试使用keras来拟合我的模型(神经网络),但我得到了ValueError错误.

进口keras

from keras.models import Sequential

from keras.layers import Dense


classificador_rede_neural = Sequential()


# # Camadas Ocultas e de Saída

# camadas ocultas = (entradas + saídas)/2 #estimando o numero de neurônios em camada oculta
# 
# temos:len(train.columns) - 1   atributos previsores
# 
# 1 classe


#len(train.columns)


camadas_ocultas = round(len(train.columns)/2)



print(camadas_ocultas)


classificador_rede_neural.add(Dense(units=camadas_ocultas, activation='relu',input_dim =len(train.columns) ))#primeira camada


classificador_rede_neural.add(Dense(units=camadas_ocultas, activation='relu' ))#segunda camada


classificador_rede_neural.add(Dense(units=1, activation='sigmoid' ))#camada de saída. a saída é binária, logo units=1
Run Code Online (Sandbox Code Playgroud)

classificador_rede_neural.compile(优化= '亚当',损耗= 'binary_crossentropy',度量= [ '准确性'])

classificador_rede_neural.fit(X_train2,y_train2,batch_size=10,epochs =100)
Run Code Online (Sandbox Code Playgroud)

我收到错误:

 ValueError: …
Run Code Online (Sandbox Code Playgroud)

neural-network python-3.x keras

5
推荐指数
1
解决办法
8370
查看次数

试图理解面向对象编程。如何使用对象组合?

是两个类Point()Circle()定义如下:

class Point:
    def __init__(self, x, y):
        self._x = x
        self._y = y

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, x):
        self._x = x

    @property
    def y(self):
        return self._y

    @y.setter
    def y(self, y):
        self._y = y

    def __repr__(self):
        return f"{self._x}, {self._y}"

    def move(self, x, y):
        self._x = x
        self._y = y
        return self._x, self._y


class Circle:
    def __init__(self, radius, x, y):
        self._radius = radius
        self.x = x
        self.y = y

    def move(self, x, y):
        Point.move(self, …
Run Code Online (Sandbox Code Playgroud)

python oop python-3.x

4
推荐指数
1
解决办法
83
查看次数

使用LaTeX使算法更具可读性

我试图使我的算法在LaTeX中更具可读性:

\documentclass{IEEEtran}

\usepackage{algpseudocode}
\usepackage{algorithm}

\begin{document}

    \begin{algorithm}[H]
    \caption{Detecta \textit{Slowloris}}
    \begin{algorithmic}[1]

        \Function{Divide\_slices\_1min }{Arquivo PCAP}
        \State \Return \textit{sliceAtual}
        \EndFunction
        \Function{Separa\_IP\_Origem\_Destino}{sliceAtual}
        \State \Return $ArquivoIPs$
        \EndFunction
        \Function{Calcula\_entropia\_IP\_Origem\_Destino}{ArquivoIP}
        \State \Return $EntroSliceAtualIP$
        \EndFunction
        \Function{PVS}{sliceAtual}
        \State \Return $PVS_SliceAtual$
        \EndFunction

        \Function{FCS}{sliceAtual}
        \State \Return $FCS_SliceAtual$
        \EndFunction

        \If {$entropiaSliceAtual$ > $entropiaSemAtaq$ + 0,10*$entropiaSemAtaq$}
        \If {$PVS_SliceAtual$ > $PVS_SemAtaq$ + 0.60*$PVS_SemAtaq$}  
        \If {$FCS_SliceAtual$ > $FCS_SemAtaq$ - 0.40*$FCS_SemAtaq$}

        \State \Return $Ataque$
        \Else
        \State \Return $SemAtaques$
        \EndIf
        \EndIf
        \EndIf

    \end{algorithmic}
    \end{algorithm}
\end{document}
Run Code Online (Sandbox Code Playgroud)

结果不是很好:

在此处输入图片说明

我希望它只显示对函数的调用而没有太多的结尾。

我想要这样的东西:

我想要这样的东西

有什么建议吗?我正在努力!IEEEtran类为:https ://ctan.org/pkg/ieeetran ? lang = en

latex

3
推荐指数
1
解决办法
2443
查看次数

使用 DBSCAN 进行聚类:如果不提前设置聚类数量,如何训练模型?

我正在使用 sklearn 的内置数据集 iris 进行聚类。在 KMeans 中,我预先设置了簇的数量,但对于 DBSCAN 来说并非如此。如果不提前设置簇数,如何训练模型?

我试过:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#%matplotib inline

from sklearn.cluster import DBSCAN,MeanShift
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split,KFold,cross_val_score
from sklearn.metrics import accuracy_score,confusion_matrix

iris = load_iris()
X = iris.data
y = iris.target

dbscan = DBSCAN(eps=0.3,min_samples=10)

dbscan.fit(X,y)
Run Code Online (Sandbox Code Playgroud)

我已经被困住了!

python cluster-analysis machine-learning dbscan scikit-learn

0
推荐指数
1
解决办法
7349
查看次数