zoukankan      html  css  js  c++  java
  • python统计分析-因子分析

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    # <editable>
    
    def execute():
        # <editable>
        '''
        载入模块
        '''
        from sklearn.decomposition import FactorAnalysis
        import numpy as np
        import pandas as pd
        from sqlalchemy import create_engine
        '''
        连接数据库
        '''
        engine = create_engine('mysql+pymysql://root:123123qwe@127.0.0.1:3306/analysis')
        '''
        选择目标数据
        '''
        params = {
            "columns": "SUNACTIVITY",
            "n_components": 1,
            "max_iter": 100,    # default=1000
        }
        inputs = {"table": '纯随机性检验'}
        data_sql = 'select ' + params['columns'] + ' from ' + inputs['table']
        data_in = pd.read_sql_query(data_sql, engine)
        print(data_in)
    
        '''
        因子分析
        '''
        data_in = data_in.select_dtypes(include=['number'])  # 筛选数值型数据
        fit = FactorAnalysis(n_components=int(params['n_components']), max_iter=int(params['max_iter'])).fit_transform(
            data_in)
        data_out = pd.DataFrame(fit)
        data_out = np.around(data_out, decimals=4)
        '''
        将结果写出
        '''
        print(data_out)
        '''
        数据示例
                SUNACTIVITY
        0           5.0
        1          11.0
        2          16.0
        3          23.0
        4          36.0
        5          40.4
        6          29.8
        7          15.2
        8           7.5
        9           2.9
        10         83.4
        11         47.7
        12         47.8
        13         30.7
        14         12.2
        15         40.4
        16         29.8
        17         15.2
        18          7.5
        19          2.9
        20         12.6
                 0
        0  -1.0104
        1  -0.7014
        2  -0.4439
        3  -0.0834
        4   0.5861
        5   0.8127
        6   0.2668
        7  -0.4851
        8  -0.8816
        9  -1.1185
        10  3.0273
        11  1.1887
        12  1.1938
        13  0.3132
        14 -0.6396
        15  0.8127
        16  0.2668
        17 -0.4851
        18 -0.8816
        19 -1.1185
        20 -0.6190
        '''
    
    # </editable>
    
    
    if __name__ == '__main__':
        execute()
    作者:沐禹辰
    出处:http://www.cnblogs.com/renfanzi/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    Mybatis 使用Mybatis时实体类属性名和表中的字段名不一致
    getResourceAsStream 地址
    Memory Allocation with COBOL
    静态call 动态call LINK
    反编译
    eclipse 设置英文
    WAR/EAR 概念
    application.xml
    对ContentProvider中getType方法的一点理解
    总结使人进步,可视化界面GUI应用开发总结:Android、iOS、Web、Swing、Windows开发等
  • 原文地址:https://www.cnblogs.com/renfanzi/p/14688820.html
Copyright © 2011-2022 走看看