我通过scikit-learn图书馆学习机器学习.我使用以下代码将决策树分类器和随机森林分类器应用于我的数据:
def decision_tree(train_X, train_Y, test_X, test_Y):
clf = tree.DecisionTreeClassifier()
clf.fit(train_X, train_Y)
return clf.score(test_X, test_Y)
def random_forest(train_X, train_Y, test_X, test_Y):
clf = RandomForestClassifier(n_estimators=1)
clf = clf.fit(X, Y)
return clf.score(test_X, test_Y)
Run Code Online (Sandbox Code Playgroud)
为什么随机森林分类器的结果更好(100次运行,随机抽样2/3的数据用于训练,1/3用于测试)?
100%|???????????????????????????????????????| 100/100 [00:01<00:00, 73.59it/s]
Algorithm: Decision Tree
Min : 0.3883495145631068
Max : 0.6476190476190476
Mean : 0.4861783113770316
Median : 0.48868030937802126
Stdev : 0.047158171852401135
Variance: 0.0022238931724605985
100%|???????????????????????????????????????| 100/100 [00:01<00:00, 85.38it/s]
Algorithm: Random Forest
Min : 0.6846846846846847
Max : 0.8653846153846154
Mean : 0.7894823428836184
Median : 0.7906101571063208
Stdev : 0.03231671150915106
Variance: 0.0010443698427656967 …Run Code Online (Sandbox Code Playgroud) python machine-learning decision-tree random-forest scikit-learn
我正在尝试分析我的代码以检测瓶颈。我搜索了一些分析器,但从未找到我要找的东西。
我过去使用过很多Python,有一个软件: line_profiler,它给出了这样的回报:
0 Line Hits Time Per Hit % Time Line Contents
11 @profile
12 def compute_prior(folder):
13 """
14 Given a folder, we compute the prior of neg and pos
15 folder = "./movie-reviews-en/train/"
16 """
17 # we compute the number of positive reviews
18 3 1719 573.0 52.9 number_positive = len([f for f in listdir(folder + "pos/")])
19 # then the negative
20 3 1512 504.0 46.6 number_negative = len([f for f in listdir(folder + …Run Code Online (Sandbox Code Playgroud) 我正在尝试解析这个forme的文件:
12
0,1,2,3,1,2,3,4,2,3,4,5
1,0,1,2,2,1,2,3,3,2,3,4
2,1,0,1,3,2,1,2,4,3,2,3
3,2,1,0,4,3,2,1,5,4,3,2
1,2,3,4,0,1,2,3,1,2,3,4
2,1,2,3,1,0,1,2,2,1,2,3
3,2,1,2,2,1,0,1,3,2,1,2
4,3,2,1,3,2,1,0,4,3,2,1
2,3,4,5,1,2,3,4,0,1,2,3
3,2,3,4,2,1,2,3,1,0,1,2
4,3,2,3,3,2,1,2,2,1,0,1
5,4,3,2,4,3,2,1,3,2,1,0
0,5,2,4,1,0,0,6,2,1,1,1
5,0,3,0,2,2,2,0,4,5,0,0
2,3,0,0,0,0,0,5,5,2,2,2
4,0,0,0,5,2,2,10,0,0,5,5
1,2,0,5,0,10,0,0,0,5,1,1
0,2,0,2,10,0,5,1,1,5,4,0
0,2,0,2,0,5,0,10,5,2,3,3
6,0,5,10,0,1,10,0,0,0,5,0
2,4,5,0,0,1,5,0,0,0,10,10
1,5,2,0,5,5,2,0,0,0,5,0
1,0,2,5,1,4,3,5,10,5,0,2
1,0,2,5,1,0,3,0,10,0,2,0
Run Code Online (Sandbox Code Playgroud)
第一行给出矩阵的大小:n x n
n跟随线是矩阵D.然后n跟随线是矩阵W.所以2n + 1有线.
这是解析它并将其放入变量的代码.
func readFile(path string) (int64, Matrix, Matrix) {
// open the file
f, _ := os.Open(path)
defer f.Close()
// init the new reader on the opened file
r := bufio.NewReader(f)
// we get the n value
line, _ := r.ReadString('\n')
splitedLine := strings.Fields(line)
tmp, _ …Run Code Online (Sandbox Code Playgroud)