【问题标题】:Clustering values in a dataframe in pythonpython中数据框中的值聚类
【发布时间】:2020-07-08 05:47:31
【问题描述】:

我有一个包含 76 列的数据框。第一列包含日期值,其他 75 列是来自 75 个不同钻孔的地下水位。我想根据趋势对钻孔进行聚类(遵循相同模式的钻孔被组合在一起)。我如何在 python 中做到这一点?

这是我的数据框示例

df = pd.DataFrame({
'Date': [1980, 1985, 1990, 1995, 2000],
'borehole1': [53, 43, 33, 22, 18],
'borehole2': [50, 40, 30, 50, 40],
'borehole3': [22, 32, 42, 32, 13],
'borehole4': [60, 65, 82, 72, 60],
'borehole5': [80, 70, 60, 80, 70],
'borehole6': [43, 33, 22, 18, 13]
}) 

df.plot()

因此,在本例中,我将有 3 个集群:

  • borehole1 & borehole 6 >> cluster 1
  • borehole2 & borehole 5 >> cluster 2
  • 钻孔 4 和钻孔 3 >> 集群 3

【问题讨论】:

    标签: python pandas dataframe cluster-analysis


    【解决方案1】:

    K-Means 算法非常适合这个!这是一个示例(下)。只需将 X 和 y 指向您的特定数据集并将“K”设置为 3(在本示例中已为您完成)。

    # K-MEANS CLUSTERING
    # Importing Modules
    from sklearn import datasets
    from sklearn.cluster import KMeans
    import matplotlib.pyplot as plt
    from sklearn.decomposition import PCA
    # Loading dataset
    iris_df = datasets.load_iris()
    
    # Declaring Model
    model = KMeans(n_clusters=3)
    
    # Fitting Model
    model.fit(iris_df.data)
    
    # Predicitng a single input
    predicted_label = model.predict([[7.2, 3.5, 0.8, 1.6]])
    
    # Prediction on the entire data
    all_predictions = model.predict(iris_df.data)
    
    # Printing Predictions
    print(predicted_label)
    print(all_predictions)
    
    
    # import some data to play with
    iris = datasets.load_iris()
    X = iris.data[:, :3]  # we only take the first two features.
    y = iris.target
    
    
    fig = plt.figure(figsize=(10,10))
    plt = fig.add_subplot(111, projection='3d')
    plt.scatter(X[:,0],X[:,1],X[:,2], 
                c=all_predictions, edgecolor='red', s=40, alpha = 0.5)
    plt.set_title("First three PCA directions")
    plt.set_xlabel("Educational_Degree")
    plt.set_ylabel("Gross_Monthly_Salary")
    plt.set_zlabel("Claim_Rate")
    plt.dist = 10
    plt
    

    查看此链接了解更多信息。

    https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 2021-06-16
      • 1970-01-01
      • 2016-06-18
      相关资源
      最近更新 更多