import numpy as np
import pandas as pd
import os
os.chdir("C:\\Users\\Administrator\\Desktop")
data = pd.read_excel("发电场数据.xlsx")
datah = data.head(6)
print(datah)
AT V AP RH PE
0 14.96 41.76 1024.07 73.17 463.26
1 25.18 62.96 1020.04 59.08 444.37
2 5.11 39.40 1012.16 92.14 488.56
3 20.86 57.32 1010.24 76.64 446.48
4 10.82 37.50 1009.23 96.62 473.90
5 26.27 59.44 1012.23 58.77 443.67
y = data.iloc[:,4].values
x = data.iloc[:,0:4].values
from sklearn.linear_model import LinearRegression as LR
lr = LR()
lr.fit(x,y)
R2 = lr.score(x,y)
print(R2)
0.9286960898122536
c_x = lr.coef_
print(c_x)
[-1.97751311 -0.23391642 0.06208294 -0.1580541 ]
c_b = lr.intercept_
print(c_b)
454.6092743153102
x1 = np.array([28.4,50.6,1011.9,80.54])
x1 = x1.reshape(1,4)
P1 = lr.predict(x1)
print(P1)
[436.70378447]
r1 = x1*c_x
P2 = r1.sum()+c_b
print(P2)
436.70378446715097
data1 = pd.read_excel("credit.xlsx")
dataq = data1.head(6)
print(dataq)
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 d
0 1 22.08 11.460 2 4 4 1.585 0 0 0 1 2 100 1213 0
1 0 22.67 7.000 2 8 4 0.165 0 0 0 0 2 160 1 0
2 0 29.58 1.750 1 4 4 1.250 0 0 0 1 2 280 1 0
3 0 21.67 11.500 1 5 3 0.000 1 1 11 1 2 0 1 1
4 1 20.17 8.170 2 6 4 1.960 1 1 14 0 2 60 159 1
5 0 15.83 0.585 2 8 8 1.500 1 1 2 0 2 100 1 1
x2 = data1.iloc[:600,:14].values
y2 = data1.iloc[:600,14].values
x3 = data1.iloc[600:,:14].values
y3 = data1.iloc[600:,14].values
from sklearn.linear_model import LogisticRegression as LJ
lj = LJ()
lj.fit(x2,y2)
r = lj.score(x2,y2)
print(r)
0.8183333333333334
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py:763: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
n_iter_i = _check_optimize_result(
R = lj.predict(x3)
print(R)
[0 1 1 1 1 0 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 1 1 0 1 1 1 0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 1 0 1 1 1 1 1 0 0 1 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 1 0 1 1 0 1 1 0 0 1 0]
z = R - y3
RS = len(z[z == 0])/len(z)
print("预测的准确率为:",RS)
预测的准确率为: 0.8
from sklearn.neural_network import MLPClassifier
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5,2), random_state=1)
clf.fit(x2, y2)
rv=clf.score(x2,y2)
print(rv)
R=clf.predict(x3)
Z=R-y3
Rs=len(Z[Z==0])/len(Z)
print('预测结果为:',R)
print('预测准确率为:',Rs)
0.795
预测结果为: [0 1 1 1 1 0 0 1 0 1 1 0 0 0 1 1 0 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 1 1 0 1 1 0 0 1 0]
预测准确率为: 0.8
x = data.iloc[:,0:4]
y = data.iloc[:,4]
from sklearn.neural_network import MLPRegressor
clf1 = MLPRegressor(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=8, random_state=1)
clf1.fit(x, y)
rv1=clf1.score(x,y)
x1=np.array([28.4,50.6,1011.9,80.54])
x1=x1.reshape(1,4)
R=clf1.predict(x1)
print('样本预测值为:',R)
样本预测值为: [439.2768771]
|