我有一些代码,我需要在列表上执行.data。但是,intable字符串不能使用.data,我需要将它们转换为整数。是的,我知道这是重复的,但其他问题说是手动完成,这个列表太大了。我该怎么做?感谢。
import numpy as np
import pandas
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn.cluster import KMeans
from sklearn import datasets
from sklearn import svm
from tabulate import tabulate
from csv import DictReader
digits = pandas.read_csv('C:/Users/saeed/Downloads/Sacramentorealestatetransactions.csv')
print(tabulate(digits, headers='keys', tablefmt='psql'))
with open('C:/Users/saeed/Downloads/Sacramentorealestatetransactions.csv') as i:
price = [row["price"] for row in DictReader(i)]
print(price)
clf = svm.SVC(gamma=0.001, C=100)
x,y = price.data[:1], price.target[:1]
错误:
AttributeError: 'list' object has no attribute 'data'
答案 0 :(得分:0)
你有:
price = [row["price"] for row in DictReader(i)]
并将其更改为:
price = [int(row["price"]) for row in DictReader(i)]
应该足够了。
答案 1 :(得分:0)
尝试用
替换你的with语句for i in digits:
price = [int(row["price"]) for row in DictReader(i)]
print(price)