我正在尝试创建一个神经网络。我削减了a
,因为我有1000多个列表。
我看到了很多教程,但是我需要一些帮助:
我可以使用列表列表代替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
答案 0 :(得分:1)
是,可以。在熊猫库中,这变得微不足道。首先,您需要import pandas
,然后使用以下代码可以将列表列表转换为熊猫数据框:
df = DataFrame(a, columns=headers)
然后您可以使用以下方法设置训练集:
X_train, y_train = df['value1'], df['value2']
您的value2
列应包含分类器要使用的标签。对于KNN分类器,标签不能为float类型,因此只需将其调整为整数即可解决此问题。
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]]))