我有一些代码,我需要在列表中执行.data。但是,intable字符串不能接受.data,我需要将它们转换为整数。是的,我知道这是重复的,但其他问题说手动做,这个列表太大了。我将如何做到这一点?谢谢。将int字符串的列表/元组转换为int
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'
取代你,我不知道你想做什么。 'price'只是从CSV文件中读取的普通列表,它不是一个numpy对象。 – Barmar
我不确定你为什么用两种不同的方法阅读CSV:熊猫和Dictreader。你已经有了数字的数据。 'price = list(digits.price)'可以在没有DictReader代码的情况下工作,或者如果您想查看数据框的那一列,则只需要'print(digits.price)'。 –