【问题标题】:How to normalize(min/max) specific column in python? (Dataframe)如何规范化(最小/最大)python中的特定列? (数据框)
【发布时间】:2020-10-27 02:49:54
【问题描述】:

我一直致力于根据 Min-Max Normalization 对数据进行规范化。 我的数据集是存储在 df_mols 列表中的数据帧,如下所示。

df_mols[0]:   
         frequency  Molecule0
 0        -326.0   2.604015
 1        -323.0   2.624186
 2        -321.0   2.644598
 3        -318.0   2.665254
 4        -316.0   2.686159
 ...         ...        ...
 1996     4589.0   4.565467
 1997     4591.0   4.512142
 1998     4594.0   4.459744
 1999     4596.0   4.408251
 2000     4598.0   4.357645
 
df_mols[1]:      
          frequency  Molecule1
 0        -357.0   0.368472
 1        -354.0   0.371063
 2        -352.0   0.373683
 3        -350.0   0.376332
 4        -347.0   0.379010
 ...         ...        ...
 1996     4293.0   0.538391
 1997     4295.0   0.532088
 1998     4297.0   0.525894
 1999     4300.0   0.519807
 2000        NaN        NaN

我只想规范化所有 Molecule 列。 到目前为止,我所做的是,

from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler()

for i in df_mols:
  i['frequency']=i['frequency'].apply(np.rint) # This was to make frequency values into int
  i[:,1]=scaler.fit_transform(i[:,1])

并出现如下错误

/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    554                     "Reshape your data either using array.reshape(-1, 1) if "
    555                     "your data has a single feature or array.reshape(1, -1) "
--> 556                     "if it contains a single sample.".format(array))
    557 
    558         # in the future np.flexible dtypes will be handled like object dtypes

ValueError: Expected 2D array, got 1D array instead:
array=[2.60401472 2.62418641 2.64459837 ... 4.45974369 4.4082515  4.35764454].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

我试图重塑,但做不到。我应该制作新系列然后更新价值吗?或者我应该解决这个问题? 谢谢你:)

【问题讨论】:

    标签: python pandas numpy dataframe


    【解决方案1】:

    在重塑之前,您可以使用对象方法.to_numpy() 将您的 pd.Series 转换为 np.ndarray。

    【讨论】:

      【解决方案2】:

      我不确定它是否是您要找的东西,但我认为这样的东西可以解决问题。

      import pandas as pd
      from sklearn.preprocessing import normalize
      
      data = [[-326.0, 2.604015], [-323.0, 2.624186], [-321.0, 2.644598], [-318.0, 2.665254]]
      
      df = pd.DataFrame(data, columns = ['frequency', 'Molecule0'])
      
      print("Shape of column: ", df['Molecule0'].shape)
      
      normalized_data = normalize(df['Molecule0'].to_numpy().reshape(1, -1), norm='max')[0]
      
      print("Normalized data: ", normalized_data)
      
      df['Molecule0'] = normalized_data
      print(df)
      

      当我运行它时,我得到了以下输出......

      柱形:(4,)
      标准化数据:[0.9770232 0.98459134 0.99224989 1.]

      频率分子0
      0 -326.0 0.977023
      1 -323.0 0.984591
      2 -321.0 0.992250
      3 -318.0 1.000000

      【讨论】:

      • 谢谢!但是,如果有将近 70 个分子,我想用 for 循环进行更改怎么办?/ 通用版本?
      • @sopL 所有分子都在同一个数据框中吗?
      • 不,每个分子都在不同的数据帧中,但 iloc[1](例如:[频率,分子 0],[频率,分子 1],)和 df_mols(列表)中的数据帧相同。跨度>
      • 抱歉我的解释不好。 df_mol[0]= 这是一个包含“频率”和“分子 0”列的系列,而 df_mol[1]=它是“频率”和“分子 1”列
      猜你喜欢
      • 2015-03-03
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-08
      相关资源
      最近更新 更多