|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
关于网站程序运行出现的错误,请求指导更改,保证其可以正常运行:
#!/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[50::-1]
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([float(single_square_feet)])
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: [Errno 2] File b'newdata.txt' does not exist: b'newdata.txt' |
|