【问题标题】:Python syntax error while trying to use sklearn.preprocessing尝试使用 sklearn.preprocessing 时出现 Python 语法错误
【发布时间】:2019-08-10 02:32:51
【问题描述】:

一直在看senddex的python实用机器学习教程https://pythonprogramming.net/training-testing-machine-learning-tutorial/。我试图运行相同的代码,使用 model_selection 而不是交叉验证,但是当我到达 preprocessing.scale() 时出现语法错误。 这是我的代码:

import quandl

import pandas as pd

import numpy as np

import math

from sklearn import preprocessing, svm, model_selection

from sklearn.linear_model import LinearRegression

df = quandl.get('WIKI/GOOGL')

df = df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]

df['HL_PT'] =  (df['Adj. High'] - df['Adj. Low']) / df['Adj. Close'] * 100
df['PT_Change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100

df = df[['Adj. Close', 'HL_PT', 'PT_Change', 'Adj. Volume']]

forecast_col = 'Adj. Close'
df.fillna(-99999, inplace=True)

forecast_out = int(math.ceil(0.01 * len(df)))
print(forecast_out)

df['label'] = df[forecast_col].shift(-forecast_out)
df.dropna(inplace=True)

X = np.array(df.drop(['Label'] ,1)) 

y = np.array(df.drop(['Label'])
X = preprocessing.scale(X)

y2 = np.array(df['label'])

print(forecast_out)

这是错误信息:

runfile('D:/Course Materials/Data Science/mlpractice.py', wdir='D:/Course Materials/Data Science') Traceback(最近一次通话) 最后):

文件 "G:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", 第 2963 行,在 run_code 中 exec(code_obj, self.user_global_ns, self.user_ns)

文件“”,第 1 行,在 runfile('D:/Course Materials/Data Science/mlpractice.py', wdir='D:/Course Materials/Data Science')

文件 "G:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", 第 786 行,在运行文件中 execfile(文件名,命名空间)

文件 "G:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", 第 110 行,在 execfile 中 exec(编译(f.read(),文件名,'exec'),命名空间)

文件“D:/Course Materials/Data Science/mlpractice.py”,第 36 行 X = 预处理.scale(X) ^ SyntaxError: 无效语法

如果不弄清楚为什么会发生这种情况,就无法取得进一步进展。 任何帮助都会非常感激!

【问题讨论】:

  • 缺少尾随右大括号:y = np.array(df.drop(['Label'])<--- here 应该是 y = np.array(df.drop(['Label'])) 投票以关闭为拼写错误。提示:当您遇到语法错误并且看起来令人费解时,请检查其前面的行
  • 欢迎来到 SO;如果答案解决了您的问题,请接受(见What should I do when someone answers my question?

标签: python machine-learning scikit-learn


【解决方案1】:

你需要在这一行加上一个右括号:

y = np.array(df.drop(['Label'])

错误消息中所述的前一行通常是语法错误所在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-21
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 2022-01-23
    • 2015-05-26
    • 1970-01-01
    相关资源
    最近更新 更多