How to read csv files for dbscan analysis by Python
This article introduces the relevant knowledge of "how to read csv files for dbscan analysis by Python". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Read csv data for dbscan analysis
Read the corresponding columns in the csv file, then convert them to the format required by this algorithm, and then perform dbscan operations. at present, there is a lot of open code, which is modified according to the open code.
The specific code is as follows:
From sklearn import datasetsimport numpy as npimport randomimport matplotlib.pyplot as pltimport timeimport copyimport pandas as pd# from sklearn.datasets import load_iris def find_neighbor (j, x) Eps): n = list () for i in range (x.shape [0]): temp = np.sqrt (np.sum (np.square (x [j]-x [I]) # calculate the Euclidean distance if temp = min_Pts: omega_list.append (I) # add samples to the core object set omega_list = set (omega_list) # transfer While len (omega_list) > 0: gama_old = copy.deepcopy (gama) j = random.choice (list (omega_list)) # randomly select a core object k = k + 1 Q = list () Q.append (j) gama.remove (j) while len (Q) > 0: Q = Q [0] Q.remove (Q) if len (neighbor_ list [Q]) > = min_Pts: delta = neighbor_ list [Q] & gama deltalist = list (delta) for i in range (len (delta)): Q.append (deltalist [I]) gama = Gama-delta Ck = gama_old-gama Cklist = list (Ck) for i in range (len (Ck)): cluster [Cklist [I]] = k omega_list = omega_list-Ck return cluster # X = load_iris (). Datadata = pd.read_csv ("testdata.csv") x Y=data ['Time (sec)'], data ['Height (m HAE)'] print (type (x) n=len (x) x=np.array (x) x=x.reshape (nMagol 1) y=np.array (y) y=y.reshape (nL1) X = np.hstack ((x, y)) cluster_std= [[.1]], random_state=9) eps = 0.08min_Pts = 5begin = time.time () C = DBSCAN (X, eps, min_Pts) end = time.time () plt.figure () plt.scatter (X [: 0], X [:, 1], C) plt.show () 2. The output shows that
Modify the parameter display:
Eps = 0.8min_Pts = 5
3. Calculation efficiency
When using a small amount of data to calculate, the efficiency problem is not obvious, with the increase of the amount of data, the computational efficiency problem becomes particularly obvious, it is difficult to meet the calculation needs of a large number of data. Later, we will find a way to optimize the calculation method or collect C++ code for optimization.
This is the end of the content of "how to read csv files for dbscan analysis by Python". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!