请检查这段代码有没有错误 import pandas as pd from pyecharts.charts import * from sklearn.linear_model import LogisticRegression data = pd.read_csv('双色球.csv',encoding='utf-8', engine='python') data.head() for i in range(0,6): data[f'r{i+1}'] = data['红球'].apply(lambda x:x.split(',')[i]) data[f'r{i+1}'] = data[f'r{i+1}'].astype('int64') def get_lotto_data(data, lotto, lotto_id): #取数据,指定训练集和测试集 data['lotto_id'] = lotto_id X = [] Y = [] # 标签and值 for s, p in zip(data['lotto_id'], data[lotto]): X.append([float(s)]) Y.append(float(p)) return X, Y def linear_model_test(X, Y, predict_value): #建立线性回归模型 regr = LogisticRegression() regr.fit(X, Y) 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(file, lotto, lotto_id): #使用线性回归推测中奖号码 X, Y = get_lotto_data(file, lotto, lotto_id) predict_value = [[33]] result = linear_model_test(X, Y, predict_value) if lotto_id < 7: print(f'中奖第{lotto_id}个红球为:', result['predicted_value'].astype('int64'), '号球') else: print('中奖蓝球为:', result['predicted_value'].astype('int64'), '号球') get_predicted_num(data, 'r1', 1) # 预测红1 get_predicted_num(data, 'r2', 2) # 预测红2 get_predicted_num(data, 'r3', 3) # 预测红3 get_predicted_num(data, 'r4', 4) # 预测红4 get_predicted_num(data, 'r5', 5) # 预测红5 get_predicted_num(data, 'r6', 6) # 预测红6 get_predicted_num(data, '蓝球', 7) # 预测蓝7
时间: 2023-12-14 18:37:51 浏览: 141
import pandas as pd.docx
代码中存在一些错误:
1. 在导入 pyecharts 库时,应该使用 from pyecharts import charts 而非 from pyecharts.charts import *。
2. 代码中的第二行应该加上一个换行符,以使代码更易读。
3. 在调用 get_predicted_num() 函数时,应该删除每行代码前面的空格,以使代码能够正确执行。
修改后的代码如下所示:
```python
import pandas as pd
from pyecharts import charts
from sklearn.linear_model import LogisticRegression
data = pd.read_csv('双色球.csv', encoding='utf-8', engine='python')
data.head()
for i in range(0,6):
data[f'r{i+1}'] = data['红球'].apply(lambda x:x.split(',')[i])
data[f'r{i+1}'] = data[f'r{i+1}'].astype('int64')
def get_lotto_data(data, lotto, lotto_id):
# 取数据,指定训练集和测试集
data['lotto_id'] = lotto_id
X = []
Y = []
# 标签and值
for s, p in zip(data['lotto_id'], data[lotto]):
X.append([float(s)])
Y.append(float(p))
return X, Y
def linear_model_test(X, Y, predict_value):
# 建立线性回归模型
regr = LogisticRegression()
regr.fit(X, Y)
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(file, lotto, lotto_id):
# 使用线性回归推测中奖号码
X, Y = get_lotto_data(file, lotto, lotto_id)
predict_value = [[33]]
result = linear_model_test(X, Y, predict_value)
if lotto_id < 7:
print(f'中奖第{lotto_id}个红球为:', result['predicted_value'].astype('int64'), '号球')
else:
print('中奖蓝球为:', result['predicted_value'].astype('int64'), '号球')
get_predicted_num(data, 'r1', 1) # 预测红1
get_predicted_num(data, 'r2', 2) # 预测红2
get_predicted_num(data, 'r3', 3) # 预测红3
get_predicted_num(data, 'r4', 4) # 预测红4
get_predicted_num(data, 'r5', 5) # 预测红5
get_predicted_num(data, 'r6', 6) # 预测红6
get_predicted_num(data, '蓝球', 7) # 预测蓝7
```
阅读全文