import operator from math import sqrt import numpy as np import pandas as pd from random import sample df = pd.DataFrame(pd.read_csv("4.0.csv", names=['密度', '含糖率'], header=None, encoding="ansi")) # 读取csv文件 data_array = np.array(df) # 先将dataframe转换为数组 data_list = data_array.tolist() # 其次转换为列表 # print(data_list) def number(D): number = 0 for _ in D: number += 1 return number def dis_ed(list1, list2): dis__ed = sqrt((list1[0] - list2[1]) ** 2 + (list1[0] - list2[1]) ** 2) return dis__ed def dis_md(list1, list2): dis__md = abs(list1[0] - list2[1]) + abs(list1[0] + list2[1]) return dis__md def k_means(D, k): D_initial = sample(D, k) while True: cluster = [[D_initial[0]], [D_initial[1]], D_initial[2]] for j in range(0, number(D)): distance = [] for i in range(0, k): dis__ed = dis_ed(D[j], D_initial[i]) distance.append(dis__ed) min_dis = min(distance) min_index = distance.index(min_dis) cluster[min_index].append(D[j]) flag = True # 用于判断是否结束循环 D_new = [] for i in range(0, k): cluster_array = np.array(cluster[i]) cluster_avg=np.mean(cluster_array, axis=0) D_new.append(cluster_avg[0]) if (D_new[i] != D_initial[i]).all(): D_initial[i] = D_new[i] flag=False return cluster print(k_means(data_list, 3))
时间: 2023-05-26 19:05:24 浏览: 90
这是一段 Python 代码,主要通过使用导入的操作和模块实现各种功能。其中从 math 模块中导入了平方根函数 sqrt,从 numpy 模块中导入了命名为 np 的模块,从 pandas 模块中导入了命名为 pd 的模块。此外,从 random 模块中还导入了 sample 函数。最后,使用 pd.read_csv 函数读取名为 4.0.csv 的文件,并将其转换为 Pandas DataFrame 格式。该 DataFrame 的列名分别为 '密度' 和 '含糖率',编码方式为 ansi。
相关问题
import operator import numpy as np
import operator和import numpy as np是Python中的两个常用的导入模块的语句。其中,import operator用于导入Python内置的operator模块,该模块提供了一系列的操作符函数,例如itemgetter函数,可以用于获取对象的位置。而import numpy as np则是导入了一个名为numpy的第三方库,并将其命名为np,numpy是Python中用于科学计算的一个常用库,提供了许多高效的数学函数和数据结构,例如多维数组和矩阵运算等。
help(pd.read_csv()[].apply().tolist)
`pd.read_csv()` is a function in the pandas library that reads a CSV file and returns a pandas DataFrame. The `[]` operator is used to select a specific column or columns from the DataFrame. The `.apply()` method is used to apply a function to each element of the selected column(s). Finally, the `.tolist()` method is used to convert the resulting pandas Series or DataFrame to a Python list.
So the code `pd.read_csv()[].apply().tolist` doesn't make sense as there are no arguments passed to the `apply()` method. You need to provide a function to the `apply()` method to transform the data in the selected column(s) before converting it to a list with `tolist()`.
阅读全文