小编Md.*_*que的帖子

如何从python中的key,value创建字典列表

如何从现有的键值列表创建字典词典?

celebr = ['Tony','Harry','Katty','Sam']
perc = [69,31,0,0]


d = dict(zip(celebr, perc))


dlist = []

for i in d.items():
    dlist.append(i)

print(dlist)
Run Code Online (Sandbox Code Playgroud)

输出:

[('Tony': 69), ('Harry': 31), ('Katty': 0), ('Sam': 0)]
Run Code Online (Sandbox Code Playgroud)

当我使用d.items时,它会自动给我元组,而不是字典.有没有优雅的方法来创建字典列表而不是元组?

需要获得以下结构:

[{'Tony': 69}, {'Harry': 31}, {'Katty': 0}, {'Sam': 0}]
Run Code Online (Sandbox Code Playgroud)

python dictionary python-3.x

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

从python中的给定模式(通配符)生成所有二进制字符串

给定一个模式,我们需要通过填充模式中缺少的位置来生成所有可能的二进制数0 and 1.

例如

Pattern = "x1x";
Run Code Online (Sandbox Code Playgroud)

输出:

010 
110 
011 
111
Run Code Online (Sandbox Code Playgroud)

python

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

计算训练集的混淆矩阵

我是机器学习的新手。最近,我已经学会了如何计算confusion_matrix对Test set的KNN Classification。但我不知道,如何计算confusion_matrix为Training set的KNN Classification?

我如何可以计算confusion_matrix为Training set的KNN Classification从下面的代码?

下面的代码是用于计算confusion_matrix为Test set:

# Split test and train data
import numpy as np
from sklearn.model_selection import train_test_split
X = np.array(dataset.ix[:, 1:10])
y = np.array(dataset['benign_malignant'])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

#Define Classifier
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = …
Run Code Online (Sandbox Code Playgroud)

python machine-learning knn cross-validation

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

ArrayList第一个元素查找

我是新手Collection Framework.我正在运行一个java程序ArrayList().当我尝试循环它以获取它的元素,但它抛出错误就像

HelloWorld.java:15: error: cannot find symbol                                                                                                                                   
           for(int k=0;k<al.length;k++)                                                                                                                                         
                           ^                                                                                                                                                    
  symbol:   variable length                                                                                                                                                     
  location: variable al of type ArrayList<String>                                                                                                                               
HelloWorld.java:17: error: array required, but ArrayList<String> found                                                                                                          
             System.out.println("elements are"+al[k]);  
Run Code Online (Sandbox Code Playgroud)

这是我写的代码.

import java.util.ArrayList;

public class HelloWorld{
   public static void main(String[] args) {
       ArrayList<String> al = new ArrayList<String>();
       al.add("pen");
       al.add("pencil");
       al.add("ink");
       al.add("notebook");
       al.add("book");
       al.add("books");
       al.add("paper");
       al.add("white board");

       for(int k=0;k<al.length;k++)
   {
         System.out.println("elements are"+al[k]);
  }
   }
}
Run Code Online (Sandbox Code Playgroud)

帮我指出我的错误.提前致谢

java arrays arraylist

2
推荐指数
1
解决办法
2158
查看次数

如何在 X_train、y_train、X_test、y_test 中分割图像数据集?

我有一个类似于以下结构的数据集:

Dataset/
   |
   |
   -----Pothole/
   |         |
   |         ------ umm001.jpg
   |         |
   |         ------ abd.jpg
   |         |
   |         ------ 
   |         |
   |
   |
   ----Road/
         |
         ------road005.jpg
         |
         ------ummm.jpg
         |
         ------
         |
Run Code Online (Sandbox Code Playgroud)

我想将此数据集拆分为X_train, y_train, X_test, y_test.

这样:

### data: shuffled and split between train and test
(X_train, y_train), (X_test, y_test) = mnist.load_data()
Run Code Online (Sandbox Code Playgroud)

或者,

(X_train, y_train), (X_test, y_test) = train_test_split(X,y, test_size=0.20)
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

python dataset pandas

2
推荐指数
1
解决办法
5444
查看次数

从python中的字符串中获取特定的子字符串

如何存储lst1 = [26.7,8.2,13.7,8.6,16]从Train_level1的python?

Train_level1 = ['1_Rehana_Karim_F49_850_GL=26.7','43_G.M.Alauddin Azad_M42_940_GL=8.2','110_Ronojeet_Bishwash_M47_940_GL=13.7','112_Mustafizur_Rahman_M60_850_GL=8.6','123_Farida_Yeasmin_F55_940_GL=16']
Run Code Online (Sandbox Code Playgroud)

同样,如何存储lst2 = [11.5,12.9,9.2]从Train_level2的python?

Train_level2 = ['S140_M75_GL=11.5-850LEDFon_F=110.jpg', 'S85_F56_GL=12.9-940LEDFon_F=105.jpg', 'S108_M71_GL=9.2-850LEDFon_F=100.jpg']
Run Code Online (Sandbox Code Playgroud)

python list python-3.x

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

如何从二值图像中找到四个点?

我有一个如下所示的图像,我想从该图像中找到四个坐标(角)。

我尝试过以下代码:

# dilate thresholded image - merges top/bottom 
kernel = np.ones((3,3), np.uint8)
dilated = cv2.dilate(img, kernel, iterations=3)
# Finding contours for the thresholded image
contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

首先,我扩大了图像,使其填充散点部分,并尝试从那里找出轮廓。但它给了我错误的输出。我可以做什么来找出四个角坐标?

python opencv numpy

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

Python“赋值前引用局部变量”的问题

我对 shell 的说法有疑问local variable referenced before assignment,并且认为以前的任何答案都没有帮助。我可以对此代码有一些具体的建议吗:

import random

marginalCheck = random.randint(0,1)

print("This is the political campaign simulator")

global popularity

popularity = 50

def unpopularT():
    if popularity <= 30 and popularity < 0:
        print("You are decreasing in popularity.")
        popularityRecover = input("Do you wish to carry on or resign. If this carries on, you are likely to lose to a landslide by your Labour opposition")

        if popularityRecover == "resign":
            print("You have become an infamous, unpopular politician. You are remembered …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

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

awk:如果列空无效

互联网上的每个人都说你应该用这个:

if ($2 == "")
Run Code Online (Sandbox Code Playgroud)

或这个:

if (!length($2))
Run Code Online (Sandbox Code Playgroud)

或这个:

if (!$2)
Run Code Online (Sandbox Code Playgroud)

所有这些对我都不起作用.我得了一个csv,用";"分隔 有2列.在某些情况下,第二列是空的(我可以在gedit中看到).

我想区分该2 sets文件(2 sets = last column empty vs last column not empty).如果我只是print length($2),我看到第2列中没有任何内容的行实际上长度为1.

所以那里似乎有东西,但我看不出它是什么?

如何使该东西可见,使长度等于1?它似乎阻止了上面的3个表达式的匹配.

awk

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

Swift编译器错误:缺少参数标签

我跑Swift的Xcode 8.3.3。在这里,我得到一个有关闭包的错误。

看来这部分是正确的,[var intro = introToFriends("Jim", "Pam") intro]但没有单击。

怎么了

在此处输入图片说明

xcode ios swift

-1
推荐指数
1
解决办法
411
查看次数