zhongyong6899 发表于 2019-12-10 19:55:06

关于网站程序运行出现的错误,请求指导更改

关于网站程序运行出现的错误,请求指导更改,保证其可以正常运行:
#!/usr/bin/python
# -*- coding:UTF-8 -*-

# 导入需要的包
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import operator
from sklearn import datasets, linear_model
from sklearn.linear_model import LogisticRegression

# 读取文件
df = pd.read_table('newdata.txt', header=None, sep=',')

# 读取日期
tdate = sorted(df.loc[:, 0])


# 将以列项为数据,将球号码取出,写入到csv文件中,并取50行数据
# Function to red number to csv file
def RedToCsv(h_num, num, csv_name):
    h_num = df.loc[:, num:num].values
    h_num = h_num
    renum2 = pd.DataFrame(h_num)
    renum2.to_csv(csv_name, header=None)
    fp = file(csv_name)
    s = fp.read()
    fp.close()
    a = s.split('\n')
    a.insert(0, 'numid,number')
    s = '\n'.join(a)
    fp = file(csv_name, 'w')
    fp.write(s)
    fp.close()


# 调用取号码函数
# create file
RedToCsv('red1', 1, 'rednum1data.csv')
RedToCsv('red2', 2, 'rednum2data.csv')
RedToCsv('red3', 3, 'rednum3data.csv')
RedToCsv('red4', 4, 'rednum4data.csv')
RedToCsv('red5', 5, 'rednum5data.csv')
RedToCsv('red6', 6, 'rednum6data.csv')
RedToCsv('blue1', 7, 'bluenumdata.csv')


# 获取数据,X_parameter为numid数据,Y_parameter为number数据
# Function to get data
def get_data(file_name):
    data = pd.read_csv(file_name)
    X_parameter = []
    Y_parameter = []
    for single_square_feet, single_price_value in zip(data['numid'], data['number']):
      X_parameter.append()
      Y_parameter.append(float(single_price_value))
    return X_parameter, Y_parameter


# 训练线性模型
# Function for Fitting our data to Linear model
def linear_model_main(X_parameters, Y_parameters, predict_value):
    # Create linear regression object
    regr = linear_model.LinearRegression()
    # regr = LogisticRegression()
    regr.fit(X_parameters, Y_parameters)
    predict_outcome = regr.predict(predict_value)
    predictions = {}
    predictions['intercept'] = regr.intercept_
    predictions['coefficient'] = regr.coef_
    predictions['predicted_value'] = predict_outcome
    return predictions


# 获取预测结果函数
def get_predicted_num(inputfile, num):
    X, Y = get_data(inputfile)
    predictvalue = 51
    result = linear_model_main(X, Y, predictvalue)
    print
    "num " + str(num) + " Intercept value ", result['intercept']
    print
    "num " + str(num) + " coefficient", result['coefficient']
    print
    "num " + str(num) + " Predicted value: ", result['predicted_value']


# 调用函数分别预测红球、蓝球
get_predicted_num('rednum1data.csv', 1)
get_predicted_num('rednum2data.csv', 2)
get_predicted_num('rednum3data.csv', 3)
get_predicted_num('rednum4data.csv', 4)
get_predicted_num('rednum5data.csv', 5)
get_predicted_num('rednum6data.csv', 6)

get_predicted_num('bluenumdata.csv', 1)


# 获取X,Y数据预测结果
# X,Y = get_data('rednum1data.csv')
# predictvalue = 21
# result = linear_model_main(X,Y,predictvalue)
# print "red num 1 Intercept value " , result['intercept']
# print "red num 1 coefficient" , result['coefficient']
# print "red num 1 Predicted value: ",result['predicted_value']


# Function to show the resutls of linear fit model
def show_linear_line(X_parameters, Y_parameters):
    # Create linear regression object
    regr = linear_model.LinearRegression()
    # regr = LogisticRegression()
    regr.fit(X_parameters, Y_parameters)
    plt.figure(figsize=(12, 6), dpi=80)
    plt.legend(loc='best')
    plt.scatter(X_parameters, Y_parameters, color='blue')
    plt.plot(X_parameters, regr.predict(X_parameters), color='red', linewidth=4)
    plt.xticks(())
    plt.yticks(())
    plt.show()

出现的错误提示如下:
Traceback (most recent call last):
File "E:/Pycharm/每日一题测试.py", line 13, in <module>
    df = pd.read_table('newdata.txt', header=None, sep=',')
File "D:\python\lib\site-packages\pandas\io\parsers.py", line 685, in parser_f
    return _read(filepath_or_buffer, kwds)
File "D:\python\lib\site-packages\pandas\io\parsers.py", line 457, in _read
    parser = TextFileReader(fp_or_buf, **kwds)
File "D:\python\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
    self._make_engine(self.engine)
File "D:\python\lib\site-packages\pandas\io\parsers.py", line 1135, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
File "D:\python\lib\site-packages\pandas\io\parsers.py", line 1917, in __init__
    self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 382, in pandas._libs.parsers.TextReader.__cinit__
File "pandas/_libs/parsers.pyx", line 689, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: File b'newdata.txt' does not exist: b'newdata.txt'

zhongyong6899 发表于 2019-12-10 19:57:31

我只是个新手,以上程序来自网站,说是可以运行,我运行后出现了错误提示信息,请求高手更改,,谢谢!

冬雪雪冬 发表于 2019-12-10 20:02:02

此行读取文件
df = pd.read_table('newdata.txt', header=None, sep=',')
但程序中并未创建文件。

zhongyong6899 发表于 2019-12-10 20:04:39

如何创建?请冬雪雪冬给与指导,万分感谢

zhongyong6899 发表于 2019-12-13 20:19:24

如何创建?请冬雪雪冬给与指导,万分感谢

zhongyong6899 发表于 2019-12-14 15:23:57

如何创建?请冬雪雪冬给与指导,万分感谢

zhongyong6899 发表于 2019-12-17 14:10:09

如何创建?请冬雪雪冬给与指导,万分感谢

zhongyong6899 发表于 2019-12-18 13:39:41

如何创建?请冬雪雪冬给与指导,万分感谢

gywjj 发表于 2019-12-18 14:34:07

本帖最后由 gywjj 于 2019-12-18 14:36 编辑

这个文件是你电脑上的,最好你跟一个绝对路径,别人的位置和你的不一定一样
你在什么网上下的,上面应该有这个文件,如果你找不到,你可以自己创建一个
格式应该是这个样子的

2005-08-21, 05,10,23,27,28,30,15
2005-08-18, 04,05,17,18,26,33,04
2005-08-16, 09,12,18,21,28,29,05
页: [1]
查看完整版本: 关于网站程序运行出现的错误,请求指导更改