Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

python - Getting error:-"TypeError: no numeric data to plot" even while following a tutorial exactly

I am trying to follow a youtube machine learning tutorial from sentdex.Link

When I try to plot the graph using

df['forecast'].plot()

I get the error -TypeError: no numeric data to plot

However,

df['Adj. Close'].plot()

renders perfectly.

My code is-

'''
    
    df=quandl.get("WIKI/GOOGL")
    
    df=df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]
    
    df['HL_PCT'] = (df['Adj. High'] - df['Adj. Close']) / df['Adj. Close'] *100
    
    df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] *100
    
    forecast_col='Adj. Close'
    df.fillna(-99999, inplace=True)
    forecast_out = int(math.ceil(0.01*len(df)))
    print(len(df) ,forecast_out)
    df['label'] = df[forecast_col].shift(-forecast_out)
    X=np.array(df.drop(['label'], axis=1 ))
    X=preprocessing.scale(X)
    X=X[:-forecast_out]
    X_lately=X[-forecast_out:]
    df.dropna(inplace=True)
    y=np.array(df[['label']])
    algo=LinearRegression(n_jobs=-1)
    
    X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.2)
    algo.fit(X_train,y_train)
    y_pred =algo.predict(X_lately)
    
    df['forecast']=np.nan
    last_date=df.iloc[-1].name 
    last_unix=last_date.timestamp()
    one_day = 86400
    next_unix = last_unix +one_day
    
    for i in y_pred:
        next_date =datetime.datetime.fromtimestamp(next_unix)
        next_unix += one_day
        df.loc[next_date]= [np.NaN for _ in range(len(df.columns)-1)] + [i]  
    df['Adj. Close'].plot()
    df['forecast'].plot()   
    plt.show()   
    
    '''
question from:https://stackoverflow.com/questions/65844323/getting-error-typeerror-no-numeric-data-to-plot-even-while-following-a-tutor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sorry,Found my mistake-

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

instead of

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

Found it 5 minutes after posting this question,while I was banging my head on wall for literally hours because of this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...