Reputation: 115
I'm trying to create a neural network. I cut a
because I have more than 1000 lists.
I saw a lot of tutorials but I need some help:
Can I use a list of list as my database instead of dic?
# a[0] is the price
# a[1] is the paid value
# a[2] is my result
from sklearn.neighbors import KNeighborsClassifier
from pandas import DataFrame
a = [[0.063807299, 0.71, 0.00071],
[0.363262854, 0.7, 0.0007],
[0.836344317, 0.76, 0.00076]]
df = DataFrame(a)
df.columns = ['value1', 'value2', 'result']
X_train, y_train = df['value1'], df['value2']
knn = KNeighborsClassifier(n_neighbors=7)
knn.fit(X_train, y_train)
knn.score(X_train, y_train)
knn.predict([[1.2, 3.4]])
>>> 0.025 # This would be my results for example
Upvotes: 0
Views: 89
Reputation: 1025
Yes, yes you can. This becomes trivial with the pandas library. First you need to import pandas
, then with the following code you can convert your list of lists into a pandas dataframe:
df = DataFrame(a, columns=headers)
then you can set up the training set with:
X_train, y_train = df['value1'], df['value2']
Your value2
column is expected to contain labels for the classifier to work with. Labels cannot be of type float for KNN classifiers, so simply adjusting them to integers will solve the problem.
a = [[0.063807299, 71, 0.00071],
[0.363262854, 7, 0.0007],
[0.836344317, 76, 0.00076]]
lab_enc = preprocessing.LabelEncoder()
df = DataFrame(a)
df.columns = ['value1', 'value2', 'result']
X_train, y_train = df['value1'].values.reshape(-1,1), df['value2'].values.reshape(-1,1)
knn = KNeighborsClassifier(n_neighbors=2)
knn.fit(X_train, y_train.ravel())
knn.score(X_train, y_train)
print(knn.predict([[0.7]]))
Upvotes: 1