我正在为一个模型准备一个数据集,但不知怎么的,代码不能很好地运行。
主要错误是:
文件"/Users/liangjulia/Desktop/UW DS证书学习材料/untled6.py“,第61 'income2‘= pd.to_numeric(Adult.income,errors=’胁迫‘)^ SyntaxError:无法分配给文字
# import statement
import numpy as np
import pandas as pd
import csv
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
# loading dataset, it is a combination of categorical and numerical data
hp = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data",header=0,sep=',')
hp.columns = ['age','workclass','income','education','education-num','marital-status','occupation','relationship','race','sex','capital-gain','capital-loss','hours-per-week','native-country','salary-range']
# dataset basics
hp.head()
hp.shape
hp.dtypes
# account for all value '?'
hp.replace('?','na')
hp.isnull().sum()
#Remove onsolete data point in income
hp('income').dropna()
# replace all aberrant values
hp.replace('nan', 0)
hp.replace('NULL', 0)
# change data type of certain data point to numerical
number = LabelEncoder()
hp['income'] = number.fit_transform(hp['income'.astype('str')])
hp['capital-gain'] = number.fit_transform(hp['capital-gain'.astype('str')])
hp['capital-loss'] = number.fit_transform(hp['capital-loss'.astype('str')])
# Choose the datapoint 'income' to perform the data cleaning and remove outliers
LimitHi=np.mean('income') + 2*np.std('income')
LimitLo=np.mean('income') + 2*np.std('income')
BadIncome = ('income' > LimitHi) & ('income' < LimitLo)
# Replace outliars
RightIncome = ~BadIncome
x[BadIncome] = np.mean(x[RightIncome])
# normalize the Income Column using numpy
#'income2' = pd.to_numeric(Adult.income, errors='coerce')
minmaxscaled =('income' - min('income'))/(max('income') - min('income'))
# bin age data into several ranges
hp['bin'] = pd.cut(hp['age'], [15,30,45,60,75,90])
# construct new categorical data point with existing data point
hp['EvalonInvestment'] = 'zzz'
hp.loc[(hp['capital-gain'] >= 50000), 'loc2'] = 'investmentking'
hp.loc[(hp['capital-gain'] > 10000) & (hp['capital-gain'] < 50000), 'loc2'] = 'good-investment'
hp.loc[(hp['capital-gain'] > 0) & (hp['capital-gain'] <= 10000), 'loc2'] = 'ok-investment'
print(hp)发布于 2018-08-05 21:11:08
它应该是hp‘income2’,因为不能将可变对象分配给不可变对象,例如字符串
发布于 2018-08-05 22:50:01
第61行应改为
income2 = pd.to_numeric(hp['income'], errors = 'coerce')让我们把这个拆开。
您希望将一个新变量income2分配给pd类to_numeric中的方法的输出。
to_number采用两个参数: arg : list、tuple、1-d数组或Series,这是关于如何处理错误的选择,以及可选的下行运算符。你可以通过
help(pd.to_numeric)第61行最初提供了arg Adult.Income,它可以是一个列表、元组、一维数组或系列,但是它还没有定义,所以它不能是一个有效的arg。
试一试
类型(hp“收入”)
然后你就会得到
pandas.core.series.Series这是一个有效的论点。
这行不是代码中唯一的问题。我建议您使用iPython,以便可以看到所有其他错误。或者,只需使用命令行并一次复制几行,您就会发现要解决的新错误消息。
代码中的错误就像蟑螂--它们很少独自旅行。
祝好运!
https://datascience.stackexchange.com/questions/36496
复制相似问题