python - 在不失去值(value)重要性的情况下进行扩展 Python Sklearn

标签 python python-3.x pandas scikit-learn

csv 的示例为:

0.03528821669081923,0.4209514856338501
0.4755249949860231,0.4248427748680115
0.09710556840327728,0.4209169149398804
0.07149631133318766,0.4201127290725708
-0.2400341908399068,0.417565792798996
-0.17768551828033466,0.4184338748455048
-0.30025757809215714,0.416279673576355
-0.09094791496191304,0.41964152455329895
0.07154744586719554,0.4196658134460449
0.2381333126503035,0.42377570271492
0.2593105332145284,0.4222800433635712
-0.6691065606953182,0.4089060425758362
-0.6456401882265393,0.4092327654361725
-0.2320063391631248,0.4154394268989563
0.03676064944004283,0.4164957106113434
-0.049027521405378964,0.4175394177436829
-0.5611679536206179,0.4090659916400909
-1.151078217514793,0.3977192640304565
-1.1251183926252533,0.3976330757141113
-1.3598634565590335,0.3943647146224976
-1.452113101667516,0.3926326930522919
-1.724856436518542,0.3888352811336517
-1.3449567318568625,0.3950198888778687
-0.9327234868901516,0.39986416697502136
-0.8698905846258818,0.40163424611091614
-1.0829297248122909,0.4009062349796295
-0.7123502605778409,0.406065821647644
-0.7078240398708294,0.4043383300304413
-1.0054995188827682,0.4010890424251557
-0.40067943737923295,0.41085284948349
-0.3684788480142471,0.4130916893482208
-0.31293912846313354,0.4178936183452606

我已将其加载到 pandas 中,并尝试使用 sklearn.preprocessing.scale() 对其进行缩放,但它仅对指定的列进行缩放。

df['col1'] = sklearn.preprocessing.scale(df['col1'].values)
df['col2'] = sklearn.preprocessing.scale(df['col2'].values)

我想相对于另一列缩放列,以便我可以在同一个图上绘制。仅当这些值在同一范围内并且不失去其重要性时,这才有可能。
请建议我能做什么。

最佳答案

你可以做的一件事就是使用 sklearn.preprocessing.StandardScaler ,可以使用数组进行拟合,然后使用计算的平均值和标准差转换其他数组。所以你可以这样做:

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()

根据数据帧列 reshape numpy 数组:

col1 = x['col1'].values.reshape(-1,1)
col2 = x['col2'].values.reshape(-1,1)

使用 col1 拟合实例化对象:

fitted = scaler.fit(col1)

使用 col1 中的 meanstd 对所有特征进行标准化:

col1 = fitted.transform(col1)
col2 = fitted.transform(col2)

关于python - 在不失去值(value)重要性的情况下进行扩展 Python Sklearn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54570264/

相关文章:

python - Linux 和 Jedit 中的制表符缩进

python - 运行多个 LinearRegressions 测试时准确度没有增加

python - Pandas Dataframe内存read_csv

python - 如何用另一个数据框替换部分数据框

Python/BeautifulSoup : How to look directly beneath a code comment?

java - 从 WiFI 强度信号计算出的 3 个距离的三边测量

python - 比较两个数据框之间的元素并在相等的情况下添加列

python - Spyder : How to make pandas. 图(subplots =True)在关闭绘图窗口后再次显示?

python - 获取 Gtk.Grid 中的列数?

python-3.x - 反转字符串,但不反转整数