以下是我的文本文件中的数据集.
2.1,3.5,1.4,0.2,Iris
4.9,3.0,1.4,0.2,Ilia
3.7,3.2,1.3,0.2,Iridium
Run Code Online (Sandbox Code Playgroud)
有一个名为的列表:
list_of_keys
Run Code Online (Sandbox Code Playgroud)
它在列表中包含以下值
['S_Length','S_Width','P_Length','P_Width','Predicate']
Run Code Online (Sandbox Code Playgroud)
所以,问题是,我想创建一个字典列表来保存我的所有数据(从文本文件)使用list_of_keys as keys
字典,如下所示:
dict =
{'S_Length': 2.1, 'S_Width':3.5 , 'P_Length': 1.4, 'P_Width': 0.2, 'Predicate': Iris},
{'S_Length': 4.9, 'S_Width':3.0 , 'P_Length': 1.4, 'P_Width': 0.2, 'Predicate': Ilia},
... so on!
Run Code Online (Sandbox Code Playgroud)
我到现在为止:
# store all data from the text files as list
all_examples = file.readlines()
for outer_index in range(len(all_examples)):
for inner_index in range(0, len(list_of_keys)+1):
Run Code Online (Sandbox Code Playgroud) 我有一个从文本文件中读入的以下数据集:
all_examples= ['A,1,1', 'B,2,1', 'C,4,4', 'D,4,5']
Run Code Online (Sandbox Code Playgroud)
我需要创建一个字典列表如下:
lst = [
{"A":1, "B":2, "C":4, "D":4 },
{"A":1, "B":1, "C":4, "D":5 }
]
Run Code Online (Sandbox Code Playgroud)
我尝试使用生成器函数,但很难创建一个列表.
attributes = 'A,B,C'
def get_examples():
for value in examples:
yield dict(zip(attributes, value.strip().replace(" ", "").split(',')))
Run Code Online (Sandbox Code Playgroud) 我正在致力于在 Python 中实现 k-means 聚类。为数据集选择初始质心的好方法是什么?例如:我有以下数据集:
A,1,1
B,2,1
C,4,4
D,4,5
Run Code Online (Sandbox Code Playgroud)
我需要创建两个不同的集群。我如何从质心开始?
为什么我们要选择"最佳"属性?如果我们从任何其他属性中选择,会有什么不同吗?
我不认为这是因为该功能的范围,但我得到一个
尚未解析的引用,位于get_all_predicates(examples).count(predicate_list [0])
get_entropy_of_attributes(examples, predicate_list)
我班上的内部函数Tree
:
class Tree:
def get_examples(examples, attributes):
for value in examples:
yield dict(zip(attributes, value.strip().replace(" ", "").split(',')))
def get_all_predicates(examples):
return [d['Predicate'] for d in examples]
def get_entropy_of_attributes(examples, predicate_list):
get_all_predicates(examples).count(predicate_list[0])
return 0
examples = list(get_examples(all_examples, name_of_attributes))
predicate_list = list(set(get_all_predicates(examples)))
get_entropy_of_attributes(examples, predicate_list)
Run Code Online (Sandbox Code Playgroud)
all_examples
是词典name_of_attributes
列表,并且是列表,其中包含从文本文件导入的值。
all_examples = [{'P_Length': '1.4', 'P_Width': '0.2', 'Predicate': 'I-setosa', 'Sepal_Width': '3.5', 'S_Length': '5.1'}, ...]
name_of_attributes = ["Check","P-Width"]
Run Code Online (Sandbox Code Playgroud)
有什么帮助吗?
我在字典列表中有以下数据:
data = [{'I-versicolor': 0, 'Sepal_Length': '7.9', 'I-setosa': 0, 'I-virginica': 1},
{'I-versicolor': 0, 'I-setosa': 1, 'I-virginica': 0, 'Sepal_Width': '4.2'},
{'I-versicolor': 2, 'Petal_Length': '3.5', 'I-setosa': 0, 'I-virginica': 0},
{'I-versicolor': 1.2, 'Petal_Width': '1.2', 'I-setosa': 0, 'I-virginica': 0}]
Run Code Online (Sandbox Code Playgroud)
为了获得基于键和值的列表,我使用以下内容:
next((item for item in data if item["Sepal_Length"] == "7.9"))
Run Code Online (Sandbox Code Playgroud)
但是,所有字典都不包含密钥Sepal_Length
,我得到:
KeyError: 'Sepal_Length'
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
我有一个以下字典:
centroid = {'A': [1.0, 1.0], 'B': [2.0, 1.0]}
Run Code Online (Sandbox Code Playgroud)
使用上面的字典我创建了两个不同的字典并将它们附加到列表中:
for key in centroids:
clusters_list.append(dict(zip(key, centroids.get(key))))
Run Code Online (Sandbox Code Playgroud)
但是,当我检查我的cluster_list时,我得到以下数据:
[{'A': 1.0}, {'B': 2.0}]
Run Code Online (Sandbox Code Playgroud)
而不是
[{'A': [1.0, 1.0]}, {'B': [2.0, 1.0]}]
.我怎样才能解决这个问题?
我有以下字典:
dic= {'I-setosa': 8, 'I-versicolor': 2, 'I-virginica': 0}
Run Code Online (Sandbox Code Playgroud)
如何找到密钥的总和?在Python 2
我可以做到以下几点:
sum(dic.values())
Run Code Online (Sandbox Code Playgroud)
除此之外,还有其他方法吗?我尝试使用dic.values()
并找到该列表的总和,但这太长了。
我有一个以下的Model类:
public class Student
{
public string Name { get; set; }
public string Major { get; set; }
public int Age { get; set; }
}
public string GetPrimaryMajor(List<Student> students)
{
...
}
Run Code Online (Sandbox Code Playgroud)
如何使用迭代和LINQ方法实现该方法GetPrimaryMajor()
以确定参数中最常出现Major
的students
方法?
python ×7
dictionary ×5
python-3.5 ×2
autolayout ×1
c# ×1
centroid ×1
data-mining ×1
function ×1
ios ×1
k-means ×1
linq ×1
list ×1
loops ×1
scope ×1
storyboard ×1