- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试打印 target_playlist
中的值。问题是我想按 percentuali
列对 target_playlist
中的值进行排序,我使用了 target_playlist.sort_values('percentuali', inplace=True, ascending=False)
在 sort_values
函数之前,结果:
print("{}".format(target_playlist['percentuali'][i]))
是:
0.7010264012452779
0.19662758090847976
0.6508863154849628
0.557740362863367
0.47418798688188313
0.6634307395184526
0.17661982395954637
0.6334661569944786
0.5226247859195567
0.37647399781797003
0.6107562358792401
0.10866013071895426
0.6259167928556538
0.5107723732317271
0.5107723732317271
0.440188723891383
0.473270990299173
0.5807994015581672
0.45540535868625753
0.4156854080449265
0.5659237264842225
0.5942257114281826
0.5763053500588216
0.43676171660260443
0.6947640279542424
0.37155299947773396
0.6055124707313475
0.6642522917728619
0.6339323841512609
0.6836084778718268
0.4585485761594801
0.7687767193517359
0.7739306342996543
0.6792746883779797
0.5688985142793829
0.5763507447689178
0.6265388222033668
0.5262211637961803
0.631776719351736
0.7016345319242638
0.6549247063300238
0.6218895455057429
0.3926510809451985
0.5081035167373568
0.6149459682682933
0.44069739392952245
0.46799465192894985
0.69161263493496
0.5534053586862575
0.6968509819258842
0.4988988577428972
0.5059165111353879
0.7355655050414504
0.6792746883779797
0.4401208506283063
0.49320548887003335
0.5112768045242271
0.7361528565218765
0.2329438202247191
0.6123902228073447
0.49864712823852325
0.6909989415739581
0.6754433860184025
0.566520509644565
0.37663089180304893
0.6529677236233883
0.6089596366830047
0.7687767193517359
0.6101347817993262
0.7559795411177228
而当我在调用 sort_values
后打印值时,它们是:
Titolo: Possibili Scenari, Artista: Cesare Cremonini, Probabilita: 0.7559795411177228
Titolo: Shallow, Artista: Lady Gaga, Probabilita: 0.7559795411177228
Titolo: To the Trees, Artista: An Early Bird, Probabilita: 0.7559795411177228
Titolo: If You Wanna Love Somebody - Acoustic, Artista: Tom Odell, Probabilita: 0.7559795411177228
Titolo: Happier - Acoustic, Artista: Ed Sheeran, Probabilita: 0.7559795411177228
Titolo: Lie With Me, Artista: Josiah and the Bonnevilles, Probabilita: 0.7559795411177228
Titolo: Jubilee Road, Artista: Tom Odell, Probabilita: 0.7559795411177228
Titolo: I'll Never Love Again - Film Version, Artista: Lady Gaga, Probabilita: 0.7559795411177228
Titolo: Rise - Acoustic, Artista: Jonas Blue, Probabilita: 0.7559795411177228
Titolo: Hold My Girl, Artista: George Ezra, Probabilita: 0.7559795411177228
Titolo: Love Someone, Artista: Lukas Graham, Probabilita: 0.7559795411177228
Titolo: Angels, Artista: Tom Walker, Probabilita: 0.7559795411177228
Titolo: These Days (feat. Jess Glynne, Macklemore & Dan Caplen) - Acoustic, Artista: Rudimental, Probabilita: 0.7559795411177228
Titolo: Just For Tonight - Acoustic, Artista: James Bay, Probabilita: 0.7559795411177228
Titolo: Perfect, Artista: Ed Sheeran, Probabilita: 0.7559795411177228
Titolo: No Roots, Artista: Joshua Hyslop, Probabilita: 0.7559795411177228
Titolo: Slide, Artista: James Bay, Probabilita: 0.7559795411177228
Titolo: Be Your Man, Artista: Rhys Lewis, Probabilita: 0.7559795411177228
Titolo: No Matter What, Artista: Calum Scott, Probabilita: 0.7559795411177228
Titolo: Woes, Artista: Tom Rosenthal, Probabilita: 0.7559795411177228
Titolo: Barbed Wire (Acoustic), Artista: Tom Grennan, Probabilita: 0.7559795411177228
Titolo: Stay Awake with Me, Artista: Dan Owen, Probabilita: 0.7559795411177228
Titolo: Spent So Long, Artista: Jamie Harrison, Probabilita: 0.7559795411177228
Titolo: Tummy, Artista: Tamino, Probabilita: 0.7559795411177228
Titolo: LOVISA, Artista: FELIX SANDMAN, Probabilita: 0.7559795411177228
Titolo: Girl - Acoustic, Artista: SYML, Probabilita: 0.7559795411177228
Titolo: Party Of One (feat. Sam Smith), Artista: Brandi Carlile, Probabilita: 0.7559795411177228
Titolo: Electricity - Acoustic, Artista: Silk City, Probabilita: 0.7559795411177228
Titolo: Leftovers, Artista: Dennis Lloyd, Probabilita: 0.7559795411177228
Titolo: Hand That You Hold, Artista: Dan Owen, Probabilita: 0.7559795411177228
Titolo: Company (feat. Molly Hammar), Artista: Paul Rey, Probabilita: 0.7559795411177228
Titolo: Too Good At Goodbyes - Edit, Artista: Sam Smith, Probabilita: 0.7559795411177228
Titolo: Need You Now - Acoustic, Artista: Dean Lewis, Probabilita: 0.7559795411177228
Titolo: Such A Simple Thing, Artista: Ray LaMontagne, Probabilita: 0.7559795411177228
Titolo: Acoustic, Artista: Billy Raffoul, Probabilita: 0.7559795411177228
Titolo: Don’t Matter To Me, Artista: Drake, Probabilita: 0.7559795411177228
Titolo: when the party's over, Artista: Billie Eilish, Probabilita: 0.7559795411177228
Titolo: Someone You Loved, Artista: Lewis Capaldi, Probabilita: 0.7559795411177228
Titolo: Collide, Artista: Tom Speight, Probabilita: 0.7559795411177228
Titolo: Fading Into Grey - Acoustic, Artista: Billy Lockett, Probabilita: 0.7559795411177228
Titolo: Never Let You Go (feat. John Newman) - Acoustic Version, Artista: Kygo, Probabilita: 0.7559795411177228
Titolo: T-Shirts, Artista: James Smith, Probabilita: 0.7559795411177228
Titolo: In My Head, Artista: Peter Manos, Probabilita: 0.7559795411177228
Titolo: Where Were You In The Morning?, Artista: Shawn Mendes, Probabilita: 0.7559795411177228
Titolo: come out and play, Artista: Billie Eilish, Probabilita: 0.7559795411177228
Titolo: Tear Me Down, Artista: Paul Rey, Probabilita: 0.7559795411177228
Titolo: Come As You Are, Artista: Imaginary Future, Probabilita: 0.7559795411177228
Titolo: Consequences - orchestra, Artista: Camila Cabello, Probabilita: 0.7559795411177228
Titolo: All I Am - Acoustic, Artista: Jess Glynne, Probabilita: 0.7559795411177228
这是我正在处理的程序的一部分
import tkinter as tk
from tkinter import font as tkfont
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
import spotipy
import spotipy.util as util
from numpy import integer
from tkinter import Radiobutton
sp = spotipy.Spotify()
from spotipy.oauth2 import SpotifyClientCredentials
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.cluster import KMeans
import itertools
import threading
import time
import sys
from operator import itemgetter, attrgetter, methodcaller
target_playlist = pd.DataFrame(newPlaylist_features)
if(algoritmo_scelto==1):
pred = c.predict(target_playlist[features])
p = c.predict_proba(target_playlist[features])
if(algoritmo_scelto==2):
pred = knn.predict(target_playlist[features])
p = knn.predict_proba(target_playlist[features])
if(algoritmo_scelto==3):
pred = forest.predict(target_playlist[features])
p = forest.predict_proba(target_playlist[features])
if(algoritmo_scelto==4):
pred = k_means.predict(target_playlist[features])
p = k_means.predict_proba(target_playlist[features])
likedSongs = 0
i = 0
for prediction in pred:
target_playlist['percentuali'] = p[i][1]
print("{}".format(target_playlist['percentuali'][i]))
i = i +1
target_playlist.sort_values('percentuali', inplace=True, ascending=False)
i=0
for prediction in pred:
if(prediction == 1):
print ("Titolo: " + target_playlist["song_title"][i] + ", Artista: "+ target_playlist["artist"][i] + ", Probabilita: {} ".format(target_playlist["percentuali"][i]))
likedSongs= likedSongs + 1
i = i +1
我哪里错了?
最佳答案
在此循环中,您将 "target_playlist['percentuali']"
系列设置为单个值:
i = 0
for prediction in pred:
target_playlist['percentuali'] = p[i][1]
print("{}".format(target_playlist['percentuali'][i]))
i = i +1
由于 "target_playlist['percentuali'] = p[i][1]"
将 "p[i][1]"
作为值应用于每一行.
如本例所示:
>>> for i in [0, 1, 2]:
... print(i)
... df['this'] = i
...
0
1
2
>>> df
id col_1 col_2 col_3 this
0 1 blue 15 True 2
1 2 red 25 False 2
2 3 orange 35 False 2
3 4 yellow 24 True 2
4 5 green 12 True 2
我不知道对象 p
但您应该将结果转换为 pd.Series
。您可以将那个整个循环修改为如下所示:
target_playlist['percentuali'] = pd.Series(item[1] for item in p)
print(target_playlist['percentuali'])
在您对 DataFrame 调用 sort_values
后,您的值将不会按降序打印,因为您是按索引 引用行,例如(0, 1, 2)
.
您可以通过重置索引来快速修复,请参阅下面的示例:
>>> df.sort_values('col_2', inplace=True, ascending=False)
>>> df
id col_1 col_2 col_3
2 3 orange 35 False
1 2 red 25 False
3 4 yellow 24 True
0 1 blue 15 True
4 5 green 12 True
>>> df['col_2'][0]
15
>>> df.reset_index(inplace=True)
>>> df['col_2'][0]
35
您可以像这样遍历行,而不是通过索引进行引用:
for _, row in df.iterrows():
print("Title: {}, Artist: {}, Probability: {}".format(
row['song_title'], row['artist'], row['percentuali']
))
关于python - 为什么 "sort_values"不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54113268/
我是 javascript 的新手(今天开始弄乱它)。 我正在尝试更改名为“bar”的元素(div)的高度。条形图将成为图表的一部分。 我可以毫无问题地将按钮连接到更改栏高度的函数。一切正常,除了条形
错误 -> “UIVIew”没有名为“addSubView”的成员 override func viewDidLoad() { super.viewDidLoad() // Do an
我在命令行工具项目中复制并粘贴了 main.swift 下面链接中的代码。 How do you use CGEventTapCreate in Swift? 它构建没有错误,但是当我运行时, gua
我在尝试编译我的代码时遇到以下错误。 ERROR! ..\myCode\CPOI.cpp:68:41: error: cannot dynamic_cast 'screenType' (of type
我正在尝试将多个字符串连接到一个我已为其分配内存的字符串指针。这是一个例子: char *finalNumString = malloc(sizeof(char)*1024); finalNumStr
我在使用 dup2() 和 pipe() 时遇到问题。 当我尝试将管道的写入端 dup2 到 STDOUT_FILENO 时,我收到了 EBADF。 我用 gdb 在 dup2(pout[1], ST
首先,我应该说我运行的是 Windows 7。 因此,今天早上我尝试像往常一样从我的存储库中提取数据,但我做不到。我得到了错误: The authenticity of host 'github.co
刚开始在虚拟环境中运行Python,乱用Django,无法激活虚拟环境。 花了最后 4 个小时尝试在本地终端/VS 代码上激活虚拟环境 (venv),但没有成功。 避免使用“sudo pip inst
Tidyverse 的粉丝经常给出使用小标题而不是数据框的几个优点。它们中的大多数似乎旨在保护用户免于犯错误。例如,与数据框不同,小标题: 不需要 ,drop=FALSE不从数据中删除维度的论据。 不
我一直在对 Elm 应用程序进行 docker 化时遇到问题。据我所知,我已经创建了一个完整且有效的 Docker 文件……但它不起作用。 我会解释的。 所以我的脚本在 3 个文件中运行。 首先是启动
我可以在 Controller 中使用@Autowired,例如 @RestController public class Index { @Autowired HttpServlet
我定义了一个方法和一个函数: def print(str:String) = println val intToString = (n:Int) => n.toString 现在我想创作它们。 我的问
当我控制台单独记录变量“pokemons”时,它确实返回一个数组。但是当我尝试映射它时,出现错误: TypeError: pokemons.map is not a function 我的代码: im
每当我尝试在 Python 解释器中导入 smtplib 时,都会收到此错误: ImportError: cannot import name fix_eols 我该如何解决这个问题? 编辑:这是完整
我正在使用 Meteor.js 开发一个项目,但在使用 Handlebar 时遇到了一些问题:我想检索集合的最后一项,并显示字段:其中包含 html 的文本: 这是我的javascript代码: Te
你好,我想使用 Service 实现 GestureDetector 但是我有这个错误The method onTouchEvent(MotionEvent) of type GestureServi
我正在尝试在 Controller bean 中 Autowiring 接口(interface) 在我放置的上下文配置文件中 和 我的 Controller 类是 @Controller pub
我试图在 mainwindow.cpp 中包含 QtSvg,但是当我编译时它说无法打开包含文件:QtSvg。我已经在我的 *.pro 文件中添加了这个(QT += svg)。我可以知道可能是什么问题吗
鉴于以下 PostgreSQL 代码,我认为这段代码不容易受到 SQL 注入(inject)攻击: _filter 'day' _start 1 _end 10 _sort 'article_name
我想执行以下操作。这在 MySQL 中是非法的。 PostGRESQL 中关联的 CTE(“with”子句)有效。这里的假设是 MySQL 中的子查询不是完全限定的 CTE。 请注意:这个查询显然非常
我是一名优秀的程序员,十分优秀!