代码参考https://blog.csdn.net/DL11007/article/details/129204192?ops_request_misc=&request_id=&biz_id=102&utm_term=logistic%E6%A8%A1%E5%9E%8Bpython&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1-129204192.142^v88^control_2,239^v2^insert_chatgpt&spm=1018.2226.3001.4187
df.loc[]和df.iloc[]:https://blog.csdn.net/weixin_48701352/article/details/120247544?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168864051316800182741570%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168864051316800182741570&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-2-120247544-null-null.142^v88^control_2,239^v2^insert_chatgpt&utm_term=df.iloc&spm=1018.2226.3001.4187
后续报错:
ConvergenceWarning: lbfgs failed to converge (status=1): STOP: TOTAL NO. of ITERATIONS REACHED LIMIT
解决方法:增加迭代次数
参考:https://blog.csdn.net/qq_43391414/article/details/113144702
代码:
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score data=pd.read_pickle('ICC_rms.pkl') df=pd.DataFrame(data) X = df.iloc[:, 0:510].values #所有样本的x值,0-510列 y = df.iloc[:, 511].values #所有样本的标签,511列 #划分数据集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=0) #对数据进行特征提取,进行数据标准化 sc_X = StandardScaler() X_train = sc_X.fit_transform(X_train) X_test = sc_X.transform(X_test) #Logistic拟合模型,max_iter=1000即增加迭代次数 classifier = LogisticRegression(max_iter=1000) classifier.fit(X_train, y_train) #打印参数结果 print("Logistic参数结果:",classifier.intercept_,classifier.coef_) y_pred = classifier.predict(X_test) # 计算R方 cm = accuracy_score(y_test, y_pred) print("测试集的R方:", cm)
标签:python,模型,blog,df,train,Logistic,test,import,classifier From: https://www.cnblogs.com/jiezstudy/p/17533299.html